package com.platform.oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.base.Custom4exception;
import com.base.CustomException;
import com.platform.entities.OracleConnectorParams;
import com.platform.utils.Configs;
import com.platform.utils.FileOperateHelper;

public class OracleConnector {

	public static Logger log = Configs.DAILY_ROLLING_LOGGER
			.getLogger(OracleConnector.class);

	public OracleConnector() {
	}

	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Configs.CONSOLE_LOGGER.info("Oracle驱动加载成功");
		} catch (ClassNotFoundException e) {
			log.error(Custom4exception.OracleSQL_Except, e);
		}
	}

	public synchronized static Connection connectionBuilder(String url,
			String user, String password, OracleConnectorParams oc)
			throws CustomException {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			Configs.CONSOLE_LOGGER.info("创建oracle连接失败: [" + e.getMessage()
					+ "]");
			if (null != oc) {
				FileOperateHelper.fileWrite(
						Configs.EXTRACT_LOG_LOCALTION + oc.getName() + ".log",
						"创建oracle连接失败: [" + e.getMessage() + "]\r\n");
			}
			throw new CustomException(Custom4exception.OracleSQL_Except, e);
		}
		return conn;
	}

	public synchronized static boolean canConnect(String url, String user,
			String password) {
		Connection result = null;
		try {
			result = connectionBuilder(url, user, password, null);
		} catch (CustomException e) {
			log.error(Custom4exception.OracleSQL_Except, e);
		}
		return (null != result);
	}

	public synchronized static ResultSet getSQLExecResultSet(Connection conn,
			String sql, String filePath) {
		ResultSet resultSet = null;
		if (null != filePath) {
			filePath = filePath.replace(".log", "");
		}
		Statement statement = null;
		try {
			statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			resultSet = statement.executeQuery(sql);
			/*
			 * if(resultSet.next()){ System.out.println(resultSet.getInt(1)); }
			 */
			FileOperateHelper.fileWrite(filePath + ".log", sql + "\r\n"
					+ "OK \r\n");
		} catch (SQLException e) {
			FileOperateHelper.fileWrite(filePath + ".log",
					sql + "\r\n" + e.getMessage() + "\r\n");
			log.error(Custom4exception.OracleSQL_Except, e);
		} finally {
			if (null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					log.error(Custom4exception.OracleSQL_Except, e);
				}
			}
		}

		return resultSet;
	}

	public synchronized static ResultSet getSQLExecResultSet(String url,
			String user, String password, String sql, String filePath) {
		ResultSet result = null;
		try {
			result = getSQLExecResultSet(
					connectionBuilder(url, user, password, null), sql, filePath);
		} catch (CustomException e) {
			log.error(Custom4exception.OracleSQL_Except, e);
		}
		return result;
	}

	/**
	 * 执行对oracle数据库的增、删
	 * 
	 * @param conn
	 * @param sql
	 * @return true:执行的不返回集合数据的sql成功， 是否执行成功
	 */
	public synchronized static boolean execOracleSQL(Connection conn,
			String sql, String filePath) {
		if (null != filePath) {
			filePath = filePath.replace(".log", "");
		}
		boolean flag = false;
		Statement statement = null;
		try {
			statement = conn.createStatement();
			statement.executeUpdate(sql);
			flag = true;
			FileOperateHelper.fileWrite(filePath + ".log", sql + "\r\n" + flag
					+ " \r\n");
		} catch (SQLException e) {
			flag = false;
			FileOperateHelper.fileWrite(filePath + ".log",
					sql + "\r\n" + e.getMessage() + "\r\n");
			log.error(Custom4exception.OracleSQL_Except, e);
		} finally {
			if (null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					log.error(Custom4exception.OracleSQL_Except, e);
				}
			}
		}

		return flag ;
	}

	/**
	 * 执行对oracle数据库的返回集合数据的sql
	 * 
	 * @param conn
	 * @param sql
	 * @return true:执行结果大于1，即有数据 是否执行成功
	 */
	public synchronized static boolean execUpdateOracleSQL(Connection conn,
			String sql, String filePath) {
		if (null != filePath) {
			filePath = filePath.replace(".log", "");
		}
		boolean flag = false;
		Statement statement = null;
		try {
			statement = conn.createStatement();
			statement.executeUpdate(sql);
			flag = true;
			FileOperateHelper.fileWrite(filePath + ".log", sql + "\r\n"
					+ "OK \r\n");

		} catch (SQLException e) {
			flag = false;
			FileOperateHelper.fileWrite(filePath + ".log",
					sql + "\r\n" + e.getMessage() + "\r\n");
			log.error(Custom4exception.OracleSQL_Except, e);
		} finally {
			if (null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					log.error(Custom4exception.OracleSQL_Except, e);
				}
			}
		}
		return flag;
	}
}