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.List;
import java.util.TimerTask;
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.CraftOrdDesignStdCicEntity;
import com.steering.model.qcm.PlnSteelforfurnaceEntity;
import com.steering.model.qcm.QcmJhyElementsEntity;
import org.apache.log4j.Logger;

/**
 * 审核熔炼数据后台程序
 * @author Chenxi
 *
 */
public class AuditMeltingDataTask extends TimerTask{
	private final Logger logger = Logger.getLogger(MainTask.class);

	public void run(){
		synchronized (this){
			Connection con = null;
			Statement stmt = null;
			try{
				con = new DBCommon().getDao(CoreConstantData.sysbeanstr).getConnection();
				SqlBuilder b = new SqlBuilder("AuditMeltingDataTask.getJhyElements");
				List<QcmJhyElementsEntity> jhyElements = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJhyElementsEntity.class);
				stmt = con.createStatement();
				for(QcmJhyElementsEntity jhyElement : jhyElements){
					try {
						b = new SqlBuilder("AuditMeltingDataTask.getPlnSteelforfurnace");
						b.Fill(new String[]{ jhyElement.getStoveNo() });
						PlnSteelforfurnaceEntity steelforfurnace = EntityTool.getEntityBySqlNoClsCon(
								b.toString(), con, PlnSteelforfurnaceEntity.class);
						if(steelforfurnace == null) continue;
						b = new SqlBuilder("AuditMeltingDataTask.getCraftLg2Cic");
						b.Fill(new String[]{ steelforfurnace.getCraftNo() });
						ArrayList<CraftOrdDesignStdCicEntity> craftLg2CicList = EntityTool.getEntitysBySqlNoClsCon(
								b.toString(), con, CraftOrdDesignStdCicEntity.class);
						
						String judgeFlag = "00";
						Object[] results = ChemJudgeHelper.Judge(jhyElement, craftLg2CicList, null, false);
						if(!Boolean.parseBoolean(results[0].toString())){
							judgeFlag = "10";
						}else{
							judgeFlag = "20";
						}
						b = new SqlBuilder("AuditMeltingDataTask.updatePlnSteelforfurnace");
						b.Fill(new String[]{ judgeFlag, steelforfurnace.getStoveNo() });
						//stmt.addBatch(b.toString());
						stmt.executeUpdate(b.toString());
						b = new SqlBuilder("AuditMeltingDataTask.updateJhyElements");
						b.Fill(new String[]{ "20", steelforfurnace.getStoveNo() });
						//stmt.addBatch(b.toString());
						stmt.executeUpdate(b.toString());
						
						con.commit();
					}catch(Exception ex) {
						con.rollback();
						b = new SqlBuilder("AuditMeltingDataTask.updateJhyElements");
						b.Fill(new String[]{ "20", jhyElement.getStoveNo() });
						stmt.executeUpdate(b.toString());
						con.commit();
						logger.error("AuditMeltingDataTask异常: " + ex.getMessage());
					}
				}
			}catch (Exception ex) {
				try {
					con.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				logger.error("AuditMeltingDataTask异常: " + ex.getMessage());
				ex.printStackTrace();
			}finally{
				if(stmt != null){
					try {
						stmt.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(con != null){
					try {
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
