package com.steering.pss.judge.DAL;

import java.sql.Connection;
import java.util.ArrayList;
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.comm.tools.TransctionTool;
import com.steering.pss.judge.comm.ChemJudgeHelper;
import com.steering.pss.judge.model.ChemJudgeEntity;
import com.steering.pss.judge.model.QcmJhyElementsEntity;
import com.steering.pss.judge.model.QcmZgChemjudgeEntity;
import com.steering.pss.judge.model.QcmZgChemjudgeExceptionEntity;
import com.steering.pss.qcm.model.GenericEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

public class QcmZgChemJudgeDAL extends DBCommon{
	public CoreReturnObject query(String judgeStoveNo){
		String sql = CoreXmlSqlParsersFactory.getSql("QcmZgChemJudgeDAL.query");
		sql = StringTool.ReplaceAll(sql, "#JUDGE_STOVE_NO#", judgeStoveNo);
		return this.getData(sql);
	}
	
	/**
	 * 轧管成分判定
	 * @param jhyElements
	 * @param zgChemJudge
	 * @param matZcMParm
	 * @param chemResultCode
	 * @return
	 */
	public CoreReturnObject judge(ArrayList<QcmJhyElementsEntity> jhyElements, QcmZgChemjudgeEntity zgChemJudge,
			String heatPlanNo, String chemResultCode, String judgeType){
		TransctionTool transction = new TransctionTool();
		Connection con = transction.getCon();
		
		try{
			for(QcmJhyElementsEntity jhyElement : jhyElements){
				String sqlSetJudgeFlag = CoreXmlSqlParsersFactory.getSql("QcmJhyElementsDAL.updateJudgeFlag");
				sqlSetJudgeFlag = StringTool.ReplaceAll(sqlSetJudgeFlag, "#ASSAYNO#", jhyElement.getAssayno());
				if(!transction.ExcuteTransction(sqlSetJudgeFlag)){
					return transction.getReturnObject();
				}
			}
			
			//回写材料表
			SqlBuilder b = new SqlBuilder("MatZcMDAL.updateChemJudgeResult");
			b.Fill(new String[]{zgChemJudge.getJudgeresultChem(),
					zgChemJudge.getCreateName(), zgChemJudge.getJudgeStoveNo()});
			if(!transction.ExcuteTransction(b.toString())){
				return transction.getReturnObject();
			}
			
			//回写分批取样表
			if(judgeType.startsWith("3")){
				b = new SqlBuilder("PortMilBatchSampleResultDAL.updateResultChem");
				b.Fill(new String[]{zgChemJudge.getJudgeresultChem(), zgChemJudge.getJudgeStoveNo()});
				if(!transction.ExcuteTransction(b.toString())){
					return transction.getReturnObject();
				}
			}else{
				b = new SqlBuilder("PortVrpBatchSampleResultDAL.updateResultChem");
				b.Fill(new String[]{zgChemJudge.getJudgeresultChem(), zgChemJudge.getJudgeStoveNo()});
				if(!transction.ExcuteTransction(b.toString())){
					return transction.getReturnObject();
				}
			}
			
			GenericEntity maxChemJudgeId = EntityTool.getEntityBySqlNoClsCon(CoreXmlSqlParsersFactory.getSql(
					"QcmZgChemJudgeDAL.getMaxChemJudgeId"), con, GenericEntity.class);
			zgChemJudge.setChemJudgeId(maxChemJudgeId.getMaxValue());
			String sql = CoreXmlSqlParsersFactory.getSql("QcmZgChemJudgeDAL.insert");
			sql = EntityTool.fillSqlByEntity(sql, zgChemJudge);
			if(!transction.ExcuteTransction(sql)){
				return transction.getReturnObject();
			}
			
			//写入成分判定异常表
			//。。。。
			if (chemResultCode.equals("40740102"))
            {
				QcmJhyElementsDAL jhyElementsDAL = new QcmJhyElementsDAL();
				ArrayList<ChemJudgeEntity> chemJudges;
				if(judgeType.startsWith("3")){
					//传入参数应该为proPlanId 和gxPlanId
					b = new SqlBuilder("QcmJhyElementsDAL.queryRolledTubeDesignKey");
					b.Fill(new String[]{ heatPlanNo, "" });
					chemJudges = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, ChemJudgeEntity.class);
				}else{
					b = new SqlBuilder("QcmJhyElementsDAL.queryExpansionDesignKey");
					b.Fill(new String[]{ heatPlanNo});
					chemJudges = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, ChemJudgeEntity.class);
				}
				String assayNos = "";
				for(QcmJhyElementsEntity jhyElement : jhyElements){
					if(assayNos.equals("")){
						assayNos = jhyElement.getAssayno();
					}else{
						assayNos += "," + jhyElement.getAssayno();
					}
				}
                for(ChemJudgeEntity chemJudge : chemJudges)
                {
                	QcmZgChemjudgeExceptionEntity zgChemJudgeException = new QcmZgChemjudgeExceptionEntity();
                	zgChemJudgeException.setCreateName(zgChemJudge.getCreateName());
                	zgChemJudgeException.setJudgeStoveNo(zgChemJudge.getJudgeStoveNo());
                	zgChemJudgeException.setOrderisunchain("1");
                	zgChemJudgeException.setOrdLnPk("");
                	zgChemJudgeException.setPlanProorder(zgChemJudge.getJudgeProorder());
            		zgChemJudgeException.setProcessCdoe(zgChemJudge.getProcessCdoe());
                	zgChemJudgeException.setProcessDesc(zgChemJudge.getProcessDesc());
                	zgChemJudgeException.setAssayno(assayNos);

                    for(QcmJhyElementsEntity jhyElement : jhyElements)
                    {
//                        Object[] results = ChemJudgeHelper.Judge(chemJudge.getDesginKey(), chemJudge.getCic(),
//                        		chemJudge.getMscPline(), "D",
//                        		new String[]{"0", "1"}, jhyElement.getAssayno(), con);
                        Object[] results = null;
                        if (Boolean.parseBoolean(results[0].toString()))
                        {
                        	zgChemJudgeException.setJudgeresultChem("40740103");
                        }
                        else
                        {
                        	zgChemJudgeException.setJudgeresultChem("40740104");
                        	break;
                        }
                    }
                    
                    //写入轧管成分异常表
    				String sqlZgChemJudgeException = CoreXmlSqlParsersFactory.getSql("QcmZgChemjudgeExceptionDAL.Insert");
    				sqlZgChemJudgeException = StringTool.ReplaceAll(sqlZgChemJudgeException, "#CHEM_JUDGE_ID#", zgChemJudge.getChemJudgeId());
    				sqlZgChemJudgeException = EntityTool.fillSqlByEntity(sqlZgChemJudgeException, zgChemJudgeException);
    				if(!transction.ExcuteTransction(sqlZgChemJudgeException)){
    					return transction.getReturnObject();
    				}
                }
            }
			
			
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
	
	/**
	 * 轧管成分判定
	 * @param jhyElementJsons
	 * @param chemJudgeJson
	 * @param matZcMJSON
	 * @param listChemExceptionJson
	 * @param chemResultCode
	 * @return
	 */
	public CoreReturnObject judge(ArrayList<String> jhyElementJsons, String chemJudgeJson,
			String heatPlanNo, String chemResultCode, String judgeType){
		ArrayList<QcmJhyElementsEntity> jhyElements = new ArrayList<QcmJhyElementsEntity>();
    	for(String jhyElementJson : jhyElementJsons){
    		jhyElements.add(EntityTool.getEntityByJSONStr(jhyElementJson, QcmJhyElementsEntity.class));
    	}
    	QcmZgChemjudgeEntity zgChemJudge = EntityTool.getEntityByJSONStr(chemJudgeJson, QcmZgChemjudgeEntity.class);
    	return judge(jhyElements, zgChemJudge, heatPlanNo, chemResultCode, judgeType);
	}
}
