package com.steering.jobface.qcm.task;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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.SteerPhyDbBaseInfoEntity;
import org.apache.log4j.Logger;

public class JhyPhyDbBaseInfoToQcmPhyEntrust extends TimerTask {
	private static class JhyPhyDbBaseInfoToQcmPhyEntrustClass {
		private static final JhyPhyDbBaseInfoToQcmPhyEntrust instance = new JhyPhyDbBaseInfoToQcmPhyEntrust();
	}
	
	private JhyPhyDbBaseInfoToQcmPhyEntrust(){};
	
	public static JhyPhyDbBaseInfoToQcmPhyEntrust getInstance(){
		return JhyPhyDbBaseInfoToQcmPhyEntrustClass.instance;
	}
	private final Logger logger = Logger.getLogger(MainTask.class);
	//private boolean _run = false;
	public void run(){
//		if(_run){
//			return;
//		}
//		_run = true;
		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);
				SqlBuilder b = new SqlBuilder("JhyPhyDbBaseInfoToQcmPhyEntrust.queryJhyPhyDbBaseInfo");
				ArrayList<SteerPhyDbBaseInfoEntity> steerPhyDbBaseInfoResults = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), jhyCon, SteerPhyDbBaseInfoEntity.class);
				jhyStmt = jhyCon.createStatement();
				qcmStmt = qcmCon.createStatement();
				for (SteerPhyDbBaseInfoEntity steerPhyDbBaseInfoResult : steerPhyDbBaseInfoResults) {
					try {
						//清除质量成分表
						String sqlClear = CoreXmlSqlParsersFactory.getSql("JhyPhyDbBaseInfoToQcmPhyEntrust.deleteQcmPhyEntrust");
						sqlClear = EntityTool.fillSqlByEntity(sqlClear, steerPhyDbBaseInfoResult);
						//qcmStmt.addBatch(sqlClear);
						qcmStmt.executeUpdate(sqlClear);
						
						String sqlInsertQcmPhyEntrust = CoreXmlSqlParsersFactory
								.getSql("JhyPhyDbBaseInfoToQcmPhyEntrust.insertQcmPhyEntrust");
						sqlInsertQcmPhyEntrust = EntityTool.fillSqlByEntity(sqlInsertQcmPhyEntrust, steerPhyDbBaseInfoResult);
						//qcmStmt.addBatch(sqlInsertQcmPhyEntrust);
						qcmStmt.executeUpdate(sqlInsertQcmPhyEntrust);

						String sqlUpdateCxFlag = CoreXmlSqlParsersFactory.getSql("JhyPhyDbBaseInfoToQcmPhyEntrust.updateJhyPhyDbBaseInfo");
						steerPhyDbBaseInfoResult.setCxFlag("1");
						sqlUpdateCxFlag = EntityTool.fillSqlByEntity(sqlUpdateCxFlag, steerPhyDbBaseInfoResult);
						//jhyStmt.addBatch(sqlUpdateCxFlag);
						jhyStmt.executeUpdate(sqlUpdateCxFlag);
						qcmCon.commit();
						jhyCon.commit();
					}catch(Exception ex) {
						jhyCon.rollback();
						qcmCon.rollback();
						String sqlUpdateCxFlag = CoreXmlSqlParsersFactory.getSql("JhyPhyDbBaseInfoToQcmPhyEntrust.updateJhyPhyDbBaseInfo");
						steerPhyDbBaseInfoResult.setCxFlag("-1");
						sqlUpdateCxFlag = EntityTool.fillSqlByEntity(sqlUpdateCxFlag, steerPhyDbBaseInfoResult);
						//jhyStmt.addBatch(sqlUpdateCxFlag);
						jhyStmt.executeUpdate(sqlUpdateCxFlag);
						jhyCon.commit();
						logger.error("JhyPhyDbBaseInfoToQcmPhyEntrust异常: " + ex.getMessage());
					}
				}
			} catch (Exception ex) {
				try {
					jhyCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				try {
					qcmCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				ex.printStackTrace();
				logger.error("JhyPhyDbBaseInfoToQcmPhyEntrust异常: " + 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;
			}
		}
	}
}
