package com.platform.service;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.base.Custom4exception;
import com.platform.entities.AbstractOracleExtractTask;
import com.platform.entities.GatherOracleInfo;
import com.platform.entities.OracleConnectorParams;
import com.platform.oracle.OracleConnector;
import com.platform.utils.Configs;
import com.platform.utils.Constant;
import com.platform.utils.DateForm;
import com.platform.utils.FileOperateHelper;

public class OracleExtractTask extends AbstractOracleExtractTask {

	public static Logger log = Logger.getLogger(OracleExtractTask.class);

	private GatherOracleInfo gatherOracleInfo;

	private OracleConnectorParams oc;

	private IOracleExtractService OracleExtract;

	public OracleExtractTask(String name, GatherOracleInfo gatherOracleInfo,
			OracleConnectorParams oc, IOracleExtractService OracleExtract) {
		super(name);
		this.gatherOracleInfo = gatherOracleInfo;
		this.oc = oc;
		this.OracleExtract = OracleExtract;
	}

	public OracleExtractTask(GatherOracleInfo gatherOracleInfo,
			OracleConnectorParams oc, IOracleExtractService OracleExtract) {
		this.gatherOracleInfo = gatherOracleInfo;
		this.oc = oc;
		this.OracleExtract = OracleExtract;
	}

	private OracleExtractHelper oracleExtract = new OracleExtractHelper();

	@Override
	public void handler() {
		// TODO Auto-generated method stub
		try {
			Connection conn = OracleConnector.connectionBuilder(
					createConnectUrl(), gatherOracleInfo.getUser(),
					gatherOracleInfo.getPassword(), oc);
			if (null != conn) {
				String cmd = "kubectl label --overwrite rc " + oc.getName()
						+ " isExtract=1";
				OracleExtract.updateDataExtractStatus(oc, 1); // 更新数据库的状态
				FileOperateHelper
						.fileWrite(
								Configs.EXTRACT_LOG_LOCALTION + oc.getName()
										+ ".log",
								"["
										+ DateForm
												.date2StringBysecond(new Date())
										+ "]>>>>>>>>>>>>>>>>开始汇总 >>>>>>>>>>>>>>>>>>\r\n");
				List<String> rList = Constant.ganymedSSH
						.execCmdWaitAcquiescent(cmd);
				StringBuffer sb = new StringBuffer();
				for (String string : rList)
					sb.append(string).append("\n");
				Configs.CONSOLE_LOGGER.info(sb.toString());
				oracleExtract.createDBLink(conn, oc); // 创建dblink
				oracleExtract.createTableSpace(conn, oc, gatherOracleInfo); // 创建表空间
				oracleExtract.createUser(conn, oc, gatherOracleInfo);// 创建用户并授权
				oracleExtract.extractColleDB(conn, oc, gatherOracleInfo);// 执行抽取
				cmd = "kubectl label --overwrite rc " + oc.getName()
						+ " isExtract=2";
				rList = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
				sb = new StringBuffer();
				for (String string : rList)
					sb.append(string).append("\n");
				Configs.CONSOLE_LOGGER.info(sb.toString());
				FileOperateHelper
						.fileWrite(
								Configs.EXTRACT_LOG_LOCALTION + oc.getName()
										+ ".log",
								"["
										+ DateForm
												.date2StringBysecond(new Date())
										+ "]>>>>>>>>>>>>>>>>汇总结束 >>>>>>>>>>>>>>>>>>\r\n\r\n");
				OracleExtract.updateDataExtractStatus(oc, 2); // 更新数据库的状态
			}
		} catch (Exception e) {
			log.error(Custom4exception.OracleSQL_Except, e);
		}
	}

	private String createConnectUrl() {
		String answer = "";
		if (null != gatherOracleInfo) {
			answer = "jdbc:oracle:thin:@" + gatherOracleInfo.getIp() + ":"
					+ gatherOracleInfo.getPort() + ":"
					+ gatherOracleInfo.getDatabaseName();
		}
		return answer;
	}

}
