package com.steering.jobface.qcm.task;

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

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.model.qcm.CraftOrdDesignStdCicEntity;
import com.steering.model.qcm.GenericEntity;
import com.steering.model.qcm.QcmBcChemResultEntity;
import com.steering.model.qcm.QcmBcChemjudgeEntity;
import com.steering.model.qcm.QcmJhyElementsEntity;
import com.steering.model.qcm.QcmZcCheckconsignEntity;
public class BcChemJudgeTask {
	/**
	 * 工序预判定
	 * @param stoveNo
	 * @param judgeStoveNo
	 * @param cic
	 * @param proPlanId
	 * @param gxPlanNo
	 * @throws SQLException
	 */
	public static String[] judge(QcmZcCheckconsignEntity zcCheck,
			List<QcmJhyElementsEntity> jhyElements, ArrayList<CraftOrdDesignStdCicEntity> craftCics,
			Statement st, Connection qcmCon) throws SQLException{
		String[] results = new String[]{ "", "" };
		SqlBuilder b;
		
		b = new SqlBuilder("BcChemJudgeTask.getMaxChemJudgeId");
		GenericEntity maxChemJudgeId = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), qcmCon, GenericEntity.class);
		String judgeId = maxChemJudgeId.getMaxValue();
		
		String judgeResult = judge(zcCheck, jhyElements, craftCics, judgeId, st, qcmCon);
		
		//根据成分判定ID修改汇总成分结果、取样类型。
		b = new SqlBuilder("BcChemJudgeTask.setSampleType");
		b.Fill(new String[]{ judgeId, judgeResult });
		//st.addBatch(b.toString());
		//st.executeBatch();
		st.executeUpdate(b.toString());
		
		results[0] = judgeId;
		results[1] = judgeResult;
		return results;
	}
	
	private static String judge(QcmZcCheckconsignEntity zcCheck, List<QcmJhyElementsEntity> jhyElements,
			ArrayList<CraftOrdDesignStdCicEntity> craftCics, String judgeId,
			Statement st, Connection con)
			throws SQLException{
		SqlBuilder b;
		ArrayList<String> pChemResults = new ArrayList<String>();
		ArrayList<String> jfChemResults = new ArrayList<String>();
		if(jhyElements.size() == 0){
			return "";
		}
		
		int judgeSeq = 0;
		for(QcmJhyElementsEntity jhyElement : jhyElements){
			judgeSeq++;
			ArrayList<String> cChemResults = new ArrayList<String>();
			//判定炉号涉及到的合同都需要判定
			String memo = "";
			HashMap<String, String[]> jhyResults = new HashMap<String, String[]>();

			Object[] results = ChemJudgeHelper.Judge(jhyElement, craftCics, null, true);
			//添加成分元素
			if(results[1] != null){
				@SuppressWarnings("unchecked")
				HashMap<String, String[]> judgeJhyResults = (HashMap<String, String[]>)results[1];
				ChemJudgeHelper.mergeChemResult(judgeJhyResults, jhyResults);
			}
			if(!results[0].toString().equals("")){
				cChemResults.add("40740102");
				pChemResults.add("40740102");
				if(memo.equals("")){
					memo = craftCics.get(0).getCic() + "：" + results[0].toString();
				}else{
					memo += "\r\n" + craftCics.get(0).getCic() + "：" + results[0].toString();
				}
			}else{
				cChemResults.add("40740101");
				pChemResults.add("40740101");
			}
			
			ArrayList<QcmBcChemResultEntity> bcChemResults = FillBcChemResultEntity(
					jhyResults, judgeId, String.valueOf(judgeSeq));
			
			//*****************判定交付标准*******************
			/*b = new SqlBuilder("BcChemJudgeTask.getCraftJfChemByDlyLnPk");
			b.Fill(new String[]{ zcCheck.getOrdLnDlyPk(), zcCheck.getCraftSeq(), zcCheck.getIndexSeq(),
					"A", zcCheck.getCic() });*/
			//*****************判定交付标准*******************
			//2020-07-23 增加氧氮氢相关，熔炼判定取内控，成品判定取交付
			if(zcCheck.getOrdLnDlyPk().length()<3){
				return "";
			}
			if("A".equals(zcCheck.getProcessCode())){
				b = new SqlBuilder("BcChemJudgeTask.getCraftJfChemByOrdLnPkMaking");
				b.Fill(new String[]{ zcCheck.getOrdLnDlyPk(), zcCheck.getCraftSeq(), zcCheck.getCic() });
			}
			else{
				b = new SqlBuilder("BcChemJudgeTask.getCraftJfChemByOrdLnPk");
				b.Fill(new String[]{ zcCheck.getOrdLnDlyPk(), zcCheck.getCraftSeq(),
						zcCheck.getIndexSeq(), zcCheck.getProcessCode(), zcCheck.getCic() });
			}
			ArrayList<CraftOrdDesignStdCicEntity> craftJfCics = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, CraftOrdDesignStdCicEntity.class);
			results = ChemJudgeHelper.Judge(jhyElement, craftJfCics, null, true);
			//添加成分元素
			if(results[1] != null){
				@SuppressWarnings("unchecked")
				HashMap<String, String[]> judgeJhyResults = (HashMap<String, String[]>)results[1];
				FillBcChemResultEntity(judgeJhyResults, judgeId, String.valueOf(judgeSeq), bcChemResults);
			}
			String jfChemResult = "";
			if(!results[0].toString().equals("")){
				jfChemResults.add("40740104");
				jfChemResult = "40740104";
			}else{
				jfChemResults.add("40740103");
				jfChemResult = "40740103";
			}
			
			QcmBcChemjudgeEntity bcChemJudge = new QcmBcChemjudgeEntity();
			if(cChemResults.contains("40740102")){
				bcChemJudge.setJudgeresultChem("40740102");
			}else{
				bcChemJudge.setJudgeresultChem("40740101");
			}
			bcChemJudge.setJfChemResult(jfChemResult);
			bcChemJudge.setChemJudgeId(judgeId);
			bcChemJudge.setLotNo(zcCheck.getLotNo());
			bcChemJudge.setStoveNo(zcCheck.getStoveNo());
			bcChemJudge.setJudgeStoveNo(zcCheck.getJudgeStoveNo());
			bcChemJudge.setBatchNo(zcCheck.getBatchNo());
			bcChemJudge.setPlineCode(zcCheck.getPlineCode());
			bcChemJudge.setProcessCdoe(zcCheck.getProcessCode());
			bcChemJudge.setOrderisunchain("0");
			bcChemJudge.setAssayno(jhyElement.getAssayno());
			bcChemJudge.setCreateName("SYSTEM");
			bcChemJudge.setOrdLnDlyPk(zcCheck.getOrdLnDlyPk());
			bcChemJudge.setCraftSeq(zcCheck.getCraftSeq());
			bcChemJudge.setMscPline(zcCheck.getIndexSeq());
			bcChemJudge.setCic(zcCheck.getCic());
			bcChemJudge.setMemo(memo);
			bcChemJudge.setSampleId(jhyElement.getSampleId());
			bcChemJudge.setSampleNo(jhyElement.getSampleNo());
			bcChemJudge.setJudgeSeq(String.valueOf(judgeSeq));
			//bcChemJudge.setProPlanId();
			bcChemJudge.setOldGradecode("");
			bcChemJudge.setOldGradename("");
			bcChemJudge.setOldCic("");
			bcChemJudge.setJudgegradecode(zcCheck.getGradecode());
			bcChemJudge.setJudgesteelcode(zcCheck.getSteelcode());
			bcChemJudge.setCraftSource(zcCheck.getCraftSource());
			//bcChemJudge.setJugdeApplyCode(zcCheck.getJugdeApplyCode());
			//作废已存判的样管
			b = new SqlBuilder("BcChemJudgeTask.setValidflag");
			b.Fill(new String[]{ bcChemJudge.getJudgeStoveNo(), bcChemJudge.getBatchNo(),
					bcChemJudge.getSampleId(), bcChemJudge.getSampleNo(),
					bcChemJudge.getProcessCdoe(), "0" });
			//st.addBatch(b.toString());
			st.executeUpdate(b.toString());
			String sql = CoreXmlSqlParsersFactory.getSql("BcChemJudgeTask.insertChemJudge");
			sql = EntityTool.fillSqlByEntity(sql, bcChemJudge);
			//st.addBatch(sql);
			st.executeUpdate(sql);
			
			//保存成分判定明细（包含复合元素）
			for(QcmBcChemResultEntity bcChemResult : bcChemResults){
				String insertBcChemResult = CoreXmlSqlParsersFactory.getSql("BcChemJudgeTask.insertChemResult");
				insertBcChemResult = EntityTool.fillSqlByEntity(insertBcChemResult, bcChemResult);
				//st.addBatch(insertBcChemResult);
				st.executeUpdate(insertBcChemResult);
			}
		}
		
		if(jfChemResults.contains("40740104")){
			return "40740104";
		}else{
			if(pChemResults.contains("40740102")){
				return "40740102";
			}else{
				return "40740101";
			}
		}
	}
	
	/**
     * 填充半成品成分内控明细
     * @param jhyResults
     * @param chemJudgeId
     * @param judgeSeq
     * @return
     */
    private static ArrayList<QcmBcChemResultEntity> FillBcChemResultEntity(
    		HashMap<String, String[]> jhyResults, String chemJudgeId, String judgeSeq){
    	int chemSeq = 0;
    	ArrayList<QcmBcChemResultEntity> bcChemResults = new ArrayList<QcmBcChemResultEntity>();
		for(String key : jhyResults.keySet()){
			chemSeq++;
			QcmBcChemResultEntity bcChemResult = new QcmBcChemResultEntity();
			bcChemResult.setChemJudgeId(chemJudgeId);
			bcChemResult.setJudgeSeq(judgeSeq);
			bcChemResult.setChemSeq(String.valueOf(chemSeq));
			bcChemResult.setChemName(key);
			bcChemResult.setChemValue(jhyResults.get(key)[0]);
			bcChemResult.setNkMin(jhyResults.get(key)[1]);
			bcChemResult.setNkMax(jhyResults.get(key)[2]);
			bcChemResult.setNkQualified(jhyResults.get(key)[3]);
			bcChemResult.setUnqualifiedType(jhyResults.get(key)[4]);
			bcChemResults.add(bcChemResult);
		}
		
		return bcChemResults;
    }
    
    /**
     * 填充半成品交付明细
     * @param jhyResults
     * @param chemJudgeId
     * @param judgeSeq
     * @param bcChemResults
     */
    private static void FillBcChemResultEntity(
    		HashMap<String, String[]> jhyResults, String chemJudgeId, String judgeSeq,
    		ArrayList<QcmBcChemResultEntity> bcChemResults){
    	for(String key : jhyResults.keySet()){
    		boolean exists = false;
    		for(QcmBcChemResultEntity bcChemResult : bcChemResults){
    			if(bcChemResult.getChemName().equals(key)){
    				exists = true;
    				bcChemResult.setJfMin(jhyResults.get(key)[1]);
    				bcChemResult.setJfMax(jhyResults.get(key)[2]);
    				bcChemResult.setJfQualified(jhyResults.get(key)[3]);
    				break;
    			}
    		}
    		if(!exists){
    			QcmBcChemResultEntity bcChemResult = new QcmBcChemResultEntity();
    			bcChemResult.setChemJudgeId(chemJudgeId);
    			bcChemResult.setJudgeSeq(judgeSeq);
    			bcChemResult.setChemSeq(String.valueOf(bcChemResults.size() + 1));
    			bcChemResult.setChemName(key);
    			bcChemResult.setChemValue(jhyResults.get(key)[0]);
    			bcChemResult.setJfMin(jhyResults.get(key)[1]);
    			bcChemResult.setJfMax(jhyResults.get(key)[2]);
    			bcChemResult.setJfQualified(jhyResults.get(key)[3]);
    			bcChemResult.setUnqualifiedType(jhyResults.get(key)[4]);
    			bcChemResults.add(bcChemResult);
    		}
		}
    }
}
