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 com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.model.qcm.QcmJhyElementsEntity;
import com.steering.model.qcm.SteerChemAuditResultEntity;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import org.apache.log4j.Logger;

public class JhyLgChemToQcmChemTask extends TimerTask {
	private static class JhyLgChemToQcmChemTaskClass {
		private static final JhyLgChemToQcmChemTask instance = new JhyLgChemToQcmChemTask();
	}
	
	private JhyLgChemToQcmChemTask(){};
	
	public static JhyLgChemToQcmChemTask getInstance(){
		return JhyLgChemToQcmChemTaskClass.instance;
	}
	private final Logger logger = Logger.getLogger(MainTask.class);

	//private boolean _run = false;
	public void run(){
//		if(_run){
//			//System.out.print("跳出");
//			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);
				ArrayList<SteerChemAuditResultEntity> steerChemAuditResults = getSteelChemAuditResult(jhyCon);
				jhyStmt = jhyCon.createStatement();
				qcmStmt = qcmCon.createStatement();
				for (SteerChemAuditResultEntity steerChemAuditResult : steerChemAuditResults) {
					try {
						QcmJhyElementsEntity qcmJhyElement = new QcmJhyElementsEntity();
						CopyData(steerChemAuditResult, qcmJhyElement);
						
						//清除质量成分表
						String sqlClear = CoreXmlSqlParsersFactory.getSql("JhyLgChemToQcmChemTask.clear");
						sqlClear = EntityTool.fillSqlByEntity(sqlClear, qcmJhyElement);
						//qcmStmt.addBatch(sqlClear);
						qcmStmt.executeUpdate(sqlClear);
						String sqlInsertQcmJhyElements = CoreXmlSqlParsersFactory
								.getSql("JhyLgChemToQcmChemTask.insertQcmJhyElements");
						sqlInsertQcmJhyElements = EntityTool.fillSqlByEntity(sqlInsertQcmJhyElements, qcmJhyElement);
						//qcmStmt.addBatch(sqlInsertQcmJhyElements);
						qcmStmt.executeUpdate(sqlInsertQcmJhyElements);
						String sqlUpdateCxFlag = CoreXmlSqlParsersFactory.getSql("JhyLgChemToQcmChemTask.updateCxFlag");
						steerChemAuditResult.setCxFlag("1");
						sqlUpdateCxFlag = EntityTool.fillSqlByEntity(sqlUpdateCxFlag, steerChemAuditResult);
						//jhyStmt.addBatch(sqlUpdateCxFlag);
						jhyStmt.executeUpdate(sqlUpdateCxFlag);
						
						qcmCon.commit();
						jhyCon.commit();
					}catch(Exception ex) {
						jhyCon.rollback();
						qcmCon.rollback();
						String sqlUpdateCxFlag = CoreXmlSqlParsersFactory.getSql("JhyLgChemToQcmChemTask.updateCxFlag");
						steerChemAuditResult.setCxFlag("-1");
						sqlUpdateCxFlag = EntityTool.fillSqlByEntity(sqlUpdateCxFlag, steerChemAuditResult);
						jhyStmt.executeUpdate(sqlUpdateCxFlag);
						jhyCon.commit();
						String msg = "JhyLgChemToQcmChemTask异常: StoveNo=" + steerChemAuditResult.getHeatNo() + ex.getMessage();
						logger.error(msg);
					}
				}
			} catch (Exception ex) {
				try {
					jhyCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				try {
					qcmCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				ex.printStackTrace();
				logger.error("JhyLgChemToQcmChemTask异常: " + 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;
			}
		}
	}

	private ArrayList<SteerChemAuditResultEntity> getSteelChemAuditResult(Connection con) {
		String sqlGetSteelChemAuditResult = CoreXmlSqlParsersFactory
				.getSql("JhyLgChemToQcmChemTask.getSteelChemAuditResult");
		ArrayList<SteerChemAuditResultEntity> steerChemAuditResults = EntityTool
				.getEntitysBySqlNoClsCon(sqlGetSteelChemAuditResult, con, SteerChemAuditResultEntity.class);
		return steerChemAuditResults;
	}

	private void CopyData(SteerChemAuditResultEntity steerChemAuditResult,
			QcmJhyElementsEntity qcmJhyElement) {
		if(!steerChemAuditResult.getAnaAl().equals("")){
			qcmJhyElement.setAl(steerChemAuditResult.getAnaAl());
		}else if(!steerChemAuditResult.getAnaAlt().equals("")){
			qcmJhyElement.setAl(steerChemAuditResult.getAnaAlt());
		}
		qcmJhyElement.setAl2o3(steerChemAuditResult.getAnaAl2o3());
		qcmJhyElement.setAlins(steerChemAuditResult.getAnaAlins());
		qcmJhyElement.setAls(steerChemAuditResult.getAnaAls());
		if(!steerChemAuditResult.getAnaAlt().equals("")){
			qcmJhyElement.setAlt(steerChemAuditResult.getAnaAlt());
		}else if(!steerChemAuditResult.getAnaAl().equals("")){
			qcmJhyElement.setAlt(steerChemAuditResult.getAnaAl());
		}
		qcmJhyElement.setAsn(steerChemAuditResult.getAnaAs());
		qcmJhyElement.setAssayno(steerChemAuditResult.getHeatNo() + "_" + steerChemAuditResult.getSampleNo() + "_S");
		qcmJhyElement.setAssaytype("A");// A表示熔炼成分
		qcmJhyElement.setB(steerChemAuditResult.getAnaB());
		qcmJhyElement.setBatchNo("");// 没有性能批号
		qcmJhyElement.setBi(steerChemAuditResult.getAnaBi());
		qcmJhyElement.setBn(steerChemAuditResult.getAnaBn());
		qcmJhyElement.setBs(steerChemAuditResult.getAnaBs());
		qcmJhyElement.setC(steerChemAuditResult.getAnaC());
		qcmJhyElement.setCa(steerChemAuditResult.getAnaCa());
		qcmJhyElement.setCaf2(steerChemAuditResult.getAnaCaf2());
		qcmJhyElement.setCao(steerChemAuditResult.getAnaCao());
		qcmJhyElement.setCd(steerChemAuditResult.getAnaCd());
		qcmJhyElement.setCe(steerChemAuditResult.getAnaCe());
		qcmJhyElement.setCo(steerChemAuditResult.getAnaCo());
		qcmJhyElement.setCr(steerChemAuditResult.getAnaCr());
		qcmJhyElement.setCr2o3(steerChemAuditResult.getAnaCr2o3());
		qcmJhyElement.setCreateName(steerChemAuditResult.getCheckName());
		qcmJhyElement.setCu(steerChemAuditResult.getAnaCu());
		qcmJhyElement.setFe(steerChemAuditResult.getAnaFe());
		qcmJhyElement.setFe2o3(steerChemAuditResult.getAnaFe2o3());
		qcmJhyElement.setFeo(steerChemAuditResult.getAnaFeo());
		qcmJhyElement.setH(steerChemAuditResult.getAnaH());
		qcmJhyElement.setH2o(steerChemAuditResult.getAnaH2o());
		qcmJhyElement.setJudgeStoveNo("");// 判定炉号没有
		qcmJhyElement.setLa(steerChemAuditResult.getAnaLa());
		qcmJhyElement.setMachine(steerChemAuditResult.getDeviceName());// 光谱仪类型没有
		qcmJhyElement.setMemo(steerChemAuditResult.getRemark());
		qcmJhyElement.setMg(steerChemAuditResult.getAnaMg());
		qcmJhyElement.setMgo(steerChemAuditResult.getAnaMgo());
		qcmJhyElement.setMn(steerChemAuditResult.getAnaMn());
		qcmJhyElement.setMno(steerChemAuditResult.getAnaMno());
		qcmJhyElement.setMo(steerChemAuditResult.getAnaMo());
		qcmJhyElement.setN(steerChemAuditResult.getAnaN());
		qcmJhyElement.setNb(steerChemAuditResult.getAnaNb());
		qcmJhyElement.setNi(steerChemAuditResult.getAnaNi());
		qcmJhyElement.setO(steerChemAuditResult.getAnaO());
		qcmJhyElement.setP(steerChemAuditResult.getAnaP());
		qcmJhyElement.setP2o5(steerChemAuditResult.getAnaP2o5());
		qcmJhyElement.setPb(steerChemAuditResult.getAnaPb());
		qcmJhyElement.setZr(steerChemAuditResult.getAnaZr());
		if (steerChemAuditResult.getSampleSource().equals("3")) {
			qcmJhyElement.setPlineCode("C006");
		} else if (steerChemAuditResult.getSampleSource().equals("2")) {
			qcmJhyElement.setPlineCode("C003");
		} else {
			qcmJhyElement.setPlineCode("C001");
		}
		qcmJhyElement.setS(steerChemAuditResult.getAnaS());
		qcmJhyElement.setSampleId("");// 试验编号没有
		qcmJhyElement.setSampleNo(steerChemAuditResult.getSampleNo().trim());
		qcmJhyElement.setSb(steerChemAuditResult.getAnaSb());
		qcmJhyElement.setSe(steerChemAuditResult.getAnaSe());
		qcmJhyElement.setSi(steerChemAuditResult.getAnaSi());
		qcmJhyElement.setSio2(steerChemAuditResult.getAnaSio2());
		qcmJhyElement.setSn(steerChemAuditResult.getAnaSn());
		qcmJhyElement.setStoveNo(steerChemAuditResult.getHeatNo());
		qcmJhyElement.setTfe(steerChemAuditResult.getAnaTfe());
		qcmJhyElement.setTfeo(steerChemAuditResult.getAnaTfeo());
		qcmJhyElement.setTi(steerChemAuditResult.getAnaTi());
		qcmJhyElement.setTio2(steerChemAuditResult.getAnaTio2());
		qcmJhyElement.setV(steerChemAuditResult.getAnaV());
		qcmJhyElement.setV2o5(steerChemAuditResult.getAnaV2o5());
		qcmJhyElement.setW(steerChemAuditResult.getAnaW());
		qcmJhyElement.setXh("");// 无序号
		qcmJhyElement.setZn(steerChemAuditResult.getAnaZn());
		qcmJhyElement.setTa(steerChemAuditResult.getAnaTa());
	}
}
