package com.steering.pss.judge.DAL;

import java.sql.Connection;
import java.util.ArrayList;

import com.google.gson.reflect.TypeToken;
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.QcmJhyPhyresultEntity;
import com.steering.pss.judge.model.QcmZgPhyjudgeEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

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

// /**
//     * 通用查询
//     */
//    public CoreReturnObject Query(String stoveNo, String judgeResultCode,
//    		String timeBegin, String timeEnd, Integer judgeType){
//    	String processCode = "";
//    	if(judgeType.toString().startsWith("1")){
//    		processCode = "A";
//    	}else if(judgeType.toString().startsWith("2")){
//    		processCode = "B";
//    	}else if(judgeType.toString().startsWith("3")){
//    		processCode = "D";
//    	}else if(judgeType.toString().startsWith("4")){
//    		processCode = "E";
//    	}
//    	String strJudgeType = judgeType.toString();
//    	SqlBuilder b = new SqlBuilder("QcmZgPhyjudgeDAL.Query");
//    	b.AppendLn("AND T.STOVE_NO LIKE '%'||{1}||'%'", (strJudgeType.startsWith("1") || strJudgeType.startsWith("2")) && !stoveNo.equals(""));
//    	b.AppendLn("AND T.JUDGE_STOVE_NO LIKE '%'||{1}||'%'", !strJudgeType.startsWith("1") && !strJudgeType.startsWith("2") && !stoveNo.equals(""));
//    	b.AppendLn("AND T.JUDGERESULT_PHY = {2}", !judgeResultCode.equals(""));
//    	b.AppendLn("AND T.CREATE_TIME >= TO_DATE({3}, 'YYYY-MM-DD HH24:MI:SS')", !timeBegin.equals(""));
//    	b.AppendLn("AND T.CREATE_TIME <= TO_DATE({4}, 'YYYY-MM-DD HH24:MI:SS')", !timeEnd.equals(""));
//    	b.AppendLn("ORDER BY T.PHY_JUDGE_ID DESC");
//    	b.Fill(new String[]{ processCode, stoveNo, judgeResultCode, timeBegin, timeEnd });
//        return this.getData(b.toString());
//    }
//
//    /**
//     * 通过主键查询
//     */
//    public CoreReturnObject QueryByPk(String phyJudgeId){
//        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZgPhyjudgeDAL.QueryByPk");
//        return this.getData(sqlQuery);
//    }
//    
//    public CoreReturnObject judge(String jhyPhyResultJsons, String zgPhyJudgeJson,
//    		String judgeApplyCode, String batchNo, String judgeType){
//    	ArrayList<QcmJhyPhyresultEntity> jhyPhyResults = new ArrayList<QcmJhyPhyresultEntity>();
//    	jhyPhyResults = EntityTool.getEntitysByGson(jhyPhyResultJsons,
//    			new TypeToken<ArrayList<QcmJhyPhyresultEntity>>(){}.getType());
//		QcmZgPhyjudgeEntity zgPhyJudge = EntityTool.getEntityByJSONStr(zgPhyJudgeJson, QcmZgPhyjudgeEntity.class);
//		return judge(jhyPhyResults, zgPhyJudge, judgeApplyCode, batchNo, judgeType);
//    }
//
//    /**
//     * 判定
//     */
//    public CoreReturnObject judge(ArrayList<QcmJhyPhyresultEntity> jhyPhyResults,
//    		QcmZgPhyjudgeEntity zgPhyJudge, String judgeApplyCode, String batchNo,
//    		String judgeType){
//        TransctionTool transction = new TransctionTool();
//        try{
//            Connection con = transction.getCon();
//            GenericEntity maxPk = null;
//            //写入理化判断记录表
//            String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZgPhyjudgeDAL.getMaxPhyJudgeId");
//            maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
//            zgPhyJudge.setPhyJudgeId(maxPk.getMaxValue());
//            zgPhyJudge.setPhyResultId(maxPk.getMaxValue());
//            String sqlInsert = CoreXmlSqlParsersFactory.getSql("QcmZgPhyjudgeDAL.Insert");
//            sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, zgPhyJudge);
//            if(!transction.ExcuteTransction(sqlInsert)){
//                return transction.rollBack();
//            }
//            
//            //写入理化判定结果集表
//            int i = 0;
//        	sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZgPhyjudgeDAL.getMaxPhyResultId");
//            maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
//            for(QcmJhyPhyresultEntity jhyPhyResult : jhyPhyResults){
//            	i++;
//            	String sqlZgPhyResultInsert = CoreXmlSqlParsersFactory.getSql("QcmZgPhyresultDAL.Insert");
//            	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#PHY_RESULT_ID#", maxPk.getMaxValue());
//            	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#PHY_RESULT_SEQ#", String.valueOf(i));
//            	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#OBJECTNO#", "");
//            	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#XH#", String.valueOf(i));
//            	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#ALLRESULT#", "");
//            	sqlZgPhyResultInsert = StringTool.ReplaceAll(sqlZgPhyResultInsert, "#CREATE_NAME#", zgPhyJudge.getCreateName());
//            	sqlZgPhyResultInsert = EntityTool.fillSqlByEntity(sqlZgPhyResultInsert, jhyPhyResult);
//            	if(!transction.ExcuteTransction(sqlZgPhyResultInsert)){
//                    return transction.rollBack();
//                }
//            }
//            
//            //回写材料表
//			SqlBuilder b = new SqlBuilder("MatZcMDAL.updatePhyJudgeResult");
//			b.Fill(new String[]{zgPhyJudge.getJudgeresultPhy(),
//					zgPhyJudge.getCreateName(), zgPhyJudge.getJudgeStoveNo()});
//			if(!transction.ExcuteTransction(b.toString())){
//				return transction.getReturnObject();
//			}
//            
//            if(judgeType.startsWith("1")){
//            	//回写炼钢炉次订单分配表  
//    			b = new SqlBuilder("PlnSteelforOrdDAL.setPhyJudgeId");
//    			b.Fill(new String[]{ zgPhyJudge.getPhyJudgeId(), zgPhyJudge.getJudgeStoveNo() });
//    			if(!transction.ExcuteTransction(b.toString())){
//    				return transction.getReturnObject();
//    			}
//            }else if(judgeType.startsWith("3")){
//				b = new SqlBuilder("QcmZgJugdeApplyDAL.updatePhyResult");
//				b.Fill(new String[]{zgPhyJudge.getJudgeresultPhy(), judgeApplyCode,
//						zgPhyJudge.getJudgeStoveNo(), batchNo});
//				if(!transction.ExcuteTransction(b.toString())){
//					return transction.getReturnObject();
//				}
//			}else{
//				b = new SqlBuilder("PortVrpBatchSampleResultDAL.updateResultPhy");
//				b.Fill(new String[]{zgPhyJudge.getJudgeresultPhy(), zgPhyJudge.getJudgeStoveNo()});
//				if(!transction.ExcuteTransction(b.toString())){
//					return transction.getReturnObject();
//				}
//			}
//            
//        }catch(Exception ex){
//            CoreReturnObject cro = transction.rollBack();
//            cro.setV_errMsg(ex.getMessage());
//            return cro;
//        }
//        return transction.commit();
//    }
}
