package com.steering.pss.judge.DAL;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

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.model.CraftInfoEntity;
import com.steering.pss.judge.model.QcmGpJudgeApplyEntity;
import com.steering.pss.judge.model.QcmJhyLgPhyresultEntity;
import com.steering.pss.judge.model.QcmLgChemjudgeEntity;
import com.steering.pss.judge.model.QcmLgJudgeEntity;
import com.steering.pss.qcm.model.GenericEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * 数据库表QCM_LG_JUDGE所对应的Java类（生成工具：代码生成工具4.0   访问地址：http://172.16.2.128/tool/）
 * @author tgcx-test   date 2016-11-04
 */
public class QcmLgJudgeDAL extends DBCommon{

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String stoveNo){
        SqlBuilder b = new SqlBuilder("QcmLgJudgeDAL.Query");
        b.AppendLn("AND T.STOVE_NO = {0}");
        b.AppendLn("ORDER BY JUDGE_ID DESC");
        b.Fill(new String[]{stoveNo});
        return this.getData(b.toString());
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String judgeId){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.QueryByPk");
        return this.getData(sqlQuery);
    }
    
//    /**
//     * 判定
//     * @param gpJudgeApplyJson
//     * @param lgJudgeJson
//     * @param chemResultCode
//     * @return
//     */
//    public CoreReturnObject JudgeSteelMaking(String lgJudgeJson, String stoveNo, 
//    		String judgeStoveNo, String plineCode, String judgeResult, String judgeResultDesc,
//    		String judgeApplayCode, String faceResult, String phyResult, String judgeType){
//    	QcmLgJudgeEntity lgJudge = EntityTool.getEntityByJSONStr(lgJudgeJson, QcmLgJudgeEntity.class);
//    	return JudgeSteelMaking(lgJudge, stoveNo, judgeStoveNo, plineCode, judgeResult,
//    			judgeResultDesc, judgeApplayCode, faceResult,
//    			phyResult, judgeType);
//    }

//    /**
//     * 判定
//     */
//    public CoreReturnObject JudgeSteelMaking(QcmLgJudgeEntity lgJudge, String stoveNo,
//    		String judgeStoveNo, String plineCode, String judgeResult, String judgeResultDesc,
//    		String judgeApplayCode, String faceResult, String phyResult, String judgeType){
//        TransctionTool transction = new TransctionTool();
//        SqlBuilder b;
//        try{
//            Connection con = transction.getCon();
//            GenericEntity maxPk = null;
//            QcmLgChemjudgeEntity lgChemJudge = EntityTool.getEntityBySqlNoClsCon(
//            		new SqlBuilder("QcmLgJudgeDAL.getChemJudge").Fill(
//            				new String[]{stoveNo}).toString(),
//            		con, QcmLgChemjudgeEntity.class);
//            if(lgChemJudge != null){
//            	lgJudge.setAssayno(lgChemJudge.getAssayno());
//            	lgJudge.setJudgeresultChem(lgChemJudge.getJudgeresultChem());
//            	lgJudge.setMemoChem(lgChemJudge.getMemo());
//            }
//            
//            //。。。。。。。写理化判定记录
//            b = new SqlBuilder("QcmGpJugdeApplyDAL.QueryByPk");
//            b.Fill(new String[]{ judgeApplayCode, stoveNo });
//            
//            QcmGpJudgeApplyEntity gpJudgeApplay = EntityTool.getEntityBySqlNoClsCon(
//            		b.toString(), con, QcmGpJudgeApplyEntity.class);
//            
//            b = new SqlBuilder("QcmGpJugdeApplyDAL.queryCraftDesignKey");
//            b.Fill(new String[]{ gpJudgeApplay.getProPlanId() });
//            CraftInfoEntity craftInfo = EntityTool.getEntityBySqlNoClsCon(
//            		b.toString(), con, CraftInfoEntity.class);
//            
//            b = new SqlBuilder("QcmJhyPhyDbResultDAL.queryCraftDbCnt");
//            b.Fill(new String[]{ craftInfo.getDesginKey(), craftInfo.getMscPline(), "B", craftInfo.getPic() });
//            GenericEntity craftDbCnt = EntityTool.getEntityBySqlNoClsCon(
//            		b.toString(), con, GenericEntity.class);
//            if(!craftDbCnt.getCount().equals("0")){
//            	QcmJhyPhyDbResultDAL jhyPhyDbResultDAL = new QcmJhyPhyDbResultDAL();
//            	String jhyLgPhyResultsSql = jhyPhyDbResultDAL.getQueryString(judgeStoveNo, craftInfo.getDesginKey(), 
//            			craftInfo.getPic(), craftInfo.getMscPline(), con);
//            	List<QcmJhyLgPhyresultEntity> jhyLgPhyResults = EntityTool.getEntitysBySqlNoClsCon(
//            			jhyLgPhyResultsSql, con, QcmJhyLgPhyresultEntity.class);
//            	
//            	
//            	GenericEntity zgPhyJudgeId = null;
//                //写入理化判断记录表
//                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZgPhyjudgeDAL.getMaxPhyJudgeId");
//                zgPhyJudgeId = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
//                String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.insertZgPhyJudge");
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#PHY_JUDGE_ID#", zgPhyJudgeId.getMaxValue());
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#PHY_RESULT_ID#", zgPhyJudgeId.getMaxValue());
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#STOVE_NO#", stoveNo);
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#JUDGE_STOVE_NO#", judgeStoveNo);
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#PLINE_CODE#", plineCode);
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#PROCESS_CDOE#", "A");
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#PROCESS_DESC#", "炼钢");
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#JUDGERESULT_PHY#", phyResult);
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#JUDGEGRADECODE#", gpJudgeApplay.getGradecode());
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#MEMO#", lgJudge.getMemo());
//                sqlInsert = StringTool.ReplaceAll(sqlInsert, "#CREATE_NAME#", lgJudge.getCreateName());
//                if(!transction.ExcuteTransction(sqlInsert)){
//                    return transction.rollBack();
//                }
//                
//                //写入理化判定结果集表
//                int i = 0;
//            	sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZgPhyjudgeDAL.getMaxPhyResultId");
//                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
//                for(QcmJhyLgPhyresultEntity jhyLgPhyResult : jhyLgPhyResults){
//                	if(jhyLgPhyResult.getSampleId().equals("")) continue;
//                	i++;
//                	String sqlZgPhyResultInsert = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.insertZgPhyResult");
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#PHY_RESULT_ID#", maxPk.getMaxValue());
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#PHY_RESULT_SEQ#", String.valueOf(i));
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#STOVE_NO#", stoveNo);
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#JUDGE_STOVE_NO#", judgeStoveNo);
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#PHY_CODE#", jhyLgPhyResult.getPhyCode());
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#PHY_NAME#", jhyLgPhyResult.getPhyName());
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#VALUE1#", jhyLgPhyResult.getValue1());
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#SAMPLE_ID#", jhyLgPhyResult.getSampleId());
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#XH#", String.valueOf(i));
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#INDEX_SEQ#", String.valueOf(i));
//                	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#CREATE_NAME#", lgJudge.getCreateName());
//                	if(!transction.ExcuteTransction(sqlZgPhyResultInsert)){
//                        return transction.rollBack();
//                    }
//                }
//            }
//            
//            //。。。。。。。写表面检验记录
////            QcmZgPhyjudgeEntity lgPhyJudge = EntityTool.getEntityBySqlNoClsCon(
////            		new SqlBuilder("QcmLgJudgeDAL.getPhyJudge").Fill(
////            				new String[]{stoveNo}).toString(), 
////            		con, QcmZgPhyjudgeEntity.class);
////            if(lgPhyJudge != null){
////            	lgJudge.setPhyResultId(lgPhyJudge.getPhyResultId());
////            	lgJudge.setJudgeresultPhy(lgPhyJudge.getJudgeresultPhy());
////            	lgJudge.setMemoPhy(lgPhyJudge.getMemo());
////            }
//            lgJudge.setStoveNo(stoveNo);
//            lgJudge.setJudgeStoveNo(judgeStoveNo);
//            lgJudge.setPlineCode(plineCode);
//            lgJudge.setProcessCdoe("A");
//            lgJudge.setProcessDesc("炼钢");
//            lgJudge.setJudgeresultPhy(phyResult);
//            lgJudge.setJudgeresultFace(faceResult);
//            
//            
//            String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.GetMaxPk");
//            maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
//            lgJudge.setJudgeId(maxPk.getMaxValue());
//            String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.Insert");
//            sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, lgJudge);
//            if(!transction.ExcuteTransction(sqlInsert)){
//                return transction.rollBack();
//            }
//			
//			//回写管坯申请判定表
//            b = new SqlBuilder("QcmGpJugdeApplyDAL.setJudgeResult");
//            b.Fill(new String[]{ judgeApplayCode, stoveNo, judgeResult, judgeResultDesc, 
//            		lgJudge.getCreateName(), faceResult, phyResult });
//            if(!transction.ExcuteTransction(b.toString())){
//                return transction.rollBack();
//            }
//            
//            //回写炼钢、锻造生产物料主表
//            b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpM");
//            b.Fill(new String[]{ judgeResult, lgJudge.getCreateName(), "1", judgeApplayCode });
//            if(!transction.ExcuteTransction(b.toString())){
//                return transction.rollBack();
//            }
//        }catch(Exception ex){
//            CoreReturnObject cro = transction.rollBack();
//            cro.setV_errMsg(ex.getMessage());
//            return cro;
//        }
//        return transction.commit();
//    }
    
    public static ResultSet testQuery(String id, Statement st) throws SQLException{
    	return st.executeQuery("");
    }
    
    /**
     * 新增
     * @throws SQLException 
     */
    public static void insert(QcmLgJudgeEntity lgjudge, Statement st) throws SQLException{
    	GenericEntity maxPk = null;
        String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.GetMaxPk");
        sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, lgjudge);
        maxPk = EntityTool.getEntityByRs(st.executeQuery(sqlMaxPk), GenericEntity.class);
        lgjudge.setJudgeId(maxPk.getMaxValue());
        String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmLgJudgeDAL.Insert");
        sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, lgjudge);
        st.executeUpdate(sqlInsert);
    }
}
