package com.steering.pss.judge.Bll;

import CoreFS.SA06.CoreReturnObject;
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.comm.tools.StringTool;
import com.steering.pss.judge.comm.ChemJudgeHelper;
import com.steering.pss.judge.comm.JudgeHelper;
import com.steering.pss.judge.model.*;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscJgEntity;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class BllCreateZbsInfo extends DBCommon{
	private static String isRefreshZBS_JG = "0";// 刷新加工质保书

	public CoreReturnObject Create(String applyCode, String judgeStoveNo, String batchNo, String zbsFlag) {
		return Create(applyCode,judgeStoveNo,batchNo,zbsFlag,"0");
	}
	public CoreReturnObject Create(String applyCode, String judgeStoveNo, String batchNo, String zbsFlag,String flag){
		isRefreshZBS_JG = flag; // 1刷新加工性能
		Connection con = null;
		Statement st = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			
			//保存判定数据
			SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
            b.Fill(new String[]{ applyCode, judgeStoveNo, batchNo });
            QcmZgJugdeApplyEntity zgJudgeApply = EntityTool.getEntityBySqlNoClsCon(
            		b.toString(), con, QcmZgJugdeApplyEntity.class);

            if(zgJudgeApply.getOrdLnDlyPk().startsWith("ZZB")){
				if(zgJudgeApply.getJudgeResultCode().equals("")){
					throw new Exception("该记录未判定，不能刷新");
				}
			}else{
				if(zgJudgeApply.getJudgeTolresultCode().equals("")){
					throw new Exception("该记录成品未判定，不能刷新");
				}
			}
            if("1".equals(zgJudgeApply.getZbsLockFlag())){
				throw new Exception("该记录质保书锁定，不能刷新");
			}

            
            //刷新接箍料质保书
            b = new SqlBuilder("JdgMchTwistinglyResult.getResultDInfoByBatchNoHeatPlanNo");
    		b.Fill(new String[]{ zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo(), zgJudgeApply.getHeatPlanNo() });
    		ArrayList<MchTwistinglyResultDEntity> jgList = EntityTool.getEntitysBySqlNoClsCon(
    				b.toString(), con, MchTwistinglyResultDEntity.class);
    		for(MchTwistinglyResultDEntity jg : jgList) {
//    			b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNoNoLiku");
//    			b.Fill(new String[]{ jg.getCouplingStoveNo(), jg.getLotNo(), "H" });
				b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryBLByBatchNo");
				b.Fill(new String[]{ jg.getCouplingStoveNo(), jg.getLotNo()});
    			QcmZgJugdeApplyEntity jgApply = EntityTool.getEntityBySqlNoClsCon(
    					b.toString(), con, QcmZgJugdeApplyEntity.class);
    			if(jgApply == null) continue;
    			b = new SqlBuilder("JdgPlnZyJgWorkbill.getBlCode");
    			b.Fill(new String[] { jgApply.getProOrderId() });
    			PlnZyJgWorkbillEntity jgWorkbill = EntityTool.getEntityBySqlNoClsCon(
    					b.toString(), con, PlnZyJgWorkbillEntity.class);
//    			b = new SqlBuilder("JdgCraftOrdDesignMscPlPrc.getEndProcessByOrdLnPk");
//                b.Fill(new String[]{ jgWorkbill.getCodeJgBl(), zgJudgeApply.getCraftSeq(), zgJudgeApply.getIndexSeq() });
//                QcmZgJugdeApplyEntity endProcess = EntityTool.getEntityBySqlNoClsCon(
//                		b.toString(), con, QcmZgJugdeApplyEntity.class);
                b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryBLByBatchNo");
    			b.Fill(new String[]{ jg.getCouplingStoveNo(), jg.getLotNo() });
    			QcmZgJugdeApplyEntity blApply = EntityTool.getEntityBySqlNoClsCon(
    					b.toString(), con, QcmZgJugdeApplyEntity.class);
    			refreshZbsData(blApply, zbsFlag, "zc", new ArrayList<String>(), con, st);
    		}
    		//刷新短节料质保书
    		b = new SqlBuilder("JdgPlnZyJgxMC.queryByHeatPlanNoNew");
    		b.Fill(new String[]{ zgJudgeApply.getHeatPlanNo() });
    		ArrayList<QcmZgJugdeApplyEntity> djJudgeBatchNos = EntityTool.getEntitysBySqlNoClsCon(
    				b.toString(), con, QcmZgJugdeApplyEntity.class);
    		for(QcmZgJugdeApplyEntity djJudgeStoveNo : djJudgeBatchNos) {
                b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryBLByBatchNo");
                b.Fill(new String[]{ djJudgeStoveNo.getJudgeStoveNo(), djJudgeStoveNo.getBatchNo() });
    			QcmZgJugdeApplyEntity blApply = EntityTool.getEntityBySqlNoClsCon(
    					b.toString(), con, QcmZgJugdeApplyEntity.class);
    			if(blApply == null) continue;
    			refreshZbsData(blApply, zbsFlag, "zc", new ArrayList<String>(), con, st);
    		}
    		// 刷新备料质保书
			b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryBLByBatchNo");
			b.Fill(new String[]{ zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo() });
			QcmZgJugdeApplyEntity blApply = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZgJugdeApplyEntity.class);
			if(blApply != null){
				refreshZbsData(blApply, zbsFlag, "zc", new ArrayList<String>(), con, st);
			}
//			else{
//				b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryBLByBatchNo");
//				b.Fill(new String[]{ zgJudgeApply.getLoadJudgeStoveNo(), zgJudgeApply.getLoadBatchNo() });
//				blApply = EntityTool.getEntityBySqlNoClsCon(
//						b.toString(), con, QcmZgJugdeApplyEntity.class);
//				if(blApply != null){
//					refreshZbsData(blApply, zbsFlag, "zc", new ArrayList<String>(), con, st);
//				}
//			}

    		//刷新管体质保书
    		refreshZbsData(zgJudgeApply, zbsFlag, "zc", new ArrayList<String>(), con, st);

			con.commit();
			return cro;
		}catch(Exception ex){
			String str = _judgeStoveNo + "," + _batchNo + "," + _processCode;
//			ex.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cro.setV_errMsg("程序异常: " + ex.getMessage());
			return cro;
		}finally{
			if(st != null){
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public String refreshZbsData(QcmZgJugdeApplyEntity zgJudgeApply, String zbsFlag,
				String judgeFlag, ArrayList<String> checkDetailJsons,
				Connection con, Statement st) throws Exception{
		//保存判定数据
		SqlBuilder b = null;
        String ordLnPk = BllZcColligateJudge.getOrdLnPkByDlyPk(zgJudgeApply.getOrdLnDlyPk(), b, con);
        HashMap<String, QcmZgJugdeApplyEntity> applyMap = BllZcColligateJudge.getAllJudgeApply(
				zgJudgeApply.getJugdeApplyCode(), zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo(),
				zgJudgeApply.getProcessCode(), zgJudgeApply.getUpperHeatplanId(), null, null, con);

        //************************成品判定*********************
        QcmZcJudgeEntity zcJudge = BllZcColligateJudge.Judge(judgeFlag, zgJudgeApply, ordLnPk, applyMap,
				zbsFlag, checkDetailJsons, con);
        zcJudge.setMemo("");
        zcJudge.setCreateName("SYSTEM");
        zcJudge.setStoveNo(zgJudgeApply.getStoveNo());
        zcJudge.setJudgeStoveNo(zgJudgeApply.getJudgeStoveNo());
        zcJudge.setBatchNo(zgJudgeApply.getBatchNo());
        zcJudge.setProcessCdoe(zgJudgeApply.getProcessCode());
        zcJudge.setPlineCode(zgJudgeApply.getPlineCode());
        zcJudge.setDeliveryStateCode(zgJudgeApply.getDeliveryStateCode());
        zcJudge.setDeliveryStateName(zgJudgeApply.getDeliveryStateName());
        zcJudge.setJugdeApplyCode(zgJudgeApply.getJugdeApplyCode());
        zcJudge.setActCount(zgJudgeApply.getActCount());
        zcJudge.setActWeight(zgJudgeApply.getActWeight());
        zcJudge.setActTheoryWeight(zgJudgeApply.getActTheoryWeight());
        zcJudge.setOrdLnDlyPk(zgJudgeApply.getOrdLnDlyPk());
    	//新增前作废有效的综合判定记录
    	b = new SqlBuilder("JdgQcmZcJudge.updateValid");
    	b.Fill(new String[]{ zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo(), zgJudgeApply.getJugdeApplyCode(), "0" });
    	st.executeUpdate(b.toString());
    	//新增判定记录
    	BllZcColligateJudge.saveJudge(zcJudge, "refresh", b, con, st);
		Create(zgJudgeApply.getJugdeApplyCode(), zgJudgeApply.getJudgeStoveNo(), zgJudgeApply.getBatchNo(), zbsFlag, "",
				false, "", zgJudgeApply.getOrdLnDlyPk(), con, st);

		return zcJudge.getJudgeResult();
	}

	private static String _judgeStoveNo = "";
	private static String _batchNo = "";
	private static String _processCode = "";

	public void Create(String applyCode, String judgeStoveNo, String batchNo, String zbsFlag,
					   String oldJudgeTolResult, boolean spotInfo, String groupNo,
					   String zcOrdLnDlyPk, Connection con, Statement st) throws Exception{
		//*********（1）基础数据准备
		//是否强制老系统
		String zbsFlagNew=zbsFlag;
		//本工序送判单
		SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
		b.Fill(new String[]{ applyCode, judgeStoveNo, batchNo });
		QcmZgJugdeApplyEntity curApply = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmZgJugdeApplyEntity.class);
		_judgeStoveNo = judgeStoveNo;
		_batchNo = batchNo;
		_processCode = curApply.getProcessCode();
		b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryGroupInfoByBatchNo");
		b.Fill(new String[]{ applyCode, judgeStoveNo, batchNo });
		QcmZgJugdeApplyEntity zgJudgeApplyGroup = EntityTool.getEntityBySqlNoClsCon(
		 	b.toString(), con, QcmZgJugdeApplyEntity.class);
		curApply.setActCount(zgJudgeApplyGroup.getActCount());
		curApply.setActWeight(zgJudgeApplyGroup.getActWeight());
		curApply.setActTheoryWeight(zgJudgeApplyGroup.getActTheoryWeight());
        //成品综合判定表
		b = new SqlBuilder("JdgQcmZcJudge.getByBatchNo");
		b.Fill(new String[]{ judgeStoveNo, batchNo, curApply.getProcessCode(), applyCode });
		QcmZcJudgeEntity zcJudge = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, QcmZcJudgeEntity.class);
		if(zcJudge == null){
			throw new Exception("成品综合未判定，不能刷新！");
		}

		if(curApply.getOrdLnDlyPk()==null || "".equals(curApply.getOrdLnDlyPk()))
		{
			curApply.setZbsFlag("3");
			updateZbsFlagNoPk(curApply, curApply.getBatchGroudNo(), b, st);
			return;
		}

		//001版工艺主表
		String craftOrdLnPk;
		if(spotInfo){
			craftOrdLnPk = BllZcColligateJudge.getOrdLnPkByDlyPk(curApply.getOrdLnDlyPk(), b, con);
		}else{
			craftOrdLnPk = BllZcColligateJudge.getOrdLnPkByDlyPk(zcOrdLnDlyPk, b, con);
		}
		b = new SqlBuilder("JdgCraftOrdDesign.queryCreateTimeByOrdLnPk");
		b.Fill(new String[]{ craftOrdLnPk });
		CraftOrdDesignEntity craft001 = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignEntity.class);
		b = new SqlBuilder("JdgCraftOrdDesign.getNewByOrdLnPk");
		b.Fill(new String[]{ craftOrdLnPk });
		CraftOrdDesignEntity craftNew = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignEntity.class);

		b = new SqlBuilder("JdgCraftOrdDesignMscJg.getByOrdlnPk");
		b.Fill(new String[]{ craftOrdLnPk, curApply.getCraftSeq() });
		CraftOrdDesignMscJgEntity craftJg = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignMscJgEntity.class);
		//从轧管开始所有工序送判单集合
		//ArrayList<QcmZgJugdeApplyEntity> applys = getAllProcessApply(curApply, b, con);
		HashMap<String, QcmZgJugdeApplyEntity> applyMap = BllZcColligateJudge.getAllJudgeApply(
				curApply.getJugdeApplyCode(), judgeStoveNo, batchNo, curApply.getProcessCode(),
				curApply.getUpperHeatplanId(), null, null, con);



		//*********（2）删掉旧质保书数据
		QcmZbsInfoEntity zbsInfoOld = deleteZbsInfo(curApply, zcOrdLnDlyPk, b, con, st);

		//*********（3）验证是否从产销打印质保书
		//如果是缺项判定则跳过， 缺项判定已经给出打印方式
		CheckZbsDataComplete(curApply, craftOrdLnPk, applyMap, zbsFlag, b, con);
		if(!oldJudgeTolResult.equals("40740709") && !zbsFlag.equals("")){
			updateZbsFlag(curApply, curApply.getBatchGroudNo(), zcOrdLnDlyPk, applyMap, b, st);
		}
		//如果老系统打印就不保存质保书数据
		if(curApply.getZbsFlag().equals("2")) {
			if("2".equals(zbsFlagNew))
				return;
			else
				throw new Exception("未成功生成质保书，请检查项目是否完整！");
		}
		if(curApply.getZbsFlag().equals("3")) {
				return;
		}

		//*********（4）保存质保书主表
		QcmZbsInfoEntity zbsInfo = setZbsInfo(curApply, zcOrdLnDlyPk, zbsInfoOld, zcJudge, applyMap, b, con, st);

		//*********（6）保存管体质保书ID与接箍质保书ID的关系
		//获取品名基础表
		b = new SqlBuilder("JdgComBaseProduct.getByPk");
		b.Fill(new String[]{ curApply.getProduccode() });
		ComBaseProductEntity product = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComBaseProductEntity.class);
		String producType = product.getProducType();
		if(producType.equals("B") || producType.equals("C") || producType.equals("D") || producType.equals("E")
			|| product.getProduccode().equals("S000046")){
			setZbsInfoJg(curApply, zbsInfo, b, con, st);
		}
		if(producType.equals("C") || producType.equals("E")){
			setZbsInfoDj(curApply, zbsInfo, b, con, st);
		}

		//获取工艺信息
		ArrayList<CraftOrdDesignMscPlPrcEntity> craftProcesses = BllZcColligateJudge.getCraftProcesses(
				craftOrdLnPk, curApply.getCraftSeq(), curApply.getIndexSeq(), b, con);

//		Set<String> ordLnPks = new TreeSet<String>();
//		ordLnPks.add(craftOrdLnPk);
//		for (Map.Entry<String, QcmZgJugdeApplyEntity> applyEntityEntry : applyMap.entrySet()) {
//			TreeSet<String> crafts = new TreeSet<>(); // 工艺工序代码
//			for (CraftOrdDesignMscPlPrcEntity craftProcess : craftProcesses) {
//				crafts.add(craftProcess.getProcessCode());
//			}
//			if(crafts.contains(applyEntityEntry.getKey())){
//				continue;
//			}
//			QcmZgJugdeApplyEntity qcmZgJugdeApplyEntity = applyMap.get(applyEntityEntry.getKey());
//			// 查询备料ORD_LN_PK
//			String ordLnPk = BllZcColligateJudge.getOrdLnPkByDlyPk(qcmZgJugdeApplyEntity.getOrdLnDlyPk(), b, con);
//			if(ordLnPks.contains(ordLnPk) || StringUtils.isEmpty(ordLnPk)){
//				continue;
//			}
//			ordLnPks.add(ordLnPk);
////			if("F".equals(qcmZgJugdeApplyEntity.getProcessCode())) {
////				Create(curApply.getJugdeApplyCode(), curApply.getJudgeStoveNo(), curApply.getBatchNo(), zbsFlag, "",
////						false, "", qcmZgJugdeApplyEntity.getOrdLnDlyPk(), con, st);
////			}
//			craftProcesses.addAll(BllZcColligateJudge.getCraftProcesses(
//					ordLnPk, qcmZgJugdeApplyEntity.getCraftSeq(), qcmZgJugdeApplyEntity.getIndexSeq(), b, con));
//		}
		//循环制程工序
		for(CraftOrdDesignMscPlPrcEntity craftProcess : craftProcesses){
			QcmZgJugdeApplyEntity apply;
			if(craftProcess.getProcessCode().equals("A")){
				apply = applyMap.get("D");
			}else{
				apply = applyMap.get(craftProcess.getProcessCode());
			}
			if(apply == null) continue;

			JudgeCraftParam cp = BllZcColligateJudge.getCraftParm(curApply, apply, b, con);
			//*********（7）保存质保书成分表
			setZbsChem(curApply, cp.getOrdLnPk(), curApply.getCraftSeq(), craftProcess.getProcessCode(),
					zbsInfo, craft001, b, con, st);

			//*********（8）保存质保书理化表
			ArrayList<QcmZbsPhyResultEntity> phyResults = setZbsPhy(curApply, cp.getOrdLnPk(), curApply.getCraftSeq(),
					craftProcess.getProcessCode(), zbsInfo, craft001, b, con, st);

			//*********(9)保存探伤表
			setZbsDetect(curApply, cp.getOrdLnPk(), craftProcess.getProcessCode(), zbsInfo, craftNew, b, con, st);

			//*********（10）保存质保书工艺表
			setZbsCraft(apply, cp.getOrdLnPk(), cp.getCraftSeq(), zbsInfo, phyResults, b, con, st);
		}

		if(spotInfo){
			//更新材料表的质保书ID
			setMatZcMZbsId(judgeStoveNo, batchNo, zcOrdLnDlyPk, zbsInfo.getZbsId(),
					b, con, st);
		}else{
			//更新材料表的质保书ID
			if(curApply.getIsAllScrap().equals("4")){
				setMatZcMZbsId(judgeStoveNo, batchNo, zcOrdLnDlyPk,
						zbsInfo.getZbsId(), b, con, st);
			}else{
				setMatBcMZbsId(curApply, zbsInfo.getZbsId(), b, con, st);
			}
		}

		//更新接箍材料表
		setCouplingMatMZbsId(zbsInfo.getZbsId(), curApply, b, st);
		
		//更新探伤数据（临时补数据用）
//		b = new SqlBuilder("JdgQcmZbsInfo.temp");
//		ArrayList<QcmZgJugdeApplyEntity> applys = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, QcmZgJugdeApplyEntity.class);
//		for(QcmZgJugdeApplyEntity apply : applys){
//			//获取性能点工序
//	        b = new SqlBuilder("CraftOrdDesignMscPlSti.getPhyProcess");
//	        b.Fill(new String[]{ zcOrdLnDlyPk, curApply.getCraftSeq(), curApply.getIndexSeq() });
//	        CraftOrdDesignMscPlStiEntity tmpSti = EntityTool.getEntityBySqlNoClsCon(
//	        		b.toString(), con, CraftOrdDesignMscPlStiEntity.class);
//			HashMap<String, QcmZgJugdeApplyEntity> tmpApplyMap = BllZcColligateJudge.getAllJudgeApply(
//					apply.getJudgeStoveNo(), apply.getBatchNo(), apply.getProcessCode(), tmpSti.getProcessCode(), null, con);
//			QcmZbsInfoEntity tmpZbs = new QcmZbsInfoEntity();
//			tmpZbs.setZbsId(apply.getZbsId());
//			b = new SqlBuilder("JdgQcmZbsDetectResult.delete");
//			b.Fill(new String[]{ apply.getZbsId() });
//			st.executeUpdate(b.toString());
//			setZbsDetect(apply, apply.getOrdLnDlyPk(), tmpApplyMap, tmpZbs, b, con, st);
//		}
	}
	
	private static QcmZbsInfoEntity deleteZbsInfo(QcmZgJugdeApplyEntity curApply, String ordLnDlyPk,
			SqlBuilder b, Connection con, Statement st) throws SQLException{
		//赋值质保书主表
		b = new SqlBuilder("JdgQcmZbsInfo.getByBatchNoOrdLnDlyPk");
		String woId = "";
		if(curApply.getProcessCode().equals("H")){
			woId = curApply.getProOrderId();
		}
		b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), ordLnDlyPk, woId });
		QcmZbsInfoEntity zbsInfo = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmZbsInfoEntity.class);
		if(zbsInfo != null){
			b = new SqlBuilder("JdgQcmZbsInfo.delete");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
			b = new SqlBuilder("JdgQcmZbsInfoJg.deleteByZbsId");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
			b = new SqlBuilder("JdgQcmZbsInfoDj.deleteByZbsId");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
			b = new SqlBuilder("JdgQcmZbsChemResult.delete");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
			b = new SqlBuilder("JdgQcmZbsPhyResult.delete");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
			b = new SqlBuilder("JdgQcmZbsDetectResult.delete");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
			b = new SqlBuilder("JdgQcmZbsCraftResult.delete");
			b.Fill(new String[]{ zbsInfo.getZbsId() });
			st.executeUpdate(b.toString());
		}
		
		return zbsInfo;
	}
	
	private static QcmZbsInfoEntity setZbsInfo(QcmZgJugdeApplyEntity curApply, String ordLnDlyPk,
			QcmZbsInfoEntity zbsInfoOld, QcmZcJudgeEntity zcJudge, 
			HashMap<String, QcmZgJugdeApplyEntity> applyMap,
			SqlBuilder b, Connection con, Statement st) throws SQLException{
		//获取合同信息
		b = new SqlBuilder("JdgSlmOrderHead.getByDlyPk");
		b.Fill(new String[]{ ordLnDlyPk });
		SlmOrderHeadEntity head = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, SlmOrderHeadEntity.class);
		if(ordLnDlyPk.startsWith("ZZB")) {
			head = new SlmOrderHeadEntity();
		}
		b = new SqlBuilder("JdgSlmOrderLine.getByDlyPk");
		b.Fill(new String[]{ ordLnDlyPk });
		SlmOrderLineEntity line = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, SlmOrderLineEntity.class);
		if(ordLnDlyPk.startsWith("ZZB")) {
			line = new SlmOrderLineEntity();
		}
		b = new SqlBuilder("JdgSlmOrderLineDelivery.getByDlyPk");
		b.Fill(new String[]{ ordLnDlyPk });
		SlmOrderLineDeliveryEntity delivery = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, SlmOrderLineDeliveryEntity.class);
		if(ordLnDlyPk.startsWith("ZZB")) {
			delivery = new SlmOrderLineDeliveryEntity();
		}
		b = new SqlBuilder("JdgSlmOrderAddAsk.getZbsDesc");
		b.Fill(new String[]{ line.getAddAskNo() });
		SlmOrderAddAskEntity addAsk = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, SlmOrderAddAskEntity.class);
		if(ordLnDlyPk.startsWith("ZZB")) {
			addAsk = new SlmOrderAddAskEntity();
		}
		
		String suppCode="";
		QcmZgJugdeApplyEntity applyQuery = applyMap.get("D");
		if(applyQuery!=null){
			b = new SqlBuilder("JdgQcmZbsInfo.getLgSupp");
			b.Fill(new String[]{ applyQuery.getJudgeStoveNo() });
			QcmZbsInfoEntity supp = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZbsInfoEntity.class);
			if(supp!=null)
				suppCode=supp.getSuppCode();
		}
		
		//计算分切数
		int cutNum = 0;
		String lotNoGroup = "";
		for(String key : applyMap.keySet()){
			QcmZgJugdeApplyEntity apply = applyMap.get(key);
			if(apply.getTarget().equals("A") || apply.getTarget().equals("B")){
				String process = apply.getLikuProcessCode().equals("") ? apply.getProcessCode() : apply.getLikuProcessCode();
				int tmpCutNum = 0;

				if(process.equals("D")){
					b = new SqlBuilder("JdgMilPlan.getById");
					b.Fill(new String[]{ apply.getHeatPlanNo() });
					MilPlanEntity milPlan = EntityTool.getEntityBySqlNoClsCon(
							b.toString(), con, MilPlanEntity.class);
					if(milPlan != null){
						tmpCutNum = Integer.parseInt(milPlan.getSawNum());
					}
				}else if(process.equals("E")){
					b = new SqlBuilder("JdgPlnZyDbkM.queryByPk");
					b.Fill(new String[]{ apply.getHeatPlanNo() });
					PlnZyDbkMEntity plnDbk = EntityTool.getEntityBySqlNoClsCon(
							b.toString(), con, PlnZyDbkMEntity.class);
					tmpCutNum = Integer.parseInt(plnDbk.getOutnumCut());
				}else if(process.equals("F")){
					lotNoGroup = apply.getLotNoGroup();
					b = new SqlBuilder("JdgPlnZyRclM.queryByPk");
					b.Fill(new String[]{ apply.getHeatPlanNo() });
					PlnZyRclMEntity plnRcl = EntityTool.getEntityBySqlNoClsCon(
							b.toString(), con, PlnZyRclMEntity.class);
					tmpCutNum = Integer.parseInt(plnRcl.getOutnumCut());
				}else if(process.equals("G")){
					b = new SqlBuilder("JdgPlnZyJgxM.queryByPk");
					b.Fill(new String[]{ apply.getHeatPlanNo() });
					PlnZyJgxMEntity plnJgx = EntityTool.getEntityBySqlNoClsCon(
							b.toString(), con, PlnZyJgxMEntity.class);
					if(plnJgx != null){
						tmpCutNum = Integer.parseInt(plnJgx.getOutnumCut());
					}
				}
				
				if(cutNum == 0 && tmpCutNum > 0){
					cutNum = tmpCutNum;
				}else{
					if(cutNum > 0 && tmpCutNum > 0){
						cutNum = cutNum * tmpCutNum;
					}
				}
			}
		}
		
		//保存质保书主表
		QcmZbsInfoEntity zbsInfo = new QcmZbsInfoEntity();
		if(zbsInfoOld == null){
			b = new SqlBuilder("JdgQcmZbsInfo.getMaxZbsId");
			QcmZbsInfoEntity maxZbsId = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZbsInfoEntity.class);
			zbsInfo.setZbsId(maxZbsId.getZbsId());
		}else{
			zbsInfo.setZbsId(zbsInfoOld.getZbsId());
		}
		zbsInfo.setStoveNo(zcJudge.getStoveNo());
		zbsInfo.setJudgeStoveNo(zcJudge.getJudgeStoveNo());
		zbsInfo.setBatchNo(zcJudge.getBatchNo());
		zbsInfo.setPlineCode(zcJudge.getPlineCode());
		zbsInfo.setProcessCode(zcJudge.getProcessCdoe());
		zbsInfo.setProcessDesc(zcJudge.getProcessDesc());
		zbsInfo.setChemResult(zcJudge.getChemResult());
		zbsInfo.setPhyResult(zcJudge.getPhyResult());
		zbsInfo.setFaceResult(zcJudge.getFaceResult());
		zbsInfo.setSpecResult(zcJudge.getSpecResult());
		zbsInfo.setDetectResult(zcJudge.getDetectResult());
		zbsInfo.setDetectResult2(zcJudge.getDetectResult2());
		zbsInfo.setWaterResult(zcJudge.getWaterResult());
		zbsInfo.setProcessResult(zcJudge.getProcessResult());
		zbsInfo.setJudgeResult(zcJudge.getJudgeResult());
		zbsInfo.setMemo(zcJudge.getMemo());
		zbsInfo.setCreateName(zcJudge.getCreateName());
		zbsInfo.setCreateTime(zcJudge.getCreateTime());
		zbsInfo.setDeliveryStateCode(zcJudge.getDeliveryStateCode());
		zbsInfo.setDeliveryStateName(zcJudge.getDeliveryStateName());
		zbsInfo.setActCount(curApply.getActCount());
		zbsInfo.setActWeight(curApply.getActWeight());
		zbsInfo.setActTheoryWeight(curApply.getActTheoryWeight());
		zbsInfo.setSteelcode(curApply.getSteelcode());
		zbsInfo.setSteelname(curApply.getSteelname());
		zbsInfo.setGradecode(curApply.getGradecode());
		zbsInfo.setGradename(curApply.getGradename());
		zbsInfo.setCutNum(String.valueOf(cutNum));
		zbsInfo.setLotNo(curApply.getLotNo());
		zbsInfo.setLotNoGroup(lotNoGroup);
		zbsInfo.setCustomerNo(head.getCustomerNo());
		zbsInfo.setCustomerNm(head.getCustomerNm());
		zbsInfo.setReceivNo(delivery.getReceivNo());
		zbsInfo.setReceivNm(delivery.getReceivNm());
		zbsInfo.setOrderNo(head.getOrderNo());
		zbsInfo.setOrderSeq(line.getOrderSeq());
		zbsInfo.setStdCode(line.getStdCode());
		zbsInfo.setStdName(line.getStdName());
		zbsInfo.setStdStyle(line.getStdStyle());
		zbsInfo.setStdStyleDesc(line.getStdStyleDesc());
		zbsInfo.setProduccode(line.getProduccode());
		zbsInfo.setProducname(line.getProducname());
		zbsInfo.setOrderSpecCode(line.getOrderSpecCode());
		zbsInfo.setOrderSpecDesc(line.getOrderSpecDesc());
		zbsInfo.setModelCode(line.getModelCode());
		zbsInfo.setModelDesc(line.getModelDesc());
		zbsInfo.setOutputStdNo(line.getOutputStdNo());
		zbsInfo.setLenNo(line.getLenNo());
		zbsInfo.setLenDesc(line.getLenDesc());
		zbsInfo.setUseCode(line.getUseCode());
		zbsInfo.setUseDesc(line.getUseDesc());
		zbsInfo.setCustmOrderNo(head.getCustmOrderNo());
		zbsInfo.setOrdLnDlyPk(ordLnDlyPk);
		zbsInfo.setOrdLnPk(line.getOrdLnPk());
		zbsInfo.setSpecCode(line.getSpecCode());
		zbsInfo.setSpecName(line.getSpecName());
		zbsInfo.setLenUnit(line.getLenUnit());
		zbsInfo.setLenMin(line.getLenMin());
		zbsInfo.setLenMax(line.getLenMax());
		String lenMin = line.getLenMin().equals("") ? "" : String.valueOf(Double.parseDouble(line.getLenMin()));
		String lenMax = line.getLenMax().equals("") ? "" : String.valueOf(Double.parseDouble(line.getLenMax()));
		zbsInfo.setLenMinMaxUnit(lenMin + "-" + lenMax + line.getLenUnit());
		zbsInfo.setSuppCode(suppCode);
		if(curApply.getProcessCode().equals("H")){
			zbsInfo.setWoId(curApply.getProOrderId());
		}
		if(addAsk != null){
			zbsInfo.setZbsAddAskDesc(addAsk.getAskSubItmeValDesc());
		}
		
		String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsInfo.insert");
		insert = EntityTool.fillSqlByEntity(insert, zbsInfo);
		st.executeUpdate(insert);
		
		return zbsInfo;
	}

	private static ArrayList<MchTwistinglyResultDEntity> setZbsInfoJg(QcmZgJugdeApplyEntity curApply,
			QcmZbsInfoEntity zbsInfo, SqlBuilder b, Connection con, Statement st) throws Exception{
		b = new SqlBuilder("JdgMchTwistinglyResult.getResultDInfoByBatchNoHeatPlanNo");
		b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), curApply.getHeatPlanNo() });
		ArrayList<MchTwistinglyResultDEntity> jgList = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, MchTwistinglyResultDEntity.class);
//		if(craftJg != null && jgList.size() == 0){
//			throw new Exception ("该炉号需要配箍，但没有找到拧接信息！");
//		}
		for(MchTwistinglyResultDEntity jg : jgList){
			b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNoNoLiku");
			b.Fill(new String[]{ jg.getCouplingStoveNo(), jg.getLotNo(), "H" });
			QcmZgJugdeApplyEntity jgApply = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZgJugdeApplyEntity.class);
			if(jgApply == null || jgApply.getJudgeResultCode().equals("")){
				//throw new Exception ("接箍炉号:" + jg.getCouplingStoveNo() + " 批号:" + jg.getLotNo() + "还未判定，请先进行接箍判定！");
			}else if(!jgApply.getJudgeResultCode().equals("40740701")){
				//throw new Exception ("接箍炉号:" + jg.getCouplingStoveNo() + " 批号:" + jg.getLotNo() + "不是判定合格状态！");
			}
			
			b = new SqlBuilder("JdgQcmZbsInfo.getBlByBatchNo");
			b.Fill(new String[]{ jg.getCouplingStoveNo(), jg.getLotNo() });
			QcmZbsInfoEntity jgZbs = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZbsInfoEntity.class);
			QcmZbsInfoJgEntity zbsInfoJg = new QcmZbsInfoJgEntity();
			zbsInfoJg.setZbsId(zbsInfo.getZbsId());
			
			if(jgZbs == null || jgZbs.getZbsId().equals("")){
				continue;
				//throw new Exception ("接箍炉号:" + jg.getCouplingStoveNo() + " 批号:" + jg.getLotNo() + "还未判定，请先进行接箍判定！");
			}
			jg.setZbsId(jgZbs.getZbsId());
			zbsInfoJg.setJgZbsId(jgZbs.getZbsId());
			zbsInfoJg.setJudgeStoveNo(zbsInfo.getJudgeStoveNo());
			zbsInfoJg.setJgJudgeStoveNo(jgZbs.getJudgeStoveNo());
			String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsInfoJg.insert");
			insert = EntityTool.fillSqlByEntity(insert, zbsInfoJg);
			st.executeUpdate(insert);
		}

		b = new SqlBuilder("JdgQcmZbsInfo.getBlByBatchNo");
		b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo() });
		QcmZbsInfoEntity jgZbs = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmZbsInfoEntity.class);

//		b = new SqlBuilder("JdgQcmZbsInfo.getBlByBatchNo");
//		b.Fill(new String[]{ curApply.getLoadJudgeStoveNo(), curApply.getLoadBatchNo() });
//		jgZbs = EntityTool.getEntityBySqlNoClsCon(
//				b.toString(), con, QcmZbsInfoEntity.class);

		QcmZbsInfoJgEntity zbsInfoJgBl = new QcmZbsInfoJgEntity();
		zbsInfoJgBl.setZbsId(zbsInfo.getZbsId());
		if(jgZbs != null && !jgZbs.getZbsId().equals("")){
			zbsInfoJgBl.setJgZbsId(jgZbs.getZbsId());
//			zbsInfoJgBl.setJgJudgeStoveNo(jgZbs.getJudgeStoveNo());
//			zbsInfoJgBl.setJudgeStoveNo(zbsInfo.getJudgeStoveNo());
			String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsInfoJg.insert");
			insert = EntityTool.fillSqlByEntity(insert, zbsInfoJgBl);
			st.executeUpdate(insert);

			MchTwistinglyResultDEntity twistResult = new MchTwistinglyResultDEntity();
			twistResult.setZbsId(jgZbs.getZbsId());
			twistResult.setCouplingNum(curApply.getActCount());

			boolean exist = false;
			for(MchTwistinglyResultDEntity tmpTwist : jgList){
				if(tmpTwist.getZbsId().equals(twistResult.getZbsId())){
					exist = true;
				}
			}
			if(!exist){
				jgList.add(twistResult);
			}
		}
		
		return jgList;
	}
	
	private static void setZbsInfoDj(QcmZgJugdeApplyEntity curApply, QcmZbsInfoEntity zbsInfo,
			SqlBuilder b, Connection con, Statement st) throws Exception{
		b = new SqlBuilder("JdgPlnZyJgxMC.queryByHeatPlanNoNew");
		b.Fill(new String[]{ curApply.getHeatPlanNo() });
		ArrayList<QcmZgJugdeApplyEntity> djJudgeBatchNos = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, QcmZgJugdeApplyEntity.class);
		// 多个炉号组成的短节
		for(QcmZgJugdeApplyEntity djJudgeBatchNo : djJudgeBatchNos){
			b = new SqlBuilder("JdgQcmZbsInfo.getBlByBatchNo");
			b.Fill(new String[]{ djJudgeBatchNo.getJudgeStoveNo(), djJudgeBatchNo.getBatchNo() });
			QcmZbsInfoEntity djZbs = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZbsInfoEntity.class);
			QcmZbsInfoDjEntity zbsInfoDj = new QcmZbsInfoDjEntity();
			zbsInfoDj.setZbsId(zbsInfo.getZbsId());
			
			if(djZbs == null || djZbs.getZbsId().equals("")){
				//continue;
				//throw new Exception ("短节炉号:" + djJudgeBatchNo.getJudgeStoveNo() + " 批号:" + djJudgeBatchNo.getBatchNo() + "还未判定，请先进行判定！");
			}
			zbsInfoDj.setDjZbsId(djZbs.getZbsId());
			String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsInfoDj.insert");
			insert = EntityTool.fillSqlByEntity(insert, zbsInfoDj);
			st.executeUpdate(insert);
		}

		//普通单炉号短节
		b = new SqlBuilder("JdgQcmZbsInfo.getBlByBatchNo");
		b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo() });
		QcmZbsInfoEntity djZbs = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmZbsInfoEntity.class);
		QcmZbsInfoDjEntity zbsInfoDj = new QcmZbsInfoDjEntity();
		zbsInfoDj.setZbsId(zbsInfo.getZbsId());
		if(djZbs != null && !djZbs.getZbsId().equals("")){
			zbsInfoDj.setDjZbsId(djZbs.getZbsId());
			String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsInfoDj.insert");
			insert = EntityTool.fillSqlByEntity(insert, zbsInfoDj);
			st.executeUpdate(insert);
		}
	}
	
	private static void setZbsChem(QcmZgJugdeApplyEntity curApply, String craftOrdLnPk, String craftSeq,
			String processCode, QcmZbsInfoEntity zbsInfo, CraftOrdDesignEntity craft001,
			SqlBuilder b, Connection con, Statement st) throws Exception{
		//成分圆整基础数据
		List<ComBaseChemEntity> baseChems = ChemJudgeHelper.getElementRounds();//圆整位数
		HashMap<String, String> rounds = new HashMap<String, String>();
		for(ComBaseChemEntity baseChem : baseChems) {
			rounds.put(baseChem.getChemName(), baseChem.getRoundingDigits());
		}
		
		b = new SqlBuilder("JdgQcmZcChemColresult.getZbsByBatchNoApplyCode");
		b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), curApply.getJugdeApplyCode(), processCode });
		ArrayList<QcmZbsChemResultEntity> parms = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, QcmZbsChemResultEntity.class);


//		if(product.getProducType().equals("D")){
//			if(jgBlZbs != null){
//				b = new SqlBuilder("JdgQcmZbsChemResult.getByZbsId");
//				b.Fill(new String[]{ jgBlZbs.getZbsId() });
//				parms = EntityTool.getEntitysBySqlNoClsCon(
//						b.toString(), con, QcmZbsChemResultEntity.class);
//			}
//		}
		
		HashMap<String, Integer> mapRound = new HashMap<String, Integer>();
		for(QcmZbsChemResultEntity parm : parms){
			if(!parm.getProcessCode().equals("A")){ 
				int round = getChemRound(parm, rounds);
				if(!mapRound.containsKey(parm.getChemName())){
					mapRound.put(parm.getChemName(), round);
				}
			}
		}
		for(QcmZbsChemResultEntity parm : parms){
			if(parm.getProcessCode().equals("A")){
				int round = getChemRound(parm, rounds);
				if(!mapRound.containsKey(parm.getChemName())){
					mapRound.put(parm.getChemName(), round);
				}
			}
		}
		//2020-07-23 增加氧氮氢相关，熔炼判定取内控，成品判定取交付 
		//工艺成分
		ArrayList<CraftOrdDesignStdCicEntity> jfChems=new ArrayList<CraftOrdDesignStdCicEntity>();
		if("A".equals(processCode)){
			b = new SqlBuilder("CraftOrdDesignStdCic.getCraftJfChemByOrdLnPkMaking");
			b.Fill(new String[]{ craftOrdLnPk, craftSeq,curApply.getCic() });
			jfChems = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdCicEntity.class);
		}else{
			b = new SqlBuilder("CraftOrdDesignStdCic.getCraftJfChemByOrdLnPk");
			b.Fill(new String[]{ craftOrdLnPk, craftSeq });
			jfChems = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdCicEntity.class);
		}
		
		
		//保存
		List<String> alSampleNos = new ArrayList<String>();
		for(QcmZbsChemResultEntity parm : parms){
			if(parm.getProcessCode().equals("A")){
				parm.setChemType("0");
			}else{
				parm.setChemType("1");
			}
			String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsChemResult.insert");
			parm.setZbsId(zbsInfo.getZbsId());
			parm.setRound(String.valueOf(mapRound.get(parm.getChemName())));
			
			//特殊处理Al和Alt元素
			if((parm.getChemName().equals("Al") || parm.getChemName().equals("Alt"))){
				if(alSampleNos.contains(parm.getSampleNo())){
					continue;
				}else{
					if(parm.getChemName().equals("Alt")){
						parm.setChemName("Al");
					}
					alSampleNos.add(parm.getSampleNo());
				}
			}
			
			//处理交付标准是否报出（质保书的显示依据交付标准的是否报出）
			Date craftDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(craft001.getCreateTime());
			Date setDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-20 00:00:00");
			//第一版工艺生成时间大于设定时间的统一使用交付的是否报出
			if(craftDate.after(setDate)){
				boolean quote = false;
				for(CraftOrdDesignStdCicEntity jfChem : jfChems){
					if(jfChem.getChemName().equals("Alt")){
						jfChem.setChemName("Al");
					}
					if(jfChem.getChemName().equals(parm.getChemName())){
						
						//验证限制元素
						String express = "";
						String value = "";
						if (!jfChem.getEleMinSign().equals("")) {
							express = jfChem.getEleMinSign() + jfChem.getEleMin();
							for(QcmZbsChemResultEntity parm2 : parms){
								if(parm2.getSampleNo().equals(parm.getSampleNo()) && parm2.getChemName().equals(jfChem.getEleName())){
									value = parm2.getChemValue();
									break;
								}
							}
							if(!Boolean.parseBoolean(JudgeHelper.eval(value + express))){
								continue;
							}
						}
						if (!jfChem.getEleMaxSign().equals("")) {
							express = jfChem.getEleMaxSign() + jfChem.getEleMax();
							for(QcmZbsChemResultEntity parm2 : parms){
								if(parm2.getSampleNo().equals(parm.getSampleNo()) && parm2.getChemName().equals(jfChem.getEleName())){
									value = parm2.getChemValue();
									break;
								}
							}
							if(!Boolean.parseBoolean(JudgeHelper.eval(value + express))){
								continue;
							}
						}
						
						if(jfChem.getIsquote().equals("1")){
							quote = true;
						}
					}
				}
				if(!quote){
					continue;
				}
			}
			
			insert = EntityTool.fillSqlByEntity(insert, parm);
			st.executeUpdate(insert);
		}
	}
	
	private static int getChemRound(QcmZbsChemResultEntity parm, HashMap<String, String> rounds){
		int round = 0;
		if(parm.getJfMin().equals("") && parm.getJfMax().equals("") && parm.getNkMin().equals("")
				&& parm.getNkMax().equals("")){
			round = Integer.parseInt(rounds.get(parm.getChemName()));
		}else{
			String[] jfMinValues = parm.getJfMin().replaceAll("<", "").replaceAll("=", "").replaceAll(">", "").split("\\.");
			int jfMinRound = 0;
			if(jfMinValues.length > 1){
				jfMinRound = jfMinValues[1].length();
			}
			String[] jfMaxValues = parm.getJfMax().replaceAll("<", "").replaceAll("=", "").replaceAll(">", "").split("\\.");
			int jfMaxRound = 0;
			if(jfMaxValues.length > 1){
				jfMaxRound = jfMaxValues[1].length();
			}
			String[] nkMinValues = parm.getNkMin().replaceAll("<", "").replaceAll("=", "").replaceAll(">", "").split("\\.");
			int nkMinRound = 0;
			if(nkMinValues.length > 1){
				nkMinRound = nkMinValues[1].length();
			}
			String[] nkMaxValues = parm.getNkMax().replaceAll("<", "").replaceAll("=", "").replaceAll(">", "").split("\\.");
			int nkMaxRound = 0;
			if(nkMaxValues.length > 1){
				nkMaxRound = nkMaxValues[1].length();
			}
			
			ArrayList<Integer> stdRounds = new ArrayList<Integer>();
			stdRounds.add(jfMinRound);
			stdRounds.add(jfMaxRound);
			stdRounds.add(nkMinRound);
			stdRounds.add(nkMaxRound);
			if(Collections.max(stdRounds) == 0){
				String strRound = rounds.get(parm.getChemName());
				if(strRound != null && StringTool.isNumeric(strRound))
				round = Integer.parseInt(strRound);
			}else{
				round = Collections.max(stdRounds);
			}
		}
		
		return round;
	}

	public static int zbsSeq = 0;
	private static ArrayList<QcmZbsPhyResultEntity> setZbsPhy(QcmZgJugdeApplyEntity curApply, String ordLnPk,
			String craftSeq, String processCode, QcmZbsInfoEntity zbsInfo, CraftOrdDesignEntity craft001,
			SqlBuilder b, Connection con, Statement st) throws Exception{
		//理化取样信息
		b = new SqlBuilder("JdgQcmZcPhyColresult.getZbsByBatchNoApplyCode");
		b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), curApply.getJugdeApplyCode(), processCode });
		ArrayList<QcmZbsPhyResultEntity> parms = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, QcmZbsPhyResultEntity.class);

		b = new SqlBuilder("CraftOrdDesignStdPic.getCraftJfPhyByOrdLnPk");
		b.Fill(new String[]{ ordLnPk, craftSeq, "" });
		ArrayList<CraftOrdDesignStdPicEntity> jfPhys = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignStdPicEntity.class);
		
		//保存
	    Connection jhyCon = new DBCommon().getDao("JhyDao").getConnection();
	    try{
	    	for(QcmZbsPhyResultEntity parm : parms){
	    		//暂时屏蔽加工工序的性能
	    		if(parm.getProcessCode().equals("G") && "0".equals(isRefreshZBS_JG)){
	    			continue;
	    		}
	    		if(parm.getPhyCode().equals("P0612")){
	    			String a="1";
	    			a="";
	    		}
	    		zbsSeq++;
				//获取126试验方法
				b = new SqlBuilder("JdgPipeCheckConsignDetail.getFormualNameByPhyMin");
				b.Fill(new String[]{ parm.getSampleId(), parm.getBatchNo(), parm.getPhyCodeMin(),
						parm.getSampleNo() });
				PipeCheckconsignDetailEntity detail = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), jhyCon, PipeCheckconsignDetailEntity.class);
				if(detail != null) {
				    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    String strCheckTime;
				    if (detail.getCheckTime() == null || detail.getCheckTime().trim().isEmpty()) {
				        strCheckTime = ""; 
				    } else {
			            Date checkTime = sdf.parse(detail.getCheckTime());
			            strCheckTime = sdf.format(checkTime);
				    }
				    String strAuditTime;
				    if (detail.getAuditTime() == null || detail.getAuditTime().trim().isEmpty()) {
				        strAuditTime = "";
				    } else {
			            Date auditTime = sdf.parse(detail.getAuditTime());
			            strAuditTime = sdf.format(auditTime);
				    }
				    // 设置到parm对象中
				    parm.setFormualNo(detail.getFormualNo());
				    parm.setFormualName(detail.getFormualName());
				    parm.setSizeDesc(detail.getSizeDesc());
				    parm.setDeviceNo(detail.getDeviceNo());
				    parm.setDeviceName(detail.getDeviceName());
				    parm.setCheckName(detail.getCheckName());
				    parm.setCheckTime(strCheckTime); // 使用处理后的时间
				    parm.setAuditName(detail.getAuditName());
				    parm.setAuditTime(strAuditTime); // 使用处理后的时间
				    parm.setRemark(detail.getRemark());
				}
				//获取公英制
				b = new SqlBuilder("QcmJhyPhyresultDAL.getZbsBySampleNoPhy");
				b.Fill(new String[]{ parm.getSampleId(), parm.getJudgeStoveNo(), parm.getBatchNo(),
					parm.getPhyCode() });
				QcmJhyPhyresultEntity jhyPhy = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmJhyPhyresultEntity.class);
				parm.setValueKsi(jhyPhy.getValueKsi());
				parm.setValuePsi(jhyPhy.getValuePsi());
				parm.setValueF2(jhyPhy.getValueF2());
				parm.setValueF3(jhyPhy.getValueF3());
				parm.setAverageValueF(jhyPhy.getAverageValueF());
				String isRcl = jhyPhy.getIsrcl().equals("0") ? "0" : "1";
				parm.setIsrcl(isRcl);
				//处理交付标准是否报出（质保书的显示依据交付标准的是否报出）
				Date craftDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(craft001.getCreateTime());
				Date setDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-20 00:00:00");
				//第一版工艺生成时间大于设定时间的统一使用交付的是否报出
				if(craftDate.after(setDate)){
					boolean quote = false;
					for(CraftOrdDesignStdPicEntity jfPhy : jfPhys){
						//冲击试验只用检验项和方向匹配交付标准 （内控有尺寸递减、温度递减）
						if(jfPhy.getPhyCodeMax().equals("R0002")){
							if(jfPhy.getPhyCode().equals(parm.getPhyCode()) && (jfPhy.getItemCodeF().equals(parm.getItemCodeF()))
									|| jfPhy.getItemCodeF().equals("")){
								if(jfPhy.getIsquote().equals("1")){
									quote = true;
								}
							}
						}else{
							if(jfPhy.getPhyCode().equals(parm.getPhyCode())
									&& (jfPhy.getItemCodeF().equals("") || jfPhy.getItemCodeF().equals(parm.getItemCodeF()))
									&& (jfPhy.getItemCodeS().equals("") || jfPhy.getItemCodeS().equals(parm.getItemCodeS()))
									&& (jfPhy.getItemCodeW().equals("") || jfPhy.getItemCodeW().equals(parm.getItemCodeW()))){
								if(jfPhy.getIsquote().equals("1")){
									quote = true;
								}
							}
						}
					}
					if(!quote){
						continue;
					}
				}
				parm.setZbsId(zbsInfo.getZbsId());
				String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsPhyResult.insert");
				insert = EntityTool.fillSqlByEntity(insert, parm);
				System.out.println(insert);
				st.executeUpdate(insert);
			}
	    }catch(Exception ex){
	    	System.out.println(ex.getMessage());
	    }finally{
	    	jhyCon.close();
	    }
	    
	    return parms;
	}
	
	private static void setZbsDetect(QcmZgJugdeApplyEntity curApply, String ordLnPk,
			String procesCode, QcmZbsInfoEntity zbsInfo, CraftOrdDesignEntity craftNew,
			SqlBuilder b, Connection con, Statement st) throws Exception{
		//验证制程
		CraftOrdDesignEntity craftPlNew = null;
		if(craftNew != null){
			b = new SqlBuilder("JdgCraftOrdDesignMscPl.getWholeBacklog");
			b.Fill(new String[]{ ordLnPk, craftNew.getCraftSeq(), curApply.getIndexSeq() });
			craftPlNew = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignEntity.class);
		}
		
		//保存
		b = new SqlBuilder("CraftOrdDesignStdDic.getByOrdLnPkNoDic");
		ArrayList<QcmZbsDetectResultEntity> stdDics = null;
		if(craftPlNew == null){
			b.Fill(new String[]{ ordLnPk, curApply.getCraftSeq(), curApply.getIndexSeq(), procesCode });
			stdDics = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, QcmZbsDetectResultEntity.class);
		}else{
			b.Fill(new String[]{ ordLnPk, craftNew.getCraftSeq(), curApply.getIndexSeq(), procesCode });
			stdDics = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, QcmZbsDetectResultEntity.class);
		}

		for(QcmZbsDetectResultEntity stdDic : stdDics){
			stdDic.setZbsId(zbsInfo.getZbsId());
			String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsDetectResult.insert");
			insert = EntityTool.fillSqlByEntity(insert, stdDic);
			st.executeUpdate(insert);
		}
	}
	
	private static void setZbsCraft(QcmZgJugdeApplyEntity apply, String ordLnPk, String craftSeq,
			QcmZbsInfoEntity zbsInfo, ArrayList<QcmZbsPhyResultEntity> phyResults,
			SqlBuilder b, Connection con, Statement st) throws SQLException{
		//交付水压
		b = new SqlBuilder("CraftOrdDesignStdWic.getJfByOrdLnPk");
		b.Fill(new String[]{ ordLnPk, craftSeq });
		CraftOrdDesignStdWicEntity jfWater = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignStdWicEntity.class);
		
		//保存
		boolean existNoJld = false;//是否存在非晶粒度试样组
		for(QcmZbsPhyResultEntity phyResult : phyResults){
			if(!phyResult.getProcessCode().equals(apply.getProcessCode())) continue;
			if(!phyResult.getPhyCodeMin().equals("C0012") && !phyResult.getPhyCodeMin().equals("C0104")){
				if(phyResult.getIsrcl().equals("1")){
					existNoJld = true;
					break;
				}
			}
		}
		//热处理制度
		b = new SqlBuilder("JdgQcmPhyRclInfo.getByBatchNo");
		b.Fill(new String[]{ apply.getJudgeStoveNo(), apply.getBatchNo(), apply.getProcessCode(),
				apply.getOrdLnDlyPk() });
		QcmPhyRclInfoEntity rclInfo = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmPhyRclInfoEntity.class);
		if(rclInfo != null){
			//如果只有晶粒度试样组则屏蔽小样热处理制度
			if(!existNoJld){
				rclInfo.setRclInfo("");
			}
		}else{
			//如果小样热处理制度为空，则显示正常热处理制度 (高压锅炉管、高压化肥管、核电管、石油裂化管)
			if(apply.getProduccode().equals("S000026") || apply.getProduccode().equals("S000113")
					|| apply.getProduccode().equals("S000114") || apply.getProduccode().equals("S000052")){
				rclInfo = new QcmPhyRclInfoEntity();
				rclInfo.setRclInfo(apply.getHttTechnology());
			}
		}

		//轧管出成品的高压锅炉管、石油裂化管、高压化肥管保存终轧温度
		if(apply.getProcessCode().equals("D")){
			if(apply.getProduccode().equals("S000026") || apply.getProduccode().equals("S000113") || apply.getProduccode().equals("S000052")){
				//轧管出成品的高压锅炉管、石油裂化管、高压化肥管保存终轧温度
			}else{
				apply.setExitTemp("");
			}
		}else{
			apply.setExitTemp("");
		}

		//处理水压值
		if(jfWater != null && jfWater.getOutputStyle().equals("410701")){
			apply.setTestPressure("");
		}else if(jfWater != null && jfWater.getOutputStyle().equals("410702")){
			apply.setTestPressureY("");
		}

		//处理探伤
		if(apply.getTestingType().equals("")){
			b = new SqlBuilder("JdgHttCrackDetectionEt.getZbsDetect", apply.getJudgeStoveNo(),
					apply.getBatchNo(), apply.getProcessCode());
			HttCrackDetectionEtEntity detecInfo = EntityTool.getEntityBySqlNoClsCon(b.toString(),
					con, HttCrackDetectionEtEntity.class);
			if(detecInfo != null){
				apply.setTestingGrade(detecInfo.getTestingGrade());
				apply.setTestingType(detecInfo.getTestingType());
			}
		}

		// 加工通径
		if(apply.getDriftModelLen().equals("") && apply.getProcessCode().equals("G")){
			b = new SqlBuilder("JdgMchPathResult.getInfo", apply.getJudgeStoveNo(),apply.getBatchNo());
			MchPathResultEntity mchPathResult = EntityTool.getEntityBySqlNoClsCon(b.toString(),
					con, MchPathResultEntity.class);
			if(mchPathResult != null){
				apply.setDriftModelLen(mchPathResult.getDriftModelLen());
				apply.setDriftModelMin2(mchPathResult.getDriftModelMin());
			}
		}

		String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsCraftResult.insert");
		insert = insert.replaceAll("#ZBS_ID#", zbsInfo.getZbsId());
		insert = EntityTool.fillSqlByEntity(insert, apply);

//			MchPathResultEntity pathResult = null;
//			MchPathResultDEntity pathResultD = null;
//			if(apply.getProcessCode().equals("G")){
//				b = new SqlBuilder("JdgMchPathResult.getZbsByBatchNo");
//				b.Fill(new String[]{ apply.getJudgeStoveNo(), apply.getBatchNo() });
//				pathResult = EntityTool.getEntityBySqlNoClsCon(
//						b.toString(), con, MchPathResultEntity.class);
//				if(pathResult != null){
//					b = new SqlBuilder("JdgMchPathResultD.getZbsByBatchNo");
//					b.Fill(new String[]{ pathResult.getResultNo() });
//					pathResultD = EntityTool.getEntityBySqlNoClsCon(
//							b.toString(), con, MchPathResultDEntity.class);
//				}
//			}
//			if(pathResult == null) pathResult = new MchPathResultEntity();
//			if(pathResultD == null) pathResultD = new MchPathResultDEntity();
//			insert = EntityTool.fillSqlByEntity(insert, pathResult);
//			insert = EntityTool.fillSqlByEntity(insert, pathResultD);

		if(rclInfo != null){
			insert = EntityTool.fillSqlByEntity(insert, rclInfo);
		}else{
			insert = EntityTool.fillSqlByEntity(insert, new QcmPhyRclInfoEntity());
		}

		st.executeUpdate(insert);
	}
	
	public static void CheckZbsDataComplete(QcmZgJugdeApplyEntity curApply, String craftOrdLnPk,
			HashMap<String, QcmZgJugdeApplyEntity> applyMap, String zbsFlag, SqlBuilder b, 
			Connection con) throws Exception{
		if(curApply.getZbsFlag().equals("2")){
			if("2".equals(zbsFlag))
				return;
			else
				throw new Exception("未成功生成质保书，请检查项目是否完整！");
		}
		if(curApply.getProcessCode().equals("D") || curApply.getProcessCode().equals("E")){
			zbsFlag = "1";
		}else{
			//获取工艺信息
			b = new SqlBuilder("JdgCraftOrdDesignMscPlPrc.getPhyProcessByOrdLnPk");
			b.Fill(new String[]{ craftOrdLnPk, curApply.getCraftSeq(), curApply.getIndexSeq() });
			ArrayList<CraftOrdDesignMscPlPrcEntity> craftPrcs = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignMscPlPrcEntity.class);
			
			//String zbsFlag = "1"; // 1产销打印  2老系统打印
			//验证各工序是否有理化判定结果
			//没有则通过老系统出质保书
			for(CraftOrdDesignMscPlPrcEntity craftPrc : craftPrcs){
				if(craftPrc.getProcessCode().equals("A") || craftPrc.getProcessCode().equals("B")
						|| craftPrc.getProcessCode().equals("C")) continue;
				//直缝焊套管 新系统出加工信息质保书， 性能结果在外购的质保书中
				if(curApply.getProduccode().equals("S000103") || curApply.getProduccode().equals("S000125")){
					continue;
				}
				boolean exists = false;
				for(String key : applyMap.keySet()){
					QcmZgJugdeApplyEntity apply = applyMap.get(key);
					if(apply.getProcessCode().equals(craftPrc.getProcessCode())){
						exists = true;
						//是否有判定记录
//						b = new SqlBuilder("JdgQcmBcJudge.getByBatchNo");
//						b.Fill(new String[]{ apply.getJugdeApplyCode(), apply.getJudgeStoveNo(),
//								apply.getBatchNo(), apply.getProcessCode() });
//						QcmBcJudgeEntity bcJudge = EntityTool.getEntityBySqlNoClsCon(
//								b.toString(), con, QcmBcJudgeEntity.class);
						b = new SqlBuilder("JdgQcmZcCheckconsign.queryByBatchNoNoCheckNo",
								new Object[]{apply.getJudgeStoveNo(), apply.getBatchNo(),
										apply.getProcessCode(), new String[]{"0", "1"}} );
						QcmZcCheckconsignEntity consing = EntityTool.getEntityBySqlNoClsCon(
								b.toString(), con, QcmZcCheckconsignEntity.class);
						if(consing==null){
							b = new SqlBuilder("JdgQcmZcCheckconsign.queryByLotNoNoCheckNo",
									new Object[]{apply.getLotNo(), apply.getLotNoGroup(),
											apply.getProcessCode(), new String[]{"0", "1"},apply.getPlineCode(),apply.getOrdLnDlyPk()} );
							 consing = EntityTool.getEntityBySqlNoClsCon(
									b.toString(), con, QcmZcCheckconsignEntity.class);
						}
						if(consing==null){
							// 查询循环值
							int loopValue = -1;
							PreparedStatement ps = null;
							ResultSet rs = null;
							String sqlPic = CoreXmlSqlParsersFactory
									.getSql("StoveHelper.querySqlPic");
							ps = con.prepareStatement(sqlPic);
							ps.setObject(1, craftPrc.getDesginKey());
							ps.setObject(2, curApply.getIndexSeq());
							ps.setObject(3, craftPrc.getProcessCode());
							ps.setObject(4, curApply.getPic());
							rs = ps.executeQuery();
							if (rs.next()) {
								loopValue = rs.getInt("LOOP_VALUE");
							}
							rs.close();
							ps.close();
							if(loopValue == 0){
								String ordLnPk = "";
								if (apply.getOrdLnDlyPk() != null && !"".equals(apply.getOrdLnDlyPk())) {
									ordLnPk = getOrdLnPkByDlyPk(apply.getOrdLnDlyPk(), b, con);
								}
								b = new SqlBuilder("JdgQcmZcCheckconsign.queryByLotNoNoCheckNo",
										new Object[]{"", "",
												apply.getProcessCode(), new String[]{"0", "1"},apply.getPlineCode(),ordLnPk} );
								consing = EntityTool.getEntityBySqlNoClsCon(
										b.toString(), con, QcmZcCheckconsignEntity.class);
							}
						}
						if(consing == null && !curApply.getOrdLnDlyPk().startsWith("ZZB")){
							zbsFlag = "2";
						}
					}
				}
				if(!exists){
					zbsFlag = "2";
				}
			}

			//更新质保书打印标志
			if(zbsFlag.equals("1")){
				b = new SqlBuilder("QcmZgJugdeApplyDAL.getZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode() });
				QcmZgJugdeApplyEntity applyZgsFlag = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmZgJugdeApplyEntity.class);
				if(applyZgsFlag != null){
					zbsFlag = "2";
				}
			}
		}
		
		curApply.setZbsFlag(zbsFlag);
	}

	public static String getOrdLnPkByDlyPk(String ordLnDlyPk, SqlBuilder b, Connection con) {
		if (ordLnDlyPk.startsWith("ZZB")) {
			b = new SqlBuilder("JdgPlnSaleOrd.getOrdLnPkByDlyPk");
		} else {
			b = new SqlBuilder("JdgSlmOrderLineDelivery.getOrdLnPkByDlyPk");
		}
		b.Fill(new String[]{ordLnDlyPk});
		PlnSaleordEntity plnSaleord = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, PlnSaleordEntity.class);

		return plnSaleord.getOrdLnPk();
	}
	
	private static void updateZbsFlag(QcmZgJugdeApplyEntity curApply, String groupNo,
			String ordLnDlyPk, HashMap<String, QcmZgJugdeApplyEntity> applyMap,
			SqlBuilder b, Statement st) throws SQLException{
		if(curApply.getProcessCode().equals("D") || curApply.getProcessCode().equals("E")){
			if(curApply.getOrdLnDlyPk().equals(ordLnDlyPk) && !curApply.getIsAllScrap().equals("4")){
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatBcM.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatBcBackup.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}else{
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatZcM.setZbsFlag");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), groupNo, 
						ordLnDlyPk, curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}
		}else{
			if(curApply.getOrdLnDlyPk().equals(ordLnDlyPk) && !curApply.getIsAllScrap().equals("4")){
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatBcM.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}else{
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatZcM.setZbsFlag");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), groupNo, 
						ordLnDlyPk, curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}
		}
	}
	
	private static void updateZbsFlagNoPk(QcmZgJugdeApplyEntity curApply, String groupNo,
			SqlBuilder b, Statement st) throws SQLException{
		if(curApply.getProcessCode().equals("D") || curApply.getProcessCode().equals("E")){
			if (!curApply.getIsAllScrap().equals("4")){
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatBcM.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatBcBackup.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}else{
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatZcM.setZbsFlagNoPk");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), groupNo, curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}
		}else{
			if( !curApply.getIsAllScrap().equals("4")){
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatBcM.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}else{
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setZbsFlagByJudgeStoveNo");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getProcessCode(), curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("JdgMatZcM.setZbsFlagNoPk");
				b.Fill(new String[]{ curApply.getJudgeStoveNo(), curApply.getBatchNo(), groupNo, curApply.getZbsFlag() });
				st.executeUpdate(b.toString());
			}
		}
	}
	
	private static void setMatBcMZbsId(QcmZgJugdeApplyEntity curApply, String zbsId,
			 SqlBuilder b, Connection con, Statement st) throws SQLException{
		b = new SqlBuilder("JdgMatBcM.setZbsId");
		b.Fill(new String[]{ curApply.getJugdeApplyCode(), curApply.getJudgeStoveNo(),
				curApply.getBatchNo(), zbsId });
		st.executeUpdate(b.toString());
	}
	
	private static void setMatZcMZbsId(String judgeStoveNo, String batchNo, String ordLnDlyPk,
			String zbsId, SqlBuilder b, Connection con, Statement st) throws SQLException{
		b = new SqlBuilder("JdgMatZcM.setZbsId");
		b.Fill(new String[]{ judgeStoveNo, batchNo, ordLnDlyPk, zbsId, });
		st.executeUpdate(b.toString());
	}

	private static void setCouplingMatMZbsId(String zbsId, QcmZgJugdeApplyEntity curApply, SqlBuilder b, Statement st) throws SQLException {
		String setZbs = CoreXmlSqlParsersFactory.getSql("JdgCouplingMatM.setZbsId");
		CouplingMatMEntity couplingMatM = new CouplingMatMEntity();
		couplingMatM.setZbsFlag(curApply.getZbsFlag());
		couplingMatM.setZbsId(zbsId);
		couplingMatM.setJudgeStoveNo(curApply.getJudgeStoveNo());
		couplingMatM.setBatchNo(curApply.getBatchNo());
		couplingMatM.setDeterNo(curApply.getJugdeApplyCode());
		couplingMatM.setWoId(curApply.getProOrderId());
		setZbs = EntityTool.fillSqlByEntity(setZbs, couplingMatM);
		st.executeUpdate(setZbs);
	}
	
	public static void delete(String applyCode, String judgeStoveNo, String batchNo,
			Connection con, Statement st) throws Exception{
		SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
		b.Fill(new String[]{ applyCode, judgeStoveNo, batchNo });
		QcmZgJugdeApplyEntity curApply = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmZgJugdeApplyEntity.class);
		//赋值质保书主表
		b = new SqlBuilder("JdgQcmZbsInfo.getByBatchNoOrdLnDlyPk");
		String woId = "";
		if(curApply.getProcessCode().equals("H")){
			woId = curApply.getProOrderId();
		}
		b.Fill(new String[]{ judgeStoveNo, batchNo, curApply.getOrdLnDlyPk(), woId });
		QcmZbsInfoEntity zbsInfo = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmZbsInfoEntity.class);
		if(zbsInfo == null){
			return;
		}

		b = new SqlBuilder("JdgQcmZbsInfo.delete");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());
		b = new SqlBuilder("JdgQcmZbsInfoJg.deleteByZbsId");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());
		b = new SqlBuilder("JdgQcmZbsInfoDj.deleteByZbsId");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());
		b = new SqlBuilder("JdgQcmZbsChemResult.delete");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());
		b = new SqlBuilder("JdgQcmZbsPhyResult.delete");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());
		b = new SqlBuilder("JdgQcmZbsDetectResult.delete");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());
		b = new SqlBuilder("JdgQcmZbsCraftResult.delete");
		b.Fill(new String[]{ zbsInfo.getZbsId() });
		st.executeUpdate(b.toString());

		return;
	}
	
	public CoreReturnObject modifyZbsInfo(ArrayList<String> jsons){
		Connection con = null;
		Statement st = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			for(String json : jsons){
				QcmZbsInfoEntity info = EntityTool.getEntityByGson(json, QcmZbsInfoEntity.class);
				String sql = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsInfo.update2");
				sql = EntityTool.fillSqlByEntity(sql, info);
				st.executeUpdate(sql);
			}
			
			con.commit();
			return cro;
		}catch(Exception ex){
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public CoreReturnObject modifyZbsCraft(ArrayList<String> jsons){
		Connection con = null;
		Statement st = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			for(String json : jsons){
				QcmZbsCraftResultEntity craftReult = EntityTool.getEntityByGson(json, QcmZbsCraftResultEntity.class);
				String sql = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsCraftResult.update");
				sql = EntityTool.fillSqlByEntity(sql, craftReult);
				st.executeUpdate(sql);
			}
			
			con.commit();
			return cro;
		}catch(Exception ex){
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public CoreReturnObject modifyZbsDetect(ArrayList<String> jsons){
		Connection con = null;
		Statement st = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			for(String json : jsons){
				QcmZbsDetectResultEntity craftReult = EntityTool.getEntityByGson(json, QcmZbsDetectResultEntity.class);
				String sql = CoreXmlSqlParsersFactory.getSql("JdgQcmZbsDetectResult.update");
				sql = EntityTool.fillSqlByEntity(sql, craftReult);
				st.executeUpdate(sql);
			}
			
			con.commit();
			return cro;
		}catch(Exception ex){
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public CoreReturnObject Test(String judgeStoveNo, String batchNo, String groupNo,
			String processCode, String ordLnDlyPk){
		CoreReturnObject cro = new CoreReturnObject();
		Connection con = null;
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			saveSpotInfo(judgeStoveNo, batchNo, groupNo, processCode, ordLnDlyPk, con);
			
			con.commit();
			return cro;
		}catch(Exception ex){
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public CoreReturnObject saveSpotInfo(String judgeStoveNo, String batchNo, String groupNo,
			String processCode, String ordLnDlyPk){
		CoreReturnObject cro = new CoreReturnObject();
		Connection con = null;
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			saveSpotInfo(judgeStoveNo, batchNo, groupNo, processCode, ordLnDlyPk, con);
			
			con.commit();
			return cro;
		}catch(Exception ex){
			try {
				con.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 现货利库
	 * @param judgeStoveNo
	 * @param batchNo
	 * @param groupNo
	 * @param processCode
	 * @param ordLnDlyPk
	 * @param con
	 * @throws Exception
	 */
	public static void saveSpotInfo(String judgeStoveNo, String batchNo, String groupNo,
			String processCode, String ordLnDlyPk, Connection con) throws Exception{
		Statement st = null;
		try{
			st = con.createStatement();
			//*********（1）基础数据准备
			//本工序送判单
			SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNoNoLiku");
			b.Fill(new String[]{ judgeStoveNo, batchNo, processCode });
			QcmZgJugdeApplyEntity curApply = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, QcmZgJugdeApplyEntity.class);
			if(curApply == null){
				b = new SqlBuilder("JdgMatZcM.setZbsFlag");
				b.Fill(new String[]{ judgeStoveNo, batchNo, groupNo, ordLnDlyPk, "2" });
				st.executeUpdate(b.toString());
				return;
			}
			_judgeStoveNo = judgeStoveNo;
			_batchNo = batchNo;
			_processCode = curApply.getProcessCode();
			b = new SqlBuilder("JdgMatZcM.getCountWeightByBatchNo");
			b.Fill(new String[]{ judgeStoveNo, batchNo,groupNo, ordLnDlyPk });
			MatZcMEntity zcMat = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, MatZcMEntity.class);
			curApply.setActCount(zcMat.getActCount());
			curApply.setActWeight(zcMat.getActWeight());
			curApply.setActTheoryWeight(zcMat.getActTheoryWeight());
	        //成品综合判定表
			b = new SqlBuilder("JdgQcmZcJudge.getByBatchNo");
			b.Fill(new String[]{ judgeStoveNo, batchNo, curApply.getProcessCode(), curApply.getJugdeApplyCode() });
			QcmZcJudgeEntity zcJudge = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, QcmZcJudgeEntity.class);
			if(zcJudge == null){
				throw new Exception("成品综合未判定！");
			}

			new BllCreateZbsInfo().Create(zcJudge.getJugdeApplyCode(), zcJudge.getJudgeStoveNo(), zcJudge.getBatchNo(),
					"", "", true, groupNo, ordLnDlyPk, con, st);
		}finally{
			if(st != null){
				st.close();
			}
		}
	}

	public CoreReturnObject UpdateZbsLock(String applyCode,String judgeStoveNo, String batchNo, String zbsLockFlag){
		CoreReturnObject cro = new CoreReturnObject();
		Connection con = null;
		Statement st = null;
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			st = con.createStatement();
			SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.UpdateZbsLock");
			b.Fill(new String[]{ applyCode, judgeStoveNo, batchNo, zbsLockFlag});
			st.executeUpdate(b.toString());
			con.commit();
			return cro;
		}catch(Exception ex){
			ex.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
