package com.steering.jobface.qcm.task;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.TimerTask;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.model.qcm.QcmJhyLgPhyresultEntity;
import com.steering.model.qcm.SteerPhydbAuditResultEntity;
import org.apache.log4j.Logger;

public class SteerPhyDbToQcmLgPhyTask extends TimerTask{
	private static class SteerPhyDbToQcmLgPhyTaskClass {
		private static final SteerPhyDbToQcmLgPhyTask instance = new SteerPhyDbToQcmLgPhyTask();
	}
	
	private SteerPhyDbToQcmLgPhyTask(){};
	
	public static SteerPhyDbToQcmLgPhyTask getInstance(){
		return SteerPhyDbToQcmLgPhyTaskClass.instance;
	}
	private final Logger logger = Logger.getLogger(MainTask.class);
	public void run(){
		synchronized (this) {
			Connection qcmCon = null;
			Connection jhyCon = null;
			Statement jhyStmt = null;
			Statement qcmStmt = null;
			try{
				qcmCon = new DBCommon().getDao(CoreConstantData.sysbeanstr).getConnection();
				jhyCon = new DBCommon().getDao("JhyDao").getConnection();
				jhyCon.setAutoCommit(false);
				qcmCon.setAutoCommit(false);
				jhyStmt = jhyCon.createStatement();
				qcmStmt = qcmCon.createStatement();
				List<SteerPhydbAuditResultEntity> steerPhyDbs = getSteerPhyDb(jhyCon);
				int indexSeq = 0;
				for(SteerPhydbAuditResultEntity steerPhyDb : steerPhyDbs){
					try {
						QcmJhyLgPhyresultEntity qcmJhyLgPhy = new QcmJhyLgPhyresultEntity();
						indexSeq++;
						copyData(steerPhyDb, qcmJhyLgPhy, String.valueOf(indexSeq));
						
						//清除质量理化低倍表
						String sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.DeleteQcmJhyLgPhy");
						sql = EntityTool.fillSqlByEntity(sql, qcmJhyLgPhy);
						//qcmStmt.addBatch(sql);
						qcmStmt.executeUpdate(sql);
						
						//新增质量理化低倍表
						sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.insertQcmJhyLgPhy");
						sql = EntityTool.fillSqlByEntity(sql, qcmJhyLgPhy);
						//qcmStmt.addBatch(sql);
						qcmStmt.executeUpdate(sql);
						
						//回写cxflag
						sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.updateSteerPhyDb");
						steerPhyDb.setCxFlag("1");
						sql = EntityTool.fillSqlByEntity(sql, steerPhyDb);
						//jhyStmt.addBatch(sql);
						jhyStmt.executeUpdate(sql);
						
						qcmCon.commit();
						jhyCon.commit();
					}catch(Exception ex) {
						jhyCon.rollback();
						qcmCon.rollback();
						//回写cxflag
						String sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.updateSteerPhyDb");
						steerPhyDb.setCxFlag("-1");
						sql = EntityTool.fillSqlByEntity(sql, steerPhyDb);
						jhyStmt.executeUpdate(sql);
						jhyCon.commit();
						logger.error("SteerPhyDbToQcmLgPhyTask异常: " + ex.getMessage());
					}
				}
			}catch(Exception ex){
				try {
					jhyCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				try {
					qcmCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				logger.error("SteerPhyDbToQcmLgPhyTask异常: " + ex.getMessage());
			}finally{
				if(jhyStmt != null){
					try {
						jhyStmt.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(qcmStmt != null){
					try {
						qcmStmt.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(jhyCon != null){
					try {
						jhyCon.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(qcmCon != null){
					try {
						qcmCon.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				//_run = false;
			}
		}
	}
	
//	public SteerPhyDbToQcmLgPhyTask(Connection jhyCon, Connection qcmCon){
//		Statement jhyStmt = null;
//		Statement qcmStmt = null;
//		try{
//			jhyCon.setAutoCommit(false);
//			qcmCon.setAutoCommit(false);
//			jhyStmt = jhyCon.createStatement();
//			qcmStmt = qcmCon.createStatement();
//			List<SteerPhydbAuditResultEntity> steerPhyDbs = getSteerPhyDb(jhyCon);
//			int indexSeq = 0;
//			for(SteerPhydbAuditResultEntity steerPhyDb : steerPhyDbs){
//				QcmJhyLgPhyresultEntity qcmJhyLgPhy = new QcmJhyLgPhyresultEntity();
//				indexSeq++;
//				copyData(steerPhyDb, qcmJhyLgPhy, String.valueOf(indexSeq));
//				
//				//清除质量理化低倍表
//				String sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.DeleteQcmJhyLgPhy");
//				sql = EntityTool.fillSqlByEntity(sql, qcmJhyLgPhy);
//				qcmStmt.addBatch(sql);
//				
//				//新增质量理化低倍表
//				sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.insertQcmJhyLgPhy");
//				sql = EntityTool.fillSqlByEntity(sql, qcmJhyLgPhy);
//				qcmStmt.addBatch(sql);
//				
//				//回写cxflag
//				sql = CoreXmlSqlParsersFactory.getSql("SteerPhyDbToQcmLgPhyTask.updateSteerPhyDb");
//				sql = EntityTool.fillSqlByEntity(sql, steerPhyDb);
//				jhyStmt.addBatch(sql);
//			}
//			qcmStmt.executeBatch();
//			jhyStmt.executeBatch();
//			
//			jhyCon.commit();
//			qcmCon.commit();
//		}catch(Exception ex){
//			MainTask.InsertException(ex.getMessage(), "SteerPhyDbToQcmLgPhyTask异常", qcmCon);
//			try {
//				jhyCon.rollback();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//			try {
//				qcmCon.rollback();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		}finally{
//			if(jhyStmt != null){
//				try {
//					jhyStmt.close();
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}
//			}
//			if(qcmStmt != null){
//				try {
//					qcmStmt.close();
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}
//			}
//		}
//	}
	
	private List<SteerPhydbAuditResultEntity> getSteerPhyDb(Connection jhyCon){
		SqlBuilder b = new SqlBuilder("SteerPhyDbToQcmLgPhyTask.getSteerPhyDb");
		List<SteerPhydbAuditResultEntity> steerPhyDbs = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), jhyCon, SteerPhydbAuditResultEntity.class);
		
		return steerPhyDbs;
	}
	
	private void copyData(SteerPhydbAuditResultEntity steerPhyDb, QcmJhyLgPhyresultEntity qcmJhyLgPhy,
			String indexSeq){
		qcmJhyLgPhy.setSampleId(steerPhyDb.getChangeHeatNo() + "_" + steerPhyDb.getSampleNo() + "P");
		qcmJhyLgPhy.setSampleNo(steerPhyDb.getSampleNo());
		qcmJhyLgPhy.setIndexSeq(indexSeq);
		qcmJhyLgPhy.setStoveNo(steerPhyDb.getHeatNo());
		qcmJhyLgPhy.setJudgeStoveNo(steerPhyDb.getChangeHeatNo());
		qcmJhyLgPhy.setSampleType("0");
		qcmJhyLgPhy.setPhyCodeMax(steerPhyDb.getPhyCodeMax());
		qcmJhyLgPhy.setPhyNameMax(steerPhyDb.getPhyNameMax());
		qcmJhyLgPhy.setPhyCodeMin(steerPhyDb.getPhyCodeMin());
		qcmJhyLgPhy.setPhyNameMin(steerPhyDb.getPhyNameMin());
		qcmJhyLgPhy.setPhyCode(steerPhyDb.getPhyCode());
		qcmJhyLgPhy.setPhyName(steerPhyDb.getPhyName());
		qcmJhyLgPhy.setItemCodeF(steerPhyDb.getItemCodeF());
		qcmJhyLgPhy.setItemNameF(steerPhyDb.getItemNameF());
		qcmJhyLgPhy.setItemCodeS(steerPhyDb.getItemCodeS());
		qcmJhyLgPhy.setItemNameS(steerPhyDb.getItemNameS());
		qcmJhyLgPhy.setItemCodeW(steerPhyDb.getItemCodeW());
		qcmJhyLgPhy.setItemNameW(steerPhyDb.getItemNameW());
		qcmJhyLgPhy.setValue1(steerPhyDb.getValue1());
		qcmJhyLgPhy.setValue2(steerPhyDb.getValue2());
		qcmJhyLgPhy.setValue3(steerPhyDb.getValue3());
		qcmJhyLgPhy.setCreateName("SYSTEM");
	}
}
