package com.steering.jobface.qcm.task;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TimerTask;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.model.qcm.*;
import org.apache.log4j.Logger;

public class JhyCpChemToQcmChemTask extends TimerTask{
	private static class JhyCpChemToQcmChemTaskClass{
		private static final JhyCpChemToQcmChemTask instance = new JhyCpChemToQcmChemTask();
	}
	
	private JhyCpChemToQcmChemTask(){}
	
	public static JhyCpChemToQcmChemTask getInstance(){
		return JhyCpChemToQcmChemTaskClass.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);
				ArrayList<PipeCheckconsignDetailEntity> pipeDetails = getPipeCheckConsignDetail(jhyCon);
				jhyStmt = jhyCon.createStatement();
				qcmStmt = qcmCon.createStatement();
				SqlBuilder b = null;
				for (PipeCheckconsignDetailEntity pipeDetail : pipeDetails) {
					try {
		//				ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetails = getPipeCheckConsignChemDetail(pipeDetail,
		//						jhyCon);
						//如果是复样或者逐支则需要在获取初样的数据。
						ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetails = getFullSample(
								pipeDetail.getCheckNo(), jhyCon, qcmCon);
		
						//按试样号分组
						HashMap<String, ArrayList<PipeCheckconsignChemdetailEntity>> mapChems = new HashMap<String, ArrayList<PipeCheckconsignChemdetailEntity>>();
						for(PipeCheckconsignChemdetailEntity pipeChemDetail : pipeChemDetails){
							if(!mapChems.containsKey(pipeChemDetail.getSampleNo())){
								mapChems.put(pipeChemDetail.getSampleNo(), new ArrayList<PipeCheckconsignChemdetailEntity>());
							}
							mapChems.get(pipeChemDetail.getSampleNo()).add(pipeChemDetail);
						}

						String newCheckNo = "";//共样新检验编号
						for(String key : mapChems.keySet()){
							QcmJhyElementsEntity qcmJhyElement = new QcmJhyElementsEntity();
							CopyData(pipeDetail, mapChems.get(key), qcmJhyElement, b, qcmCon);

							insertJhyChem(qcmJhyElement, qcmJhyElement.getSampleId(), qcmStmt);

							//复制首日检共样
                            newCheckNo = copyFdCheckConsign(qcmJhyElement, b, qcmCon, qcmStmt);
						}
						
						//把检化验同步标志置为5
						b = new SqlBuilder("JhyCpChemToQcmChemTask.getUpdateFlag");
						b.Fill(new String[]{ pipeDetail.getCheckNo(), "5" });
						//jhyStmt.addBatch(b.toString());
						jhyStmt.executeUpdate(b.toString());
						
						//更新委托一层表
						b = new SqlBuilder("JhyCpChemToQcmChemTask.updateBaseInfoFlag");
						b.Fill(new String[]{ pipeDetail.getCheckNo() });
						//jhyStmt.addBatch(b.toString());
						jhyStmt.executeUpdate(b.toString());
						
						//新增委托主表
						saveQcmJhyCheckconsign(pipeDetail, qcmCon, qcmStmt);

                        //更新委托表的报出情况
                        setConsignQuoteState(pipeDetail.getCheckNo(), qcmCon, qcmStmt);
                        if(!newCheckNo.equals("")){
                            setConsignQuoteState(newCheckNo, qcmCon, qcmStmt);
                        }

						jhyCon.commit();
						qcmCon.commit();
					}catch(Exception ex) {
						jhyCon.rollback();
						qcmCon.rollback();
						//把检化验同步标志置为-1
						b = new SqlBuilder("JhyCpChemToQcmChemTask.getUpdateFlag");
						b.Fill(new String[]{ pipeDetail.getCheckNo(), "3" });
						//jhyStmt.addBatch(b.toString());
						jhyStmt.executeUpdate(b.toString());
						jhyCon.commit();
						qcmCon.commit();
						logger.error("JhyCpChemToQcmChemTask异常: CheckNo=" + pipeDetail.getCheckNo() + "PhyCodeMins="
                                + pipeDetail.getPhyCodeMax() + ex.getMessage());
					}
				}
			} catch (Exception ex) {
				try {
					jhyCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				try {
					qcmCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
                logger.error("JhyCpChemToQcmChemTask异常:" + ex.getMessage());
				ex.printStackTrace();
			}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;
			}
		}
	}

	/**
	 * 保存126委托主表同步到105
	 * @param pipeDetail
	 * @param con
	 * @param st
	 * @throws SQLException
	 */
	public static void saveQcmJhyCheckconsign(PipeCheckconsignDetailEntity pipeDetail, Connection con,
			Statement st) throws SQLException{
		String strConsignCnt = CoreXmlSqlParsersFactory
				.getSql("JhyCpChemToQcmChemTask.getQcmJhyCheckconsignCount");
		strConsignCnt = StringTool.ReplaceAll(strConsignCnt, "#CHECK_NO#", pipeDetail.getCheckNo());
		GenericEntity consignCnt = EntityTool.getEntityBySqlNoClsCon(strConsignCnt, con, GenericEntity.class);
		if(consignCnt.getCount().equals("0")){
			String strInsert = CoreXmlSqlParsersFactory
					.getSql("JhyCpChemToQcmChemTask.insertQcmJhyCheckconsign");
			strInsert = EntityTool.fillSqlByEntity(strInsert, pipeDetail);
			st.executeUpdate(strInsert);
		}else{
			String strUpdate = CoreXmlSqlParsersFactory
					.getSql("JhyCpChemToQcmChemTask.updateQcmJhyCheckconsign");
			strUpdate = EntityTool.fillSqlByEntity(strUpdate, pipeDetail);
			st.executeUpdate(strUpdate);
		}
	}
	
	/**
	 * 保存委托表的报出情况
	 * @param checkNo
	 * @param con
	 * @param st
	 * @throws SQLException
	 */
	public static void setConsignQuoteState(String checkNo, Connection con, Statement st) throws SQLException{
		st.executeBatch();
		SqlBuilder b = new SqlBuilder("JhyCpChemToQcmChemTask.getPhyMinQuote");
		b.Fill(new String[]{ checkNo });
		ArrayList<QcmZcCheckconsignDetailEntity> consignDetais = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, QcmZcCheckconsignDetailEntity.class);
		String quoteState = "2";
		
		boolean noSsc = false;
    	boolean noHic = false;
        boolean noOther = false;
		for(QcmZcCheckconsignDetailEntity consignDetai : consignDetais){
			if(consignDetai.getCnt().equals("0")){
				quoteState = "1";
				if(consignDetai.equals("C0030") || consignDetai.equals("C0031")
						|| consignDetai.equals("C0032") || consignDetai.equals("C0033")
						|| consignDetai.equals("C0133") || consignDetai.equals("C0147")
						|| consignDetai.equals("C0150")){
					noSsc = true;
				}else if(consignDetai.equals("C0007") || consignDetai.equals("C0146")
						||consignDetai.equals("C0061") || consignDetai.equals("C0148")
						||consignDetai.equals("C0065")){
					noHic = true;
				}else{
					noOther = true;
				}
			}
		}
		
		String isNoSsc = "0";
    	if(noSsc && !noOther){
    		isNoSsc = "1";
    	}
    	String isNoHic = "0";
    	if(noHic && !noOther){
    		isNoHic = "1";
    	}
    	
		b = new SqlBuilder("JhyCpChemToQcmChemTask.setConsignQuote");
		b.Fill(new String[]{ checkNo, quoteState, isNoSsc, isNoHic });
		st.executeUpdate(b.toString());
		//首次报齐写入报齐时间
		if("2".equals(quoteState)){
			b = new SqlBuilder("JhyCpChemToQcmChemTask.setConsignQuoteDate");
			b.Fill(new String[]{ checkNo });
			st.executeUpdate(b.toString());
		}
	}
	
//	public JhyCpChemToQcmChemTask(Connection jhyCon, Connection qcmCon) {
//		Statement jhyStmt = null;
//		Statement qcmStmt = null;
//		try {
//			jhyCon.setAutoCommit(false);
//			qcmCon.setAutoCommit(false);
//			ArrayList<PipeCheckconsignDetailEntity> pipeDetails = getPipeCheckConsignDetail(jhyCon);
//			jhyStmt = jhyCon.createStatement();
//			qcmStmt = qcmCon.createStatement();
//			for (PipeCheckconsignDetailEntity pipeDetail : pipeDetails) {
//				ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetails = getPipeCheckConsignChemDetail(pipeDetail,
//						jhyCon);
//				QcmJhyElementsEntity qcmJhyElement = new QcmJhyElementsEntity();
//				CopyData(pipeDetail, pipeChemDetails, qcmJhyElement);
//				String sqlInsertQcmJhyElements = CoreXmlSqlParsersFactory
//						.getSql("JhyLgChemToQcmChemTask.insertQcmJhyElements");
//				sqlInsertQcmJhyElements = EntityTool.fillSqlByEntity(sqlInsertQcmJhyElements, qcmJhyElement);
//				qcmStmt.addBatch(sqlInsertQcmJhyElements);
//
//				String sqlUpdateFlag = CoreXmlSqlParsersFactory.getSql("JhyCpChemToQcmChemTask.getUpdateFlag");
//				sqlUpdateFlag = EntityTool.fillSqlByEntity(sqlUpdateFlag, pipeDetail);
//				jhyStmt.addBatch(sqlUpdateFlag);
//			}
//			qcmStmt.executeBatch();
//			jhyStmt.executeBatch();
//
//			jhyCon.commit();
//			qcmCon.commit();
//		} catch (Exception ex) {
//			MainTask.InsertException(ex.getMessage(), "JhyCpChemToQcmChemTask异常", qcmCon);
//			ex.printStackTrace();
//			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 ArrayList<PipeCheckconsignDetailEntity> getPipeCheckConsign(Connection con){
//		SqlBuilder b = new SqlBuilder("");
//	}

	private ArrayList<PipeCheckconsignDetailEntity> getPipeCheckConsignDetail(Connection con) {
		String sql = CoreXmlSqlParsersFactory.getSql("JhyCpChemToQcmChemTask.getPipeCheckConsignDetail");
		ArrayList<PipeCheckconsignDetailEntity> list = EntityTool.getEntitysBySqlNoClsCon(sql, con,
				PipeCheckconsignDetailEntity.class);
		return list;
	}

//	private ArrayList<PipeCheckconsignChemdetailEntity> getPipeCheckConsignChemDetail(
//			PipeCheckconsignDetailEntity pipeDetail, Connection con) {
//		String sql = CoreXmlSqlParsersFactory.getSql("JhyCpChemToQcmChemTask.getPipeCheckConsignChemDetail");
//		sql = EntityTool.fillSqlByEntity(sql, pipeDetail);
//		ArrayList<PipeCheckconsignChemdetailEntity> list = EntityTool.getEntitysBySqlNoClsCon(sql, con,
//				PipeCheckconsignChemdetailEntity.class);
//		return list;
//	}

	private void CopyData(PipeCheckconsignDetailEntity pipeDetail,
			ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetails, 
			QcmJhyElementsEntity qcmJhyElement, SqlBuilder b, Connection con) {
		Class<QcmJhyElementsEntity> qcmJhyElementsClass = QcmJhyElementsEntity.class;
		for (PipeCheckconsignChemdetailEntity pipeChemDetail : pipeChemDetails) {
			try {
				qcmJhyElement.setMatNo(pipeChemDetail.getMatNo());
				qcmJhyElement.setHeadMatNo(pipeChemDetail.getHeadMatNo());
				qcmJhyElement.setBarCode(pipeChemDetail.getBarCode());
				
				String jhyElementKey = pipeChemDetail.getChemName();
				if(jhyElementKey.equals("As")){
					jhyElementKey = "Asn";
				}

				String value = pipeChemDetail.getQuoteValue();
				Method method = qcmJhyElementsClass.getMethod("set" + jhyElementKey, String.class);
				method.invoke(qcmJhyElement, value);
			} catch (Exception ex) {

			}
		}
		
		//处理al元素
		if(qcmJhyElement.getAl().equals("") && !qcmJhyElement.getAlt().equals("")){
			qcmJhyElement.setAl(qcmJhyElement.getAlt());
		}
		if(!qcmJhyElement.getAl().equals("") && qcmJhyElement.getAlt().equals("")){
			qcmJhyElement.setAlt(qcmJhyElement.getAl());
		}

		String sampleNo = pipeChemDetails.get(0).getSampleNo().trim();
		qcmJhyElement.setAssayno(pipeDetail.getCheckNo() + "_" + sampleNo + "_C");
		qcmJhyElement.setAssaytype("B");// A表示熔炼成分
		qcmJhyElement.setCreateName(pipeDetail.getCheckName());
		qcmJhyElement.setJudgeStoveNo(pipeDetail.getJudgeStoveNo());// 判定炉号没有
		qcmJhyElement.setBatchNo(pipeChemDetails.get(0).getBatchNo());
		qcmJhyElement.setMachine(pipeDetail.getDeviceName());// 光谱仪类型没有
		qcmJhyElement.setMemo(pipeDetail.getRemark());
		qcmJhyElement.setPlineCode(pipeDetail.getPlineCode());
		qcmJhyElement.setSampleId(pipeDetail.getCheckNo());// 试验编号没有
		qcmJhyElement.setSampleNo(sampleNo);
		qcmJhyElement.setStoveNo(pipeDetail.getStoveNo());
		qcmJhyElement.setSampleType(pipeDetail.getCheckCount());
		qcmJhyElement.setCraftSource(pipeDetail.getCraftSource());
		qcmJhyElement.setXh("");// 无序号

		//处理复取样和生产补样。
		//处理成分复验
		QcmJhyElementsEntity qcmJhyChemOld = null;
		if(pipeDetail.getCheckNo().startsWith("F")){//复验
			b = new SqlBuilder("JhyCpChemToQcmChemTask.getFirstChemByBatch");
			b.Fill(new String[]{ pipeDetail.getJudgeStoveNo(), pipeChemDetails.get(0).getBatchNo() });
			qcmJhyChemOld = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, QcmJhyElementsEntity.class);
		}
		Method[] qcmJhyMethods = qcmJhyElementsClass.getMethods();
		for(Method getMethod : qcmJhyMethods){
			if(getMethod.getName().startsWith("get")){
				try {
					if(getMethod.invoke(qcmJhyElement).toString().equals("")){
						if(qcmJhyChemOld != null){
							Method oldMethod = qcmJhyElementsClass.getMethod(getMethod.getName(), new Class[0]);
							String oldValue = oldMethod.invoke(qcmJhyChemOld).toString();

							Method setMethod = qcmJhyElementsClass.getMethod(
									getMethod.getName().replaceAll("get", "set"),
									String.class);
							setMethod.invoke(qcmJhyElement, oldValue);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 添加之前的取样
	 */
	private ArrayList<PipeCheckconsignChemdetailEntity> getFullSample(
			String checkNo, Connection jhyCon, Connection qcmCon){
		SqlBuilder b = null;
		b = new SqlBuilder("JhyCpChemToQcmChemTask.getSampleByCheckNo");
		b.Fill(new String[]{ checkNo });
		ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetails = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), jhyCon, PipeCheckconsignChemdetailEntity.class);
		return pipeChemDetails;
//	    if(checkNo.startsWith("F")){ //把复样的值替换到初样里（超过上限取复样最大，低于下限取复样最小）
//			String chekNoA = "A" + checkNo.substring(1);
//			b = new SqlBuilder("JhyCpChemToQcmChemTask.getSampleByCheckNo");
//			b.Fill(new String[]{ chekNoA });
//			ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetailAs = EntityTool.getEntitysBySqlNoClsCon(
//					b.toString(), jhyCon, PipeCheckconsignChemdetailEntity.class);
//			b = new SqlBuilder("JhyCpChemToQcmChemTask.getunqualifiedSampleNo");
//			b.Fill(new String[]{ chekNoA });
//			ArrayList<QcmBcChemjudgeEntity> bcChemJudges = EntityTool.getEntitysBySqlNoClsCon(
//					b.toString(), qcmCon, QcmBcChemjudgeEntity.class);
//			for(QcmBcChemjudgeEntity bcChemJudge : bcChemJudges){ //定试样号
//				//获取不合的元素
//				b = new SqlBuilder("JhyCpChemToQcmChemTask.getChemJudgeResult");
//				b.Fill(new String[]{ chekNoA, bcChemJudge.getSampleNo() });
//				ArrayList<QcmBcChemResultEntity> bcChemResults = EntityTool.getEntitysBySqlNoClsCon(
//						b.toString(), qcmCon, QcmBcChemResultEntity.class);
//				for(QcmBcChemResultEntity bcChemResult : bcChemResults){
//					//查找复样与初样相对应的试样号
//					double quoteValue = 0.0;
//					String chemName = "";
//					for(int i = 0; i < pipeChemDetails.size(); i++){
//						PipeCheckconsignChemdetailEntity pipeChemDetailF = pipeChemDetails.get(i);
//						//if(pipeChemDetailF.getChemName().equals(chemName)){
//							if(pipeChemDetailF.getChemName().equals(bcChemResult.getChemName())){
//								double quoteValue2 = Double.parseDouble(pipeChemDetailF.getQuoteValue());
//								if(chemName.equals("")){
//									quoteValue = quoteValue2;
//								}else{
//									if(bcChemResult.getUnqualifiedType().equals("1")){
//										if(quoteValue2 < quoteValue){
//											quoteValue = quoteValue2;
//										}
//									}else if(bcChemResult.getUnqualifiedType().equals("2")){
//										if(quoteValue2 > quoteValue){
//											quoteValue = quoteValue2;
//										}
//									}
//								}
//								chemName = pipeChemDetailF.getChemName();
//							}
//						//}
//					}
//					
//					//找到对应的复样值后替换初样值。
//					if(!chemName.equals("")){
//						for(PipeCheckconsignChemdetailEntity pipeChemDetailA : pipeChemDetailAs){
//							if(pipeChemDetailA.getCheckNo().equals(bcChemJudge.getSampleId())
//									&& pipeChemDetailA.getSampleNo().equals(bcChemJudge.getSampleNo())
//									&& pipeChemDetailA.getChemName().equals(chemName)){
//								pipeChemDetailA.setQuoteValue(String.valueOf(quoteValue));
//								break;
//							}
//						}
//					}
//				}
//			}
//			return pipeChemDetailAs;
//		}else if(checkNo.startsWith("Z")){
//			b = new SqlBuilder("JhyCpChemToQcmChemTask.getSampleByCheckNo");
//			b.Fill(new String[]{ checkNo });
//			pipeChemDetails = EntityTool.getEntitysBySqlNoClsCon(
//					b.toString(), jhyCon, PipeCheckconsignChemdetailEntity.class);
//			HashMap<String, ArrayList<String>> sampleChemNames = new HashMap<String, ArrayList<String>>();
//			for(int i = 0; i < pipeChemDetails.size(); i++){
//				PipeCheckconsignChemdetailEntity pipeChemDetailF = pipeChemDetails.get(i);
//				if(sampleChemNames.containsKey(pipeChemDetailF.getSampleNo())){
//					sampleChemNames.get(pipeChemDetailF.getSampleNo()).add(pipeChemDetailF.getChemName());
//				}else{
//					ArrayList<String> list = new ArrayList<String>();
//					list.add(pipeChemDetailF.getChemName());
//					sampleChemNames.put(pipeChemDetailF.getSampleNo(), list);
//				}
//			}
//			
//			b = new SqlBuilder("JhyCpChemToQcmChemTask.getASample");
//			b.Fill(new String[]{ checkNo });
//			ArrayList<PipeCheckconsignChemdetailEntity> pipeChemDetailAs = EntityTool.getEntitysBySqlNoClsCon(
//					b.toString(), jhyCon, PipeCheckconsignChemdetailEntity.class);
//			for(String key : sampleChemNames.keySet()){
//				for(PipeCheckconsignChemdetailEntity pipeChemDetailA : pipeChemDetailAs){
//					boolean exists = false;
//					for(String chemName : sampleChemNames.get(key)){
//						if(pipeChemDetailA.getChemName().equals(chemName)){
//							exists = true;
//						}
//					}
//					if(!exists){
//						pipeChemDetailA.setCheckNo(checkNo);
//						pipeChemDetailA.setSampleNo(key);
//						pipeChemDetails.add(pipeChemDetailA);
//					}
//				}
//			}
//			
//			return pipeChemDetails;
//		}else{
//			return pipeChemDetails;
//		}
	}

	private String copyFdCheckConsign(QcmJhyElementsEntity qcmJhyElement, SqlBuilder b,
									Connection qcmCon, Statement qcmSt) throws SQLException {
		String checkNo = qcmJhyElement.getSampleId();
		String checkNoNew = "G" + qcmJhyElement.getSampleId();
		b = new SqlBuilder("JhyCpChemToQcmChemTask.getCheckConsignByCheckNo");
		b.Fill(new String[]{ checkNo, qcmJhyElement.getJudgeStoveNo(), qcmJhyElement.getBatchNo() });
		QcmZcCheckconsignEntity checkConsign = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), qcmCon, QcmZcCheckconsignEntity.class);
		b = new SqlBuilder("JhyCpChemToQcmChemTask.getChemCommon");
		b.Fill(new String[]{ checkConsign.getOrdLnDlyPk(), checkConsign.getCraftSeq() });
		CraftOrdDesignStdCicEntity craftCic = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), qcmCon, CraftOrdDesignStdCicEntity.class);
		if(craftCic == null || checkConsign.getCraftSource().equals("0")) return "";

		insertCheckConsign(checkConsign, checkNoNew, b, qcmSt);
		insertCheckConsignDetail(checkConsign, checkNo, checkNoNew, "HX", b, qcmCon, qcmSt);
		insertJhyChem(qcmJhyElement, checkNoNew, qcmSt);

		return checkNoNew;
	}

	public static void insertCheckConsign(QcmZcCheckconsignEntity checkConsign, String checkNoNew, SqlBuilder b,
									Statement qcmSt) throws SQLException {
		b = new SqlBuilder("JhyCpChemToQcmChemTask.deleteCheckConsignByCheckNo");
		b.Fill(new String[]{ checkNoNew, checkConsign.getJudgeStoveNo(), checkConsign.getBatchNo() });
		qcmSt.executeUpdate(b.toString());
		checkConsign.setCheckNo(checkNoNew);
		checkConsign.setCraftSource("0");
		String insert = CoreXmlSqlParsersFactory.getSql("JhyCpChemToQcmChemTask.insertCheckConsign");
		insert = EntityTool.fillSqlByEntity(insert, checkConsign);
		qcmSt.executeUpdate(insert);
	}

	public static void insertCheckConsignDetail(QcmZcCheckconsignEntity checkConsign, String checkNo, String checkNoNew,
		String phyCodeMin, SqlBuilder b, Connection qcmCon, Statement qcmSt) throws SQLException {
		b = new SqlBuilder("JhyCpChemToQcmChemTask.deleteConsignDetailByPhyCodeMax");
		b.Fill(new String[]{ checkNoNew, checkConsign.getBatchNo(), phyCodeMin });
		qcmSt.executeUpdate(b.toString());
		b = new SqlBuilder("JhyCpChemToQcmChemTask.getCheckConsignByPhyCodeMax");
		//System.out.println("数据1："+checkNo+", "+checkConsign.getBatchNo()+", "+phyCodeMin);
		b.Fill(new String[]{ checkNo, checkConsign.getBatchNo(), phyCodeMin });
		ArrayList<QcmZcCheckconsignDetailEntity> consignDetails = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), qcmCon, QcmZcCheckconsignDetailEntity.class);
		for(QcmZcCheckconsignDetailEntity consignDetail : consignDetails){
			//2024-12-31 注释  因为CRAFT_ORD_DESIGN_STD_PIC_SMP 找不到共样取样要求 
			//2025-1-8 取消注释
			if(!phyCodeMin.equals("HX") && !phyCodeMin.equals("ONH")){
				b = new SqlBuilder("JhyCpPhyToQcmPhyTask.getPhyCommonByPhy");
				b.Fill(new String[]{ checkConsign.getOrdLnDlyPk(), checkConsign.getCraftSeq(), phyCodeMin });
				//System.out.println("数据2："+checkConsign.getOrdLnDlyPk()+", "+ checkConsign.getCraftSeq()+", "+
				//phyCodeMin );
				CraftOrdDesignStdPicSmpEntity craftFdPhy = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), qcmCon, CraftOrdDesignStdPicSmpEntity.class);
				if(craftFdPhy == null){
					continue;
				}else{
					b = new SqlBuilder("JhyCpPhyToQcmPhyTask.getCraftPhyByPhy");
					b.Fill(new String[]{ checkConsign.getOrdLnDlyPk(), checkConsign.getCraftSeq(),
							checkConsign.getIndexSeq(), checkConsign.getProcessCode(), checkConsign.getPic(),
							phyCodeMin});
					CraftOrdDesignStdPicSmpEntity craftPhy = EntityTool.getEntityBySqlNoClsCon(
							b.toString(), qcmCon, CraftOrdDesignStdPicSmpEntity.class);
					if(craftPhy == null){
						continue;
					}
				}
			}
			consignDetail.setCheckNo(checkNoNew);
			String insert = CoreXmlSqlParsersFactory.getSql("JhyCpChemToQcmChemTask.insertConsignDetail");
			insert = EntityTool.fillSqlByEntity(insert, consignDetail);
			qcmSt.executeUpdate(insert);
		}
	}

	private static void insertJhyChem(QcmJhyElementsEntity qcmJhyElement, String checkNoNew,
									 Statement qcmSt) throws SQLException {
		qcmJhyElement.setSampleId(checkNoNew);
		qcmJhyElement.setAssayno(checkNoNew + "_" + qcmJhyElement.getSampleNo() + "_C");

		String sqlClear = CoreXmlSqlParsersFactory.getSql("JhyCpChemToQcmChemTask.clear");
		sqlClear = EntityTool.fillSqlByEntity(sqlClear, qcmJhyElement);
		qcmSt.executeUpdate(sqlClear);

		String sqlInsertQcmJhyElements = CoreXmlSqlParsersFactory
				.getSql("JhyLgChemToQcmChemTask.insertQcmJhyElements");
		sqlInsertQcmJhyElements = EntityTool.fillSqlByEntity(sqlInsertQcmJhyElements, qcmJhyElement);
		qcmSt.executeUpdate(sqlInsertQcmJhyElements);
	}
}
