package com.platform.service.thread;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.List;

import com.base.CustomException;
import com.platform.dao.DataInfoDao;
import com.platform.entities.CheckoutEntity;
import com.platform.entities.DataInfoEntity;
import com.platform.entities.GatherOracleInfo;
import com.platform.entities.OracleConnectorParams;
import com.platform.oracle.OracleConnector;
import com.platform.service.OracleExtractHelper;
import com.platform.utils.Configs;
import com.platform.utils.Constant;
import com.platform.utils.FileOperateHelper;

/** sqlServer 抽取 标准表 线程
 * @author chen
 *
 */
public class ThreadExtractStandardSqlServer extends Thread{
	
	/**
	 * 抽取
	 */
	private OracleExtractHelper oracleExtract = new OracleExtractHelper(); 
	
	/** 汇总库 */
	private GatherOracleInfo oracleConnect;
	
	/** 待抽取的实体 */
	private List<CheckoutEntity> Extractlist;
	
	/**	 数据管理 DAO	 */
	private DataInfoDao dataInfoDao;
	
	public ThreadExtractStandardSqlServer(GatherOracleInfo oracleConnect, List<CheckoutEntity> Extractlist, DataInfoDao dataInfoDao) {
		this.setDaemon(true);
		this.oracleConnect = oracleConnect;
		this.Extractlist = Extractlist;
		this.dataInfoDao = dataInfoDao;
	}
	
	@Override
	public void run() {
		for (CheckoutEntity element : Extractlist) {
			//校验结果：成功或失     0：未校验，1：不需校验，2：正在校验，3：校验成功，4：校验失败，5：待抽取，6：正在抽取，7：抽取完成
			 //TODO 开始抽取
			 //如果校验成功的  才 进行 抽取----支付标准表
			 if (Constant.CHECKOUT_STATUS_FIVE.equals(element.getPayResultLast())
					 || Constant.CHECKOUT_STATUS_SIX.equals(element.getPayResultLast())) {
				 String payFilePath = FileOperateHelper.addLastSeparator(element.getPath())+Constant.standard_pay
						 + element.getAreaCode().toLowerCase()+"_" + element.getSysCode()+".sql";
				 DataInfoEntity data = new DataInfoEntity();
				 data.setId(element.getDataId());
				 data.setPayResultLast(Constant.CHECKOUT_STATUS_SIX);
				 try {
					dataInfoDao.update(data);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					File paySql = new File(payFilePath);
					//获取连接
					Connection conn = OracleConnector.connectionBuilder("jdbc:oracle:thin:@" + oracleConnect.getIp() + ":" + oracleConnect.getPort() + ":"
							+ oracleConnect.getDatabaseName(), oracleConnect.getUser(), oracleConnect.getPassword(), null);
					//设置  日志 的 文件名
					OracleConnectorParams collectOracle = new OracleConnectorParams();
					collectOracle.setName("CQ"+ element.getAreaCode().toLowerCase()+"_"+element.getSysCode()+"_"+element.getDataVersion());
					// 创建表空间    创建  抽取标准表的 用户并授权
					oracleExtract.createTableSpace(conn, collectOracle , oracleConnect); //
					oracleExtract.createOnlyUser(conn, collectOracle, oracleConnect);//
					createPay(conn, collectOracle);
				
					//读取文件流
					FileInputStream fis = new FileInputStream(paySql);
					BufferedReader br = new BufferedReader(new InputStreamReader(fis,"GBK"));
					String sql = br.readLine();
					while (sql != null) {
						//TODO 执行sql
						sql = br.readLine();
						String resultSql = sql.replace(";", "");
						OracleConnector.execOracleSQL(conn, resultSql, FileOperateHelper.addLastSeparator(Configs.EXTRACT_STANDARD_LOG_LOCALTION)
								+ "CQ"+element.getAreaCode()+"_"+element.getSysCode()+"_"+element.getDataVersion()+".log");
						sql = br.readLine();
					}
					// 抽取完成
					 data.setPayResultLast(Constant.CHECKOUT_STATUS_SEVEN);
					 try {
						dataInfoDao.update(data);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CustomException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			 //如果校验成功的  才 进行 抽取----执行标准表
			 if (Constant.CHECKOUT_STATUS_FIVE.equals(element.getExecResultLast())
					 || Constant.CHECKOUT_STATUS_SIX.equals(element.getExecResultLast())){
				 String execFilePath = FileOperateHelper.addLastSeparator(element.getPath())+Constant.standard_indicate
						 + element.getAreaCode().toLowerCase() +"_" + element.getSysCode()+".sql";
				 DataInfoEntity data = new DataInfoEntity();
				 data.setId(element.getDataId());
				 data.setExecResultLast(Constant.CHECKOUT_STATUS_SIX);
				 try {
					dataInfoDao.update(data);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					File execSql = new File(execFilePath);
					Connection conn = OracleConnector.connectionBuilder("jdbc:oracle:thin:@" + oracleConnect.getIp() + ":" + oracleConnect.getPort() + ":"
							+ oracleConnect.getDatabaseName(), oracleConnect.getUser(), oracleConnect.getPassword(), null);
					//设置  日志 的 文件名
					OracleConnectorParams collectOracle = new OracleConnectorParams();
					collectOracle.setName("CQ"+ element.getAreaCode().toLowerCase()+"_"+element.getSysCode()+"_"+element.getDataVersion());
					// 创建表空间    创建  抽取标准表的 用户并授权
					oracleExtract.createTableSpace(conn, collectOracle , oracleConnect); //
					oracleExtract.createOnlyUser(conn, collectOracle, oracleConnect);//
					createExec(conn, collectOracle);
					
					FileInputStream fis = new FileInputStream(execSql);
					BufferedReader br = new BufferedReader(new InputStreamReader(fis,"GBK"));
					String sql = br.readLine();
					while (sql != null) {
						//TODO 执行sql
						//TODO 执行sql
						sql = br.readLine();
						String resultSql = sql.replace(";", "");
						OracleConnector.execOracleSQL(conn, resultSql, FileOperateHelper.addLastSeparator(Configs.EXTRACT_STANDARD_LOG_LOCALTION)
								+ "CQ"+element.getAreaCode()+"_"+element.getSysCode()+"_"+element.getDataVersion()+".log");
						sql = br.readLine();
					}
					//抽取完成
					data.setExecResultLast(Constant.CHECKOUT_STATUS_SEVEN);
					try {
						dataInfoDao.update(data);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CustomException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
			 }
		}
	}
	
	private void createPay(Connection conn, OracleConnectorParams oc) {
		String payCmd = "CREATE TABLE  u_bzbjy.zfxxb(XZQHDM Varchar(255),XZQHMC Varchar(255),PZBH Varchar(255),LYZBKZH Varchar(255),"
				+ "ZFDATE Varchar(255),YSDWCODE Varchar(255),YSDWNAME Varchar(255),YWGKCS Varchar(255),XMCODE Varchar(255),XMNAME Varchar(255),"
				+"XMLBCODE Varchar(255),XMLBNAME Varchar(255),ZB_NO Varchar(255),GNFLCODE Varchar(255),GNFLNAME Varchar(255),JJFLCODE Varchar(255),"
				+"JJFLNAME Varchar(255),ZJXZCODE Varchar(255),ZJXZNAME Varchar(255),JSBFFSNAME Varchar(255),SKR Varchar(255),SKRYH Varchar(255),"
				+ "SKRZHZH Varchar(255),FKZHCODE Varchar(255),FKZHNAME Varchar(255),FKYHCODE Varchar(255),FKYHNAME Varchar(255),QSZHCODE Varchar(255),"
				+ "QSZHNAME Varchar(255),QSYHCODE Varchar(255),QSYHNAME Varchar(255),JE Numeric(18,2), SFTK Varchar(255),NIAN Varchar(255),ZY Varchar(255))";
		try{
			OracleConnector.execOracleSQL(conn, payCmd, Configs.EXTRACT_STANDARD_LOG_LOCALTION + oc.getName());
		}catch(Exception e){
			
		}
	}
	
	private void createExec(Connection conn, OracleConnectorParams oc) {
		String execCmd = "CREATE TABLE  u_bzbjy.kzxzb(XZQHDM Varchar(255),XZQHMC Varchar(255),YSND Varchar(255),ZBCODE Varchar(255),ZB_ID Varchar(255),ZB_NO Varchar(255),"
				+ "ZBDJLXCODE Varchar(255),ZBDJLXNAME Varchar(255),ZBLXNAME Varchar(255),DOCNO Varchar(255),ZBSM Varchar(255),ZBFWDATE Varchar(255),ZBYSLXCODE Varchar(255),"
				+ "ZBYSLXNAME Varchar(255),ZBYSLYNAME Varchar(255),YSDWCODE Varchar(255),YSDWNAME Varchar(255),GNFLCODE Varchar(255),GNFLNAME Varchar(255),JJFLCODE Varchar(255),"
				+ "JJFLNAME Varchar(255),ZBGLCSNAME Varchar(255),SZGLCODE Varchar(255),SZGLNAME Varchar(255),XMCODE Varchar(255),XMNAME Varchar(255),GZBZ Varchar(255),"
				+ "JJBZ Varchar(255),CGBZ Varchar(255),ZFFSCODE Varchar(255),ZFFSNAME Varchar(255),JZZFBZ Varchar(255),ZBJE Numeric(18,2),ZBTJJE Numeric(18,2),ZBDJJE Numeric(18,2),"
				+ "ZBKYJE Numeric(18,2),ZYZFBZ Varchar(255),BZ Varchar(255))";
		try{
			OracleConnector.execOracleSQL(conn, execCmd, Configs.EXTRACT_STANDARD_LOG_LOCALTION + oc.getName());
		}catch(Exception e){
			
		}
	}

}
