package com.steering.jobface.qcm.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TimerTask;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.model.qcm.*;

import org.apache.log4j.Logger;

public class BcPhyJudgeTask extends TimerTask {
	private static class BcPhyJudgeTaskClass{
		private static final BcPhyJudgeTask instance = new BcPhyJudgeTask();
	}
	
	private BcPhyJudgeTask(){}
	
	public static BcPhyJudgeTask getInstance(){
		return BcPhyJudgeTaskClass.instance;
	}

	private final Logger logger = Logger.getLogger(MainTask.class);
	private String _checkNo = "";
	private String _judgeStoveNo = "";
	private String _batchNo = "";
	private String _sampleNo = "";
	
	public void run(){
		synchronized (this){
			Connection qcmCon = null;
			Statement st = null;
			Connection jhyCon = null;
			Statement stJhy = null;
			try{
				qcmCon = new DBCommon().getDao(CoreConstantData.sysbeanstr).getConnection();
				qcmCon.setAutoCommit(false);
				st = qcmCon.createStatement();
				
				jhyCon = new DBCommon().getDao("JhyDao").getConnection();
				jhyCon.setAutoCommit(false);
				stJhy=jhyCon.createStatement();
				
				String[] processCodes = new String[]{"D", "E", "F", "G"};
				for(String processCode : processCodes){

					_checkNo = "";
					_judgeStoveNo = "";
					_batchNo = "";
					_sampleNo = "";

					//获取检化验数据 熔炼炉号判定
					SqlBuilder b = new SqlBuilder("BcPhyJudgeTask.getZcCheck");
					b.Fill(new String[]{ processCode });
					List<QcmZcCheckconsignEntity> zcChecks = EntityTool.getEntitysBySqlNoClsCon(
							b.toString(), qcmCon, QcmZcCheckconsignEntity.class);

					//调试用代码
					/*zcChecks.clear();
					QcmZcCheckconsignEntity a = new QcmZcCheckconsignEntity();
					a.setCheckNo("242037302");
					zcChecks.add(a);*/

					for(QcmZcCheckconsignEntity zcCheck : zcChecks){
						try {
							_checkNo = "";
							_judgeStoveNo = "";
							_batchNo = "";
							_sampleNo = "";
							judge(zcCheck.getCheckNo(), "SYSTEM", qcmCon, st, jhyCon, stJhy);
							qcmCon.commit();
							jhyCon.commit();
						}catch(Exception ex) {
							qcmCon.rollback();
							jhyCon.rollback();
							//更新检化验表
							b = new SqlBuilder("BcChemJudgeTask.updateQcmJhyElement");
							b.Fill(new String[]{ zcCheck.getCheckNo(), "-1" });
							st.executeUpdate(b.toString());
							//修改理化检验表的判定状态
							b = new SqlBuilder("BcPhyJudgeTask.updateJudgeFlagByBatchNo");
							b.Fill(new String[]{ zcCheck.getCheckNo(), "-1" });
							st.executeUpdate(b.toString());
							qcmCon.commit();
							String msg = "BcPhyJudgeTask异常: CheckNo="+ _checkNo + "JudgeStoveNo=" + _judgeStoveNo + "BatchNo="
									+ _batchNo + "SampleNo=" + _sampleNo + ex.getMessage();
							logger.error(msg);
						}
					}
				}
			}catch(Exception ex){
				try {
					qcmCon.rollback();
					jhyCon.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				String msg = "BcPhyJudgeTask异常: CheckNo="+ _checkNo + "JudgeStoveNo=" + _judgeStoveNo + "BatchNo="
						+ _batchNo + "SampleNo=" + _sampleNo + ex.getMessage();
				logger.error(msg);
			}finally{
				if(st != null){
					try {
						st.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(stJhy != null){
					try {
						stJhy.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(qcmCon != null){
					try {
						qcmCon.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if(jhyCon != null){
					try {
						jhyCon.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	/**
     * 判定
	 * @throws Exception 
     */
    public void judge(String checkNo, String user, Connection con, Statement st,Connection jhyCon, Statement stJhy) throws Exception{
		SqlBuilder b;
		b = new SqlBuilder("BcPhyJudgeTask.getConsignDetail");
		b.Fill(new String[]{ checkNo });
		ArrayList<QcmZcCheckconsignDetailEntity> consignDetails = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, QcmZcCheckconsignDetailEntity.class);
		
        //获取检验委托表
        b = new SqlBuilder("BcPhyJudgeTask.queryConsignAllByCheckNo");
        b.Fill(new String[]{ checkNo });
        List<QcmZcCheckconsignEntity> consigns = EntityTool.getEntitysBySqlNoClsCon(
        		b.toString(), con, QcmZcCheckconsignEntity.class);
        for(QcmZcCheckconsignEntity consign : consigns){
        	b = new SqlBuilder("BcPhyJudgeTask.QueryGroupSampleNoByCheckNo");
            b.Fill(new String[]{ consign.getCheckNo(), consign.getJudgeStoveNo(),
            		consign.getBatchNo(), consign.getProcessCode() });
            ArrayList<QcmZcCheckconsignDetailEntity> detailGroups = EntityTool.getEntitysBySqlNoClsCon(
        			b.toString(), con, QcmZcCheckconsignDetailEntity.class);
            
    		b = new SqlBuilder("BcPhyJudgeTask.queryJhyElementAllByCheckNo");
    		b.Fill(new String[]{ consign.getCheckNo(), consign.getJudgeStoveNo(),
    				consign.getBatchNo(), consign.getProcessCode() });
    		List<QcmJhyElementsEntity> jhyElements = EntityTool.getEntitysBySqlNoClsCon(
    				b.toString(), con, QcmJhyElementsEntity.class);
    		
    		//判定内控
			b = new SqlBuilder("BcChemJudgeTask.queryCraftChemByDlyLnPk");
			b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
					consign.getIndexSeq(), "A", consign.getCic(), consign.getCraftSource() });
			ArrayList<CraftOrdDesignStdCicEntity> craftCics = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdCicEntity.class);
            if(consign.getSteelStandrad().contains("CIC") && craftCics.size() == 0){
            	throw new Exception("没有找到成分工艺信息！");
            }
            
            //判定成分
            String chemJudgeId = "";
            if(jhyElements.size() > 0){
            	String[] chemResults = BcChemJudgeTask.judge(consign, jhyElements,
            			craftCics, st, con);
            	chemJudgeId = chemResults[0];
            	//更新检化验表
        		b = new SqlBuilder("BcChemJudgeTask.updateQcmJhyElement");
        		b.Fill(new String[]{ consign.getCheckNo(), "20" });
        		st.executeUpdate(b.toString());
            }
            
			String phyJudgeId = "";
            if(detailGroups.size() > 0){
            	phyJudgeId = judge(detailGroups, consign, user, st, con);
            	//修改理化检验表的判定状态
                b = new SqlBuilder("BcPhyJudgeTask.updateJudgeFlagByBatchNo");
                b.Fill(new String[]{ consign.getCheckNo(), "20" });
                st.executeUpdate(b.toString());
            }
            
            //保存委托二层表的判定结果
            String processCode = consign.getProcessCode();
            //String chemResult = setChemConsignDetail(consign, processCode, checkDetails, st, con);
            ArrayList<QcmZcCheckconsignDetailEntity> batchConsignDetails = new ArrayList<QcmZcCheckconsignDetailEntity>();
            for(QcmZcCheckconsignDetailEntity consignDetail : consignDetails){
            	if(consignDetail.getBatchNo().equals(consign.getBatchNo())){
            		batchConsignDetails.add(consignDetail);
            	}
            }
            String[] phyResults = setConsignDetail(consign, processCode, batchConsignDetails, st, con , stJhy);
            String phyResult = phyResults[0];
            String chemResult = phyResults[1];
            String isNoSsc = phyResults[2];
            String isNoHic = phyResults[3];
            String resample = phyResults[4];
            String judgeResult = phyResults[5];
            
            b = new SqlBuilder("BcPhyJudgeTask.updateConsignResult");
            b.Fill(new String[]{ consign.getCheckNo(), consign.getJudgeStoveNo(), consign.getBatchNo(),
            		processCode, phyResult, chemResult, judgeResult, user, "", chemJudgeId,
            		phyJudgeId, "20", resample, isNoSsc, isNoHic });
            st.executeUpdate(b.toString());
            
            //写入首次判定时间
            b = new SqlBuilder("BcPhyJudgeTask.updateConsignFirstDate");
            b.Fill(new String[]{ consign.getCheckNo(), consign.getJudgeStoveNo(), consign.getBatchNo(),
            		processCode});
            st.executeUpdate(b.toString());
            
            
        }
        
        boolean existLotRate = false;//按生产批取样
        boolean existStoveRate = false;//按炉取样
        boolean existFd = false;//首日检
        QcmZcCheckconsignEntity consignTmp = null;
        for(QcmZcCheckconsignEntity consign : consigns){
        	consignTmp = consign;
        	if(consign.getCraftSource().equals("1")){
        		existFd = true;
        		continue;
        	}
        	b = new SqlBuilder("BcPhyJudgeTask.getConsignDetailByBatchNo");
        	b.Fill(new String[]{ consign.getCheckNo(), consign.getBatchNo() });
        	ArrayList<QcmZcCheckconsignDetailEntity> details = EntityTool.getEntitysBySqlNoClsCon(
        			b.toString(), con, QcmZcCheckconsignDetailEntity.class);
        	if(!existLotRate){
        		for(QcmZcCheckconsignDetailEntity detail : details){
        			if(detail.getRate().equals("408601")){
            			existLotRate = true;
            		}else if(detail.getRate().equals("408603")){
            			existStoveRate = true;
            		}
            	}
        	}
        	
        	if(existFd || existLotRate || existStoveRate) continue;
            setBcBatchResult(consign, user, st, con);
        }
        //保存炉批结果
        if(existFd){
    		b = new SqlBuilder("BcPhyJudgeTask.getConsignByOrdLnDlyPkPline");
    		b.Fill(new String[]{ consignTmp.getOrdLnDlyPk(), consignTmp.getPlineCode(), "0",
    				consignTmp.getProcessCode() });
    		ArrayList<QcmZcCheckconsignEntity> stoveConsigns = EntityTool.getEntitysBySqlNoClsCon(
    				b.toString(), con, QcmZcCheckconsignEntity.class);
    		for(QcmZcCheckconsignEntity stoveConsign : stoveConsigns){
    			setBcBatchResult(stoveConsign, user, st, con);
    		}
    	}else if(existLotRate){
			String loopValue = "-1";
			ArrayList<QcmZcCheckconsignEntity> lotConsigns = new ArrayList<QcmZcCheckconsignEntity>();
			if (consignTmp.getOrdLnDlyPk().length() > 35) {
				b = new SqlBuilder("JdgCraftOrdDesignStdPicSmp.getRate");
				b.Fill(new String[]{ consignTmp.getOrdLnDlyPk().substring(0,35), consignTmp.getCraftSeq(), consignTmp.getIndexSeq() , consignTmp.getProcessCode(), consignTmp.getPic() });
				ArrayList<CraftOrdDesignStdPicSmpEntity> tmpCraftSmps = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, CraftOrdDesignStdPicSmpEntity.class);

				for (CraftOrdDesignStdPicSmpEntity tmpCraftSmp : tmpCraftSmps) {
					if(tmpCraftSmp.getLoopValue().equals("0")){
						loopValue = tmpCraftSmp.getLoopValue();
					}
				}
			}
			if("0".equals(loopValue)){
				b = new SqlBuilder("BcPhyJudgeTask.getConsignByLotNo");
				b.Fill(new String[]{ consignTmp.getLotNo(), "", consignTmp.getCraftSource(),
						consignTmp.getProcessCode() });
				lotConsigns = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmZcCheckconsignEntity.class);
			} else {
				b = new SqlBuilder("BcPhyJudgeTask.getConsignByLotNo");
				b.Fill(new String[]{ consignTmp.getLotNo(), consignTmp.getLotNoGroup(), consignTmp.getCraftSource(),
						consignTmp.getProcessCode() });
				lotConsigns = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmZcCheckconsignEntity.class);
			}

    		for(QcmZcCheckconsignEntity lotConsign : lotConsigns){
    			setBcBatchResult(lotConsign, user, st, con);
    		}
    	}else if(existStoveRate){
			String loopValue = "-1";
			ArrayList<QcmZcCheckconsignEntity> stoveConsigns = new ArrayList<QcmZcCheckconsignEntity>();
			if (consignTmp.getOrdLnDlyPk().length() > 35) {
				b = new SqlBuilder("JdgCraftOrdDesignStdPicSmp.getRate");
				b.Fill(new String[]{ consignTmp.getOrdLnDlyPk().substring(0,35), consignTmp.getCraftSeq(), consignTmp.getIndexSeq() , consignTmp.getProcessCode(), consignTmp.getPic() });
				ArrayList<CraftOrdDesignStdPicSmpEntity> tmpCraftSmps = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, CraftOrdDesignStdPicSmpEntity.class);

				for (CraftOrdDesignStdPicSmpEntity tmpCraftSmp : tmpCraftSmps) {
					if(tmpCraftSmp.getLoopValue().equals("0")){
						loopValue = tmpCraftSmp.getLoopValue();
					}
				}
			}
			if("0".equals(loopValue)){
				b = new SqlBuilder("BcPhyJudgeTask.getConsignByLotNo");
				b.Fill(new String[]{ consignTmp.getLotNo(), "", consignTmp.getCraftSource(),
						consignTmp.getProcessCode() });
				stoveConsigns = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmZcCheckconsignEntity.class);
			} else {
				b = new SqlBuilder("BcPhyJudgeTask.getConsignByJudgeStoveNo");
				b.Fill(new String[]{ consignTmp.getJudgeStoveNo(), "0",
						consignTmp.getProcessCode() });
				stoveConsigns = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmZcCheckconsignEntity.class);
			}
    		for(QcmZcCheckconsignEntity stoveConsign : stoveConsigns){
    			setBcBatchResult(stoveConsign, user, st, con);
    		}
    	}
    }
	
	/**
     * 保存委托二层表判定结果
     * @param st
     * @param con
     * @throws SQLException
     */
    private String[] setConsignDetail(QcmZcCheckconsignEntity consign, String processCode,
    		ArrayList<QcmZcCheckconsignDetailEntity> consignDetails, Statement st, Connection con
    		, Statement stJhy)
    		throws SQLException{
    	String[] results = new String[6];
    	SqlBuilder b = null;
    	String okFlag="0";
    	
    	ArrayList<String> phyResults = new ArrayList<String>();
    	ArrayList<String> chemResults = new ArrayList<String>();
    	boolean noSsc = false;
    	boolean noHic = false;
    	boolean noOther = false;
		for(QcmZcCheckconsignDetailEntity consignDetail : consignDetails){
			String chkNkResult = consignDetail.getNkResult();
			String chkJfResult = consignDetail.getJfResult();
			ArrayList<String> phyNkResults = new ArrayList<String>();
			ArrayList<String> phyJfResults = new ArrayList<String>();
			if(consignDetail.getPhyCodeMin().equals("HX")){
				b = new SqlBuilder("BcPhyJudgeTask.getChemResultBySampleNo");
				b.Fill(new String[]{ consignDetail.getSampleId(), consignDetail.getJudgeStoveNo(),
						consignDetail.getBatchNo(), consignDetail.getSampleNo(), consign.getCraftSource() });
				ArrayList<QcmBcChemResultEntity> bcChemResults = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmBcChemResultEntity.class);
				for(QcmBcChemResultEntity bcChemResult : bcChemResults){
					phyNkResults.add(bcChemResult.getNkQualified());
					phyJfResults.add(bcChemResult.getJfQualified());
				}
			}else{
				b = new SqlBuilder("BcPhyJudgeTask.getPhyResultBySampleNoPhyMin");
				b.Fill(new String[]{ consignDetail.getSampleId(), consignDetail.getJudgeStoveNo(),
						consignDetail.getBatchNo(), consignDetail.getSampleNo(), consign.getCraftSource(),
						consignDetail.getPhyCodeMin() });
				ArrayList<QcmBcPhyresultEntity> bcPhyResults = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmBcPhyresultEntity.class);
				for(QcmBcPhyresultEntity bcPhyResult : bcPhyResults){
					phyNkResults.add(bcPhyResult.getNkTolQualified());
					phyJfResults.add(bcPhyResult.getJfTolQualified());
				}
			}
			
			if(phyNkResults.size() == 0){
				consignDetail.setNkResult("");
			}else if(phyNkResults.contains("2")){
				if(consignDetail.getPhyCodeMin().equals("HX")){
					if(phyNkResults.contains("1")){
						consignDetail.setNkResult("0");
					}else{
						consignDetail.setNkResult("");
					}
				}else{
					consignDetail.setNkResult("");
				}
			}else if(phyNkResults.contains("3")){
				consignDetail.setNkResult("");
			}else if(phyNkResults.contains("0")){
				consignDetail.setNkResult("0");
			}else{
				consignDetail.setNkResult("1");
			}
			if(phyJfResults.size() == 0){
				consignDetail.setJfResult("");
			}else if(phyJfResults.contains("2")){
				if(consignDetail.getPhyCodeMin().equals("HX")){
					if(phyJfResults.contains("1")){
						consignDetail.setJfResult("0");
					}else{
						consignDetail.setJfResult("");
					}
				}else{
					consignDetail.setJfResult("");
				}
			}else if(phyJfResults.contains("3")){
				consignDetail.setJfResult("");
			}else if(phyJfResults.contains("0")){
				consignDetail.setJfResult("0");
			}else{
				consignDetail.setJfResult("1");
			}
			
			if((phyNkResults.contains("3") || consignDetail.getPhyNameMin().contains("冲击试验"))
					&& (consignDetail.getChk().equals("True") || consignDetail.getResultSource().equals("1"))){
				consignDetail.setResultSource("1");
				//只有结果为3 系统处理不了的可以人工修改结果
				consignDetail.setNkResult(chkNkResult);
				consignDetail.setJfResult(chkJfResult);
				//修改判定结果
	    		b = new SqlBuilder("BcPhyJudgeTask.updatePhyResultJudge");
	    		b.Fill(new String[]{ consignDetail.getJudgeStoveNo(), consignDetail.getBatchNo(),
	    				consignDetail.getSampleId(), consignDetail.getSampleNo(), processCode,
	    				consignDetail.getPhyCodeMin(),
	    				chkNkResult, chkJfResult});
	    		st.execute(b.toString());
			}else{
				consignDetail.setResultSource("0");
			}
			
			if(consignDetail.getNkResult().equals("")){
				if(consignDetail.getPhyCodeMin().equals("C0030") || consignDetail.getPhyCodeMin().equals("C0031")
						|| consignDetail.getPhyCodeMin().equals("C0032") || consignDetail.getPhyCodeMin().equals("C0033")
						|| consignDetail.getPhyCodeMin().equals("C0133") || consignDetail.getPhyCodeMin().equals("C0147")
						|| consignDetail.getPhyCodeMin().equals("C0150")){
					noSsc = true;
				}else if(consignDetail.getPhyCodeMin().equals("C0007") || consignDetail.getPhyCodeMin().equals("C0146")
						||consignDetail.getPhyCodeMin().equals("C0061") || consignDetail.getPhyCodeMin().equals("C0148")
						||consignDetail.getPhyCodeMin().equals("C0065")){
					noHic = true;
				}else{
					noOther = true;
				}
			}
			if(consignDetail.getPhyCodeMin().equals("HX")){
				chemResults.add(consignDetail.getNkResult());
			}else{
				phyResults.add(consignDetail.getNkResult());
			}
			b = new SqlBuilder("BcPhyJudgeTask.setConsignDetailJudgeResult");
			b.Fill(new String[]{ consignDetail.getSampleId(), consignDetail.getBatchNo(), consignDetail.getSampleNo(),
					consignDetail.getPhyCodeMin(), consignDetail.getNkResult(),
					consignDetail.getJfResult(), consignDetail.getResultSource() });
			st.executeUpdate(b.toString());
			// 如果是化学，还要更新ONH。
			if(consignDetail.getPhyCodeMin().equals("HX")){
				b = new SqlBuilder("BcPhyJudgeTask.setConsignDetailJudgeResult");
				b.Fill(new String[]{ consignDetail.getSampleId(), consignDetail.getBatchNo(), consignDetail.getSampleNo(),
						"ONH", consignDetail.getNkResult(), consignDetail.getJfResult(),
						consignDetail.getResultSource() });
				st.executeUpdate(b.toString());
			}
			
			//修改检化验判定合格标识 只修改理化
			if("0".equals(consignDetail.getNkResult())||"0".equals(consignDetail.getJfResult())){
				if(!consignDetail.getPhyCodeMin().equals("HX")){
					String sql = CoreXmlSqlParsersFactory
			                .getSql("BcPhyJudgeTask.isLxPhy");
			        PreparedStatement ps = con.prepareStatement(sql);
			        ps.setObject(1, consignDetail.getPhyCodeMin());
			        String count="";
			        ResultSet rs = ps.executeQuery();
			        if (rs.next()) {
			        	count=rs.getString(1);
			        }
			        rs.close();
			        ps.close();
			        if(!"0".equals(count))
			        {
			        	okFlag="1";
			        }
				}
			}

		}
		
		//修改检化验判定合格标识 只修改理化
		b = new SqlBuilder("BcPhyJudgeTask.updatePipeFlag");
        b.Fill(new String[]{ okFlag, consign.getCheckNo()});
        stJhy.executeUpdate(b.toString());
		
		String resample = "";
		for(QcmZcCheckconsignDetailEntity consignDetail : consignDetails){
			if(consignDetail.getNkResult().equals("0")){
				int failCnt = 0;
				int judgeCnt = 0;
				for(QcmZcCheckconsignDetailEntity consignDetail2 : consignDetails){
					if(consignDetail.getPhyCodeMin().equals(consignDetail2.getPhyCodeMin())){
						judgeCnt++;
						if(consignDetail2.getNkResult().equals("0")){
							failCnt++;
						}
					}
				}
				if(judgeCnt == 0){
					resample = "1";
				}else{
					if(failCnt < judgeCnt){
    					resample = "1";
					}else{
						resample = "0";
						break;
					}
				}
			}else if(consignDetail.getNkResult().equals("")){
				if(!consignDetail.getPhyCodeMin().equals("C0030") && !consignDetail.getPhyCodeMin().equals("C0031")
						&& !consignDetail.getPhyCodeMin().equals("C0032") && !consignDetail.getPhyCodeMin().equals("C0033")
						&& !consignDetail.getPhyCodeMin().equals("C0133") && !consignDetail.getPhyCodeMin().equals("C0147")
						&& !consignDetail.getPhyCodeMin().equals("C0007") && !consignDetail.getPhyCodeMin().equals("C0146")
						&& !consignDetail.getPhyCodeMin().equals("C0061") && !consignDetail.getPhyCodeMin().equals("C0148")
						&& !consignDetail.getPhyCodeMin().equals("C0150") && !consignDetail.getPhyCodeMin().equals("C0065")){
					resample = "0";
					break;
				}
			}else{
				if(resample.equals("")){
					resample = "0";
				}
			}
		}
    	
    	String isNoSsc = "0";
    	if(noSsc && !noOther){
    		isNoSsc = "1";
    	}
    	String isNoHic = "0";
    	if(noHic && !noOther){
    		isNoHic = "1";
    	}
    	
    	results[2] = isNoSsc;
    	results[3] = isNoHic;
    	results[4] = resample;
    	
    	results[0] = "";
    	if(phyResults.contains("")){
    		results[0] = "";
    	}else if(phyResults.contains("0")){
    		results[0] = "40740202";
    	}else if(phyResults.contains("1")){
    		results[0] = "40740201";
    	}
    	
    	results[1] = "";
    	if(chemResults.contains("")){
    		results[1] = "";
    	}else if(chemResults.contains("0")){
    		results[1] = "40740102";
    	}else if(chemResults.contains("1")){
    		results[1] = "40740101";
    	}
    	
    	String judgeResult = "";
        ArrayList<String> allResults = new ArrayList<String>();
        if(chemResults.size() > 0){
        	allResults.add(results[1]);
        }
        if(phyResults.size() > 0){
        	allResults.add(results[0]);
        }
        
        if(allResults.contains("")){
        	judgeResult = "";
        }else if(allResults.contains("40740104") || allResults.contains("40740204")){
        	judgeResult = "40740708";
        }else if(allResults.contains("40740103") || allResults.contains("40740203")){
        	judgeResult = "40740707";
        }else if(allResults.contains("40740102") || allResults.contains("40740202")){
        	judgeResult = "40740704";
        }else{
        	judgeResult = "40740701";
        }
        results[5] = judgeResult;
    	
    	return results;
    }
    
    private void setBcBatchResult(QcmZcCheckconsignEntity consign, String user,
    		Statement st,  Connection con) throws SQLException{
    	SqlBuilder b = null;
    	
    	b = new SqlBuilder("BcPhyJudgeTask.getGpApplyPlineByStoveNo");
		b.Fill(new String[]{ consign.getStoveNo() });
		QcmGpJudgeApplyEntity gpJudge = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, QcmGpJudgeApplyEntity.class);
		String gpType = "";
		if(gpJudge != null){
			if(gpJudge.getPlineCode().equals("C063") || gpJudge.getPlineCode().equals("C064")){
				gpType = "1";
    		}else{
    			gpType = "0";
    		}
		}
    	
    	ArrayList<QcmJhyPhySplineEntity> splineResults = new ArrayList<QcmJhyPhySplineEntity>();
    	b = new SqlBuilder("BcPhyJudgeTask.getCraftPhyCnt");
    	b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
    			consign.getProcessCode(), "0", consign.getIndexSeq() });
    	GenericEntity phyCount = EntityTool.getEntityBySqlNoClsCon(
    			b.toString(), con, GenericEntity.class);
    	b = new SqlBuilder("BcPhyJudgeTask.getCraftPhyCnt");
    	b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
    			consign.getProcessCode(), "1", consign.getIndexSeq() });
    	GenericEntity phyCountFd = EntityTool.getEntityBySqlNoClsCon(
    			b.toString(), con, GenericEntity.class);
    	if(!phyCount.getCount().equals("0") || !phyCountFd.getCount().equals("0")){
    		b = new SqlBuilder("BcPhyJudgeTask.QueryPhyConsignDetaiByBatchNo");
        	b.Fill(new Object[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(), consign.getIndexSeq(),
        			consign.getProcessCode(), consign.getPic(), consign.getJudgeStoveNo(),
        			consign.getBatchNo(), consign.getLotNo(),
        			consign.getLotNoGroup(), "0", "0", new String[]{}, "", "0", consign.getPlineCode(), gpType });
        	ArrayList<QcmJhyPhySplineEntity> phySplineResults = EntityTool.getEntitysBySqlNoClsCon(
            		b.toString(), con, QcmJhyPhySplineEntity.class);
        	for(QcmJhyPhySplineEntity phySplineResult : phySplineResults){
        		splineResults.add(phySplineResult);
        	}
        	
        	//重新热处理
        	b = new SqlBuilder("BcPhyJudgeTask.QueryPhyConsignDetaiByBatchNo");
        	b.Fill(new Object[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(), consign.getIndexSeq(),
        			consign.getProcessCode(), consign.getPic(), consign.getJudgeStoveNo(),
        			consign.getBatchNo(), consign.getLotNo(),
        			consign.getLotNoGroup(), "0", "0", new String[]{}, "", "1", consign.getPlineCode(), gpType });
        	phySplineResults = EntityTool.getEntitysBySqlNoClsCon(
            		b.toString(), con, QcmJhyPhySplineEntity.class);
        	HashMap<String, ArrayList<QcmJhyPhySplineEntity>> phyMap = new HashMap<String, ArrayList<QcmJhyPhySplineEntity>>();
        	for(QcmJhyPhySplineEntity splineResult : splineResults){
        		if(!splineResult.getSampleNo().equals("")) continue;
        		if(phyMap.containsKey(splineResult.getPhyCodeMin())){
        			ArrayList<QcmJhyPhySplineEntity> tmpSplines = phyMap.get(splineResult.getPhyCodeMin());
        			tmpSplines.add(splineResult);
        		}else{
        			ArrayList<QcmJhyPhySplineEntity> tmpSplines = new ArrayList<QcmJhyPhySplineEntity>();
        			tmpSplines.add(splineResult);
        			phyMap.put(splineResult.getPhyCodeMin(), tmpSplines);
        		}
        	}
        	
        	for(String key : phyMap.keySet()){
        		boolean addRcl = false;
        		for(QcmJhyPhySplineEntity spline : phySplineResults){
        			if(spline.getSampleNo().equals("")) continue;
        			if(spline.getPhyCodeMin().equals(key)){
        				splineResults.add(spline);
        				addRcl = true;
        			}
        		}
        		if(addRcl){
        			ArrayList<QcmJhyPhySplineEntity> tmpSplines = phyMap.get(key);
        			for(QcmJhyPhySplineEntity tmpSpline : tmpSplines){
        				splineResults.remove(tmpSpline);
        			}
        		}
        	}
        	
        	//首日检
        	b = new SqlBuilder("BcPhyJudgeTask.QueryPhyConsignDetaiByBatchNo");
        	b.Fill(new Object[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(), consign.getIndexSeq(),
        			consign.getProcessCode(), consign.getPic(), consign.getJudgeStoveNo(),
        			consign.getBatchNo(), consign.getLotNo(),
        			consign.getLotNoGroup(), "1", "0", new String[]{}, "", "0", consign.getPlineCode(), gpType });
        	phySplineResults = EntityTool.getEntitysBySqlNoClsCon(
            		b.toString(), con, QcmJhyPhySplineEntity.class);
        	for(QcmJhyPhySplineEntity phySplineResult : phySplineResults){
        		splineResults.add(phySplineResult);
        	}
    	}
    	
    	b = new SqlBuilder("BcPhyJudgeTask.getCraftChemCnt");
    	b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
    			"0", consign.getIndexSeq(), consign.getProcessCode() });
    	GenericEntity chemCount = EntityTool.getEntityBySqlNoClsCon(
    			b.toString(), con, GenericEntity.class);
    	b = new SqlBuilder("BcPhyJudgeTask.getCraftChemCnt");
    	b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
    			"1", consign.getIndexSeq(), consign.getProcessCode() });
    	GenericEntity chemCountFd = EntityTool.getEntityBySqlNoClsCon(
    			b.toString(), con, GenericEntity.class);
    	if(!chemCount.getCount().equals("0")){
    		b = new SqlBuilder("BcPhyJudgeTask.getProductCnt");
			b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
					consign.getCraftSource() });
			CraftOrdDesignStdEntity craftStd = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdEntity.class);
    		b = new SqlBuilder("BcPhyJudgeTask.getChemConsignDetaiByBatchNo");
        	b.Fill(new Object[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(), consign.getJudgeStoveNo(),
        			consign.getBatchNo(), consign.getProcessCode(),
        			consign.getCic(), "0", craftStd.getRateN(), consign.getPlineCode(), gpType });
        	ArrayList<QcmJhyPhySplineEntity> chemSplineResults = EntityTool.getEntitysBySqlNoClsCon(
            		b.toString(), con, QcmJhyPhySplineEntity.class);
        	for(QcmJhyPhySplineEntity chemSplineResult : chemSplineResults){
        		splineResults.add(chemSplineResult);
        	}
    	}
    	if(!chemCountFd.getCount().equals("0")){
    		//首日检
        	b = new SqlBuilder("BcPhyJudgeTask.getProductCnt");
			b.Fill(new String[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(),
					consign.getCraftSource() });
			CraftOrdDesignStdEntity craftStdFd = EntityTool.getEntityBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdEntity.class);
			if(craftStdFd != null){
				b = new SqlBuilder("BcPhyJudgeTask.getChemConsignDetaiByBatchNo");
	        	b.Fill(new Object[]{ consign.getOrdLnDlyPk(), consign.getCraftSeq(), consign.getJudgeStoveNo(),
	        			consign.getBatchNo(), consign.getProcessCode(),
	        			consign.getCic(), "1", craftStdFd.getRateN(), consign.getPlineCode(), gpType });
	        	ArrayList<QcmJhyPhySplineEntity> chemSplineResults = EntityTool.getEntitysBySqlNoClsCon(
	            		b.toString(), con, QcmJhyPhySplineEntity.class);
	        	for(QcmJhyPhySplineEntity chemSplineResult : chemSplineResults){
	        		splineResults.add(chemSplineResult);
	        	}
			}
    	}
    	
    	boolean noSsc = false;
    	boolean noHic = false;
        boolean noOther = false;
        ArrayList<String> phyResults = new ArrayList<String>();
        ArrayList<String> chemResults = new ArrayList<String>();
        for(QcmJhyPhySplineEntity splineResult : splineResults){
        	String phyCodeMin = splineResult.getPhyCodeMin();
        	if(!splineResult.getSampleId().contains("F")){
        		String reSampleId = "F" + splineResult.getSampleId();
        		boolean existReSample = false;
        		for(QcmJhyPhySplineEntity splineResult2 : splineResults){
            		if(reSampleId.equals(splineResult2.getSampleId())
            				&& splineResult.getJudgeStoveNo().equals(splineResult2.getJudgeStoveNo())
            				&& splineResult.getBatchNo().equals(splineResult2.getBatchNo())
            				&& splineResult.getPhyCodeMin().equals(splineResult2.getPhyCodeMin())){
            			existReSample = true;
            			break;
            		}
            	}
        		if(existReSample){
        			continue;
        		}else{
        			if(splineResult.getPhyCodeMin().equals(splineResult.getPhyCodeMin())){
        				if(splineResult.getNkResult().equals("")){
        					if(phyCodeMin.equals("C0030") || phyCodeMin.equals("C0031")
        							|| phyCodeMin.equals("C0032") || phyCodeMin.equals("C0033")
        							|| phyCodeMin.equals("C0133") || phyCodeMin.equals("C0147")
        							|| phyCodeMin.equals("C0150")){
        						noSsc = true;
        					}else if(phyCodeMin.equals("C0007") || phyCodeMin.equals("C0146")
        							||phyCodeMin.equals("C0061") || phyCodeMin.equals("C0148")
        							||phyCodeMin.equals("C0065")){
        						noHic = true;
        					}else{
        						noOther = true;
        					}
        					if(splineResult.getPhyCodeMin().equals("HX")){
        						chemResults.add("");
        					}else{
        						if(consign.getProduccode().equals("S000046") && consign.getSteelcode().equals("C209")
        								&& splineResult.getPhyNameMin().contains("洛氏硬度")){
        							boolean existsYb = false;//是否有预备硬度
    								for(QcmJhyPhySplineEntity splineResult2 : splineResults){
    				            		if(splineResult2.getPhyNameMin().contains("预备硬度")
    				            				&& !splineResult2.getNkResult().equals("")){
    				            			existsYb = true;
    				            			break;
    				            		}
    				            	}
    								if(existsYb){
    									continue;
    								}else{
    									phyResults.add("");
    								}
        						}else{
        							phyResults.add("");
        						}
        					}
        				}else if(splineResult.getNkResult().equals("0")){
        					if(splineResult.getPhyCodeMin().equals("HX")){
        						chemResults.add("40740102");
        					}else{
        						phyResults.add("40740202");
        					}
                    	}else if(splineResult.getNkResult().equals("1")){
                    		if(splineResult.getPhyCodeMin().equals("HX")){
        						chemResults.add("40740101");
        					}else{
        						phyResults.add("40740201");
        					}
                    	}
        			}
        		}
        	}else{
        		if(splineResult.getPhyCodeMin().equals(splineResult.getPhyCodeMin())){
    				if(splineResult.getNkResult().equals("")){
    					if(phyCodeMin.equals("C0030") || phyCodeMin.equals("C0031")
    							|| phyCodeMin.equals("C0032") || phyCodeMin.equals("C0033")
    							|| phyCodeMin.equals("C0133") || phyCodeMin.equals("C0147")
    							|| phyCodeMin.equals("C0150")){
    						noSsc = true;
    					}else if(phyCodeMin.equals("C0007") || phyCodeMin.equals("C0146")
    							||phyCodeMin.equals("C0061") || phyCodeMin.equals("C0148")
    							||phyCodeMin.equals("C0065")){
    						noHic = true;
    					}else{
    						noOther = true;
    					}
    					if(splineResult.getPhyCodeMin().equals("HX")){
    						chemResults.add("");
    					}else{
    						if(consign.getProduccode().equals("S000046") && consign.getSteelcode().equals("C209")
    								&& splineResult.getPhyNameMin().contains("洛氏硬度")){
    							boolean existsYb = false;//是否有预备硬度
								for(QcmJhyPhySplineEntity splineResult2 : splineResults){
				            		if(splineResult2.getPhyNameMin().contains("预备硬度")
				            				&& !splineResult2.getNkResult().equals("")){
				            			existsYb = true;
				            			break;
				            		}
				            	}
								if(existsYb){
									continue;
								}else{
									phyResults.add("");
								}
    						}else{
    							phyResults.add("");
    						}
    					}
    				}else if(splineResult.getNkResult().equals("0")){
    					if(splineResult.getPhyCodeMin().equals("HX")){
    						chemResults.add("40740102");
    					}else{
    						phyResults.add("40740202");
    					}
                	}else if(splineResult.getNkResult().equals("1")){
                		if(splineResult.getPhyCodeMin().equals("HX")){
    						chemResults.add("40740101");
    					}else{
    						phyResults.add("40740201");
    					}
                	}
    			}
        	}
        }
        String phyResult = "";
        if(phyResults.contains("")&&!phyResults.contains("40740202")){
        	phyResult = "";
        }else if(phyResults.contains("40740202")){
        	phyResult = "40740202";
        }else if(phyResults.contains("40740201")){
        	phyResult = "40740201";
        }
        String chemResult = "";
        if(chemResults.contains("")){
        	chemResult = "";
        }else if(chemResults.contains("40740102")){
        	chemResult = "40740102";
        }else if(chemResults.contains("40740101")){
        	chemResult = "40740101";
        }
        String isNoSsc = "0";
    	if(noSsc && !noOther){
    		isNoSsc = "1";
    	}
    	String isNoHic = "0";
    	if(noHic && !noOther){
    		isNoHic = "1";
    	}
       
    	String judgeResult = "";
        ArrayList<String> allResults = new ArrayList<String>();
        if(!chemCount.getCount().equals("0")){
        	allResults.add(chemResult);
        }
        if(!phyCount.getCount().equals("0")){
        	allResults.add(phyResult);
        }
        
        if(allResults.contains("")){
        	judgeResult = "";
        }else if(allResults.contains("40740104") || allResults.contains("40740204")){
        	judgeResult = "40740708";
        }else if(allResults.contains("40740103") || allResults.contains("40740203")){
        	judgeResult = "40740707";
        }else if(allResults.contains("40740102") || allResults.contains("40740202")){
        	judgeResult = "40740704";
        }else{
        	judgeResult = "40740701";
        }
        
        b = new SqlBuilder("BcPhyJudgeTask.getBcBatchResultByBatchNo");
        b.Fill(new String[]{ consign.getJudgeStoveNo(), consign.getBatchNo(),
        		consign.getProcessCode(), "0" });
        QcmBcBatchResultEntity batchResult = EntityTool.getEntityBySqlNoClsCon(
        		b.toString(), con, QcmBcBatchResultEntity.class);
        if(batchResult == null){
        	 batchResult = new QcmBcBatchResultEntity();
        	 batchResult.setJudgeStoveNo(consign.getJudgeStoveNo());
        	 batchResult.setBatchNo(consign.getBatchNo());
        	 batchResult.setProcessCode(consign.getProcessCode());
        	 batchResult.setCraftSource("0");
        	 batchResult.setPhyResultCode(phyResult);
        	 batchResult.setChemResultCode(chemResult);
        	 batchResult.setJudgeResultCode(judgeResult);
             batchResult.setIsNoSsc(isNoSsc);
             batchResult.setIsNoHic(isNoHic);
             batchResult.setCheckNoS(consign.getCheckNo());
             batchResult.setJudgeMaker(user);
             batchResult.setOrdLnDlyPk(consign.getOrdLnDlyPk());
             String sql = CoreXmlSqlParsersFactory.getSql("BcPhyJudgeTask.insertBcBatchResult");
             sql = EntityTool.fillSqlByEntity(sql, batchResult);
             st.executeUpdate(sql);
        }else{
	       	 batchResult.setPhyResultCode(phyResult);
	       	 batchResult.setChemResultCode(chemResult);
	       	 batchResult.setJudgeResultCode(judgeResult);
	         batchResult.setIsNoSsc(isNoSsc);
	         batchResult.setIsNoHic(isNoHic);
	         batchResult.setJudgeMaker(user);
	         if(!batchResult.getCheckNoS().contains(consign.getCheckNo())){
	        	 batchResult.setCheckNoS(batchResult.getCheckNoS() + "," + consign.getCheckNo());
	         }
	         String sql = CoreXmlSqlParsersFactory.getSql("BcPhyJudgeTask.updateBcBatchResult");
             sql = EntityTool.fillSqlByEntity(sql, batchResult);
             st.executeUpdate(sql);
        }
    }
    
    private String judge(ArrayList<QcmZcCheckconsignDetailEntity> detailGroups,
    		QcmZcCheckconsignEntity consign, String user, Statement st, Connection con)
    				throws SQLException{
		int i = 0;
		SqlBuilder b = new SqlBuilder("BcPhyJudgeTask.getMaxPhyJudgeId");
    	GenericEntity maxPk = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, GenericEntity.class);
        String phyJudgeId = maxPk.getMaxValue();
        String phyResult = "";
    	for(QcmZcCheckconsignDetailEntity detailGroup : detailGroups){
        	i++;
        	_checkNo = consign.getCheckNo();
        	_judgeStoveNo = consign.getJudgeStoveNo();
        	_batchNo = consign.getBatchNo();
        	_sampleNo = detailGroup.getSampleNo();
        	b = new SqlBuilder("BcPhyJudgeTask.QueryByBatchNoSampleNo");
        	b.Fill(new Object[]{ consign.getProcessCode(), consign.getCheckNo(), consign.getJudgeStoveNo(),
        			consign.getBatchNo(), detailGroup.getSampleNo() });
        	ArrayList<QcmJhyPhyresultEntity> jhyPhyResults = EntityTool.getEntitysBySqlNoClsCon(
        			b.toString(), con, QcmJhyPhyresultEntity.class);
        	String[] judgeResults = judge(consign, phyJudgeId, String.valueOf(i),
        			user, jhyPhyResults, con, st);
        	//写入理化判断记录表
        	QcmBcPhyjudgeEntity phyJudge = new QcmBcPhyjudgeEntity();
        	phyJudge.setBatchNo(consign.getBatchNo());
        	phyJudge.setCreateName(user);
        	phyJudge.setHeatPlanNo("");
        	phyJudge.setJfPhyResult(judgeResults[1]);
        	phyJudge.setJudgegradecode(consign.getGradecode());
        	phyJudge.setJudgeresultPhy(judgeResults[0]);
        	phyJudge.setJudgeSeq(String.valueOf(i));
        	phyJudge.setJudgesteelcode(consign.getSteelcode());
        	phyJudge.setStoveNo(consign.getStoveNo());
        	phyJudge.setJudgeStoveNo(consign.getJudgeStoveNo());
        	phyJudge.setMemo(judgeResults[2]);
        	phyJudge.setMscPline(consign.getIndexSeq());
        	phyJudge.setObjectno("");
        	phyJudge.setPhyJudgeId(phyJudgeId);
        	phyJudge.setPhyResultId(phyJudgeId);
        	phyJudge.setOrdLnDlyPk(consign.getOrdLnDlyPk());
        	phyJudge.setCraftSeq(consign.getCraftSeq());
        	phyJudge.setPic(consign.getPic());
        	phyJudge.setPlineCode(consign.getPlineCode());
        	phyJudge.setProcessCdoe(consign.getProcessCode());
        	phyJudge.setProPlanId(consign.getProOrderId() + consign.getGxPlanNo());
        	phyJudge.setSampleNo(detailGroup.getSampleNo());
        	phyJudge.setSampleId(consign.getCheckNo());
        	phyJudge.setLotNo(consign.getLotNo());
        	phyJudge.setCraftSource(consign.getCraftSource());
        	b = new SqlBuilder("BcPhyJudgeTask.updatePhyJudgeValid");
        	b.Fill(new String[]{ phyJudge.getJudgeStoveNo(), phyJudge.getBatchNo(),
        			phyJudge.getSampleNo(), phyJudge.getSampleId(), phyJudge.getProcessCdoe(), "0" });
        	st.executeUpdate(b.toString());
    		String sqlInsert = CoreXmlSqlParsersFactory.getSql("BcPhyJudgeTask.InsertPhyJudge");
            sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, phyJudge);
            st.executeUpdate(sqlInsert);
            
            if(phyResult.equals("")){
            	phyResult = judgeResults[0];
            }else if(phyResult.equals("40740201")){
            	phyResult = judgeResults[0];
            }
        }
        
        //修改理化判定结果中的汇总字段
        b = new SqlBuilder("BcPhyJudgeTask.setSampleType");
        b.Fill(new String[]{ phyJudgeId, phyResult });
        st.executeUpdate(b.toString());
        
    	return phyJudgeId;
    }
	
	@SuppressWarnings("unchecked")
	private String[] judge(QcmZcCheckconsignEntity zcCheck, String phyJudgeId, String judgeSeq,
			String user, ArrayList<QcmJhyPhyresultEntity> jhyPhyResults,
    		Connection con, Statement st) throws SQLException {
    	String nkResult = "";
    	String jfResult = "";
    	String memo = "";
    	HashMap<String, QcmBcPhyresultEntity> allResultMap = new HashMap<String, QcmBcPhyresultEntity>();
    	//判定内控
    	String craftProcess = zcCheck.getLikuProcessCode().equals("") ? zcCheck.getProcessCode() : zcCheck.getLikuProcessCode();
		SqlBuilder b = new SqlBuilder("BcPhyJudgeTask.queryCraftPhyByDlyLnPk");
		b.Fill(new String[]{ zcCheck.getOrdLnDlyPk(), zcCheck.getCraftSeq(), zcCheck.getIndexSeq(),
				craftProcess, zcCheck.getPic(), "", zcCheck.getCraftSource() });
		ArrayList<CraftOrdDesignStdPicEntity> craftPics = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignStdPicEntity.class);
		 
		 Object[] results = PhyJudgeHelper.judge(jhyPhyResults, craftPics, zcCheck.getOrdLnDlyPk(),
				 zcCheck.getCheckNo(), zcCheck.getStoveNo(), "2", con);
		 if(results[0].toString().equals("")){
			 nkResult = "40740201";
		 }else{
			 nkResult = "40740202";
			 memo = zcCheck.getPic() + "：" + results[0].toString();
		 }
		 HashMap<String, QcmBcPhyresultEntity> nkPhyResultMap = new HashMap<String, QcmBcPhyresultEntity>();
		 if(results[1] != null){
			 nkPhyResultMap = (HashMap<String, QcmBcPhyresultEntity>)results[1];
		 }
		 FillPhyResults(allResultMap, nkPhyResultMap, true);
		 
		 //获取合同行pk
		 if(zcCheck.getOrdLnDlyPk().startsWith("ZZB")){
				b = new SqlBuilder("BcPhyJudgeTask.getOrdLnPkForPln");
		}else{
				b = new SqlBuilder("BcPhyJudgeTask.getOrdLnPkByDlyPk");
		}
		b.Fill(new String[]{zcCheck.getOrdLnDlyPk()});
		SlmOrderLineEntity orderLineEntity = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderLineEntity.class);
		 //判定交付
		 b = new SqlBuilder("BcPhyJudgeTask.getCraftJfPhyByDlyLnPk");
		 //改为传合同行pk
		 //b.Fill(new String[]{ zcCheck.getOrdLnDlyPk(), zcCheck.getCraftSeq(), ""});
		 b.Fill(new String[]{ orderLineEntity.getOrdLnPk(), zcCheck.getCraftSeq(), ""});
		 ArrayList<CraftOrdDesignStdPicEntity> craftJfPics = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdPicEntity.class);
		 
		 if(craftJfPics.size() == 0){
			 jfResult = "";
		 }else{
			 Object[] jfResults = PhyJudgeHelper.judge(jhyPhyResults, craftJfPics, zcCheck.getOrdLnDlyPk(),
					 zcCheck.getCheckNo(), zcCheck.getStoveNo(), "1", con);
			 if(jfResults[0].toString().equals("")){
				 jfResult = "40740203";
			 }else{
				 jfResult = "40740204";
				 memo = getJfPics(craftJfPics) + "：" + jfResults[0].toString();
			 }
			 
			 HashMap<String, QcmBcPhyresultEntity> jfPhyResultMap = new HashMap<String, QcmBcPhyresultEntity>();
			 if(jfResults[1] != null){
				 jfPhyResultMap = (HashMap<String, QcmBcPhyresultEntity>)jfResults[1];
			 }
			 FillPhyResults(allResultMap, jfPhyResultMap, false);
		 }
		 
         
         //写入理化判定结果集表
         int i = 0;
         for(String key : allResultMap.keySet()){
        	//QcmJhyPhyresultEntity jhyPhyResult
         	i++;
         	String resultInsert = CoreXmlSqlParsersFactory.getSql("BcPhyJudgeTask.InsertPhyResult");
         	resultInsert = StringTool.ReplaceAll(resultInsert, "#PHY_RESULT_ID#", phyJudgeId);
         	resultInsert = StringTool.ReplaceAll(resultInsert, "#PHY_RESULT_SEQ#", judgeSeq);
         	resultInsert = StringTool.ReplaceAll(resultInsert, "#XH#", String.valueOf(i));
         	resultInsert = StringTool.ReplaceAll(resultInsert, "#ALLRESULT#", "");
         	resultInsert = StringTool.ReplaceAll(resultInsert, "#CREATE_NAME#", user);
         	QcmBcPhyresultEntity bcPhyResult = allResultMap.get(key);
         	
         	for(QcmJhyPhyresultEntity jhyPhyResult : jhyPhyResults){
         		String tmpStr = jhyPhyResult.getPhyCode() + jhyPhyResult.getItemCodeF()
         				+ jhyPhyResult.getItemCodeW() + jhyPhyResult.getItemCodeS();
         		if(tmpStr.equals(key)){
         			bcPhyResult.setValue1(jhyPhyResult.getValue1());
         			bcPhyResult.setValue2(jhyPhyResult.getValue2());
         			bcPhyResult.setValue3(jhyPhyResult.getValue3());
         			bcPhyResult.setValue4(jhyPhyResult.getValue4());
         			bcPhyResult.setValue5(jhyPhyResult.getValue5());
         			bcPhyResult.setValue6(jhyPhyResult.getValue6());
         			bcPhyResult.setValue7(jhyPhyResult.getValue7());
         			bcPhyResult.setValue8(jhyPhyResult.getValue8());
         			bcPhyResult.setValue9(jhyPhyResult.getValue9());
         			bcPhyResult.setValue10(jhyPhyResult.getValue10());
         			bcPhyResult.setValue11(jhyPhyResult.getValue11());
         			bcPhyResult.setValue12(jhyPhyResult.getValue12());
         			bcPhyResult.setValue13(jhyPhyResult.getValue13());
         			bcPhyResult.setValue14(jhyPhyResult.getValue14());
         			bcPhyResult.setValue15(jhyPhyResult.getValue15());
         			bcPhyResult.setAverage(jhyPhyResult.getAverage());
         			bcPhyResult.setIsGood(jhyPhyResult.getIsGood());
         			bcPhyResult.setIsDefect(jhyPhyResult.getIsDefect());
         			bcPhyResult.setCheckTemp(jhyPhyResult.getCheckTemp());
         			bcPhyResult.setHardnessType(jhyPhyResult.getHardnessType());
         			bcPhyResult.setPhyCodeDir(jhyPhyResult.getPhyCodeDir());
         			bcPhyResult.setPhyNameDir(jhyPhyResult.getPhyNameDir());
         			bcPhyResult.setSampleStyle(jhyPhyResult.getSampleStyle());
         			bcPhyResult.setSampleSize(jhyPhyResult.getSampleSize());
         			break;
         		}
         	}
         	resultInsert = EntityTool.fillSqlByEntity(resultInsert, bcPhyResult);
         	st.executeUpdate(resultInsert);
         }
    	
         return new String[]{ nkResult, jfResult, memo };
    }
	
	/**
     * 填充内控结果和交付结果
     * @param allResults
     * @param phyResults
     * @param isNk
     */
    private void FillPhyResults(HashMap<String, QcmBcPhyresultEntity> allResults,
    		HashMap<String, QcmBcPhyresultEntity> phyResults, boolean isNk){
    	if(phyResults.size() == 0) return;
    	for(String key : phyResults.keySet()){
			QcmBcPhyresultEntity phyResult = phyResults.get(key);
    		if(isNk){
    			allResults.put(key, phyResult);
    		}else{
				for(QcmBcPhyresultEntity bcPhyResult : allResults.values()){
					if(bcPhyResult.getPhyCode().equals(phyResult.getPhyCode())
							&& (phyResult.getPhyCodeMax().equals("R0002") || phyResult.getItemCodeF().equals("") || phyResult.getItemCodeF().equals(bcPhyResult.getItemCodeF()))
							&& (phyResult.getPhyCodeMax().equals("R0002") || phyResult.getItemCodeS().equals("") || phyResult.getItemCodeS().equals(bcPhyResult.getItemCodeS()))
							&& (phyResult.getPhyCodeMax().equals("R0002") || phyResult.getItemCodeW().equals("") || phyResult.getItemCodeW().equals(bcPhyResult.getItemCodeW()))){
						bcPhyResult.setJfAddCondition(phyResults.get(key).getNkAddCondition());
						bcPhyResult.setJfAddJudgeBasis(phyResults.get(key).getNkAddJudgeBasis());
						bcPhyResult.setJfAddMax(phyResults.get(key).getNkAddMax());
						bcPhyResult.setJfAddMin(phyResults.get(key).getNkAddMin());
						bcPhyResult.setJfAddQualified(phyResults.get(key).getNkAddQualified());
						bcPhyResult.setJfJudgeBasis(phyResults.get(key).getNkJudgeBasis());
						bcPhyResult.setJfMax(phyResults.get(key).getNkMax());
						bcPhyResult.setJfMin(phyResults.get(key).getNkMin());
						bcPhyResult.setJfQualified(phyResults.get(key).getNkQualified());
						bcPhyResult.setJfTolQualified(phyResults.get(key).getNkTolQualified());
					}
				}
    		}
    	}
    }
    
    /**
     * 获取交付理化索引集合
     * @param craftJfPics
     * @return
     */
    private String getJfPics(ArrayList<CraftOrdDesignStdPicEntity> craftJfPics){
    	String pics = "";
    	for(CraftOrdDesignStdPicEntity craftJfPic : craftJfPics){
    		if(pics.indexOf(craftJfPic.getPic()) == -1){
    			if(pics.equals("")){
    				pics = craftJfPic.getPic();
    			}else{
    				pics += "," + craftJfPic.getPic();
    			}
    		}
    	}
    	
    	return pics;
    }
}
