package com.steering.pss.judge.DAL;

import java.sql.Connection;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.qcm.model.SlmOrderLineEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SC01.CoreConstantData;

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

    /**
     * 通用查询
     */
    public CoreReturnObject Query(String stoveNo, Integer judgeType){
    	SqlBuilder b = new SqlBuilder("QcmJhyPhyresultDAL.Query");
    	String strJudgeType = judgeType.toString();
    	if(strJudgeType.endsWith("3")){
    		if(strJudgeType.startsWith("1")){
    			b = new SqlBuilder("QcmJhyPhyresultDAL.QueryLgColligate");
    		}else if(strJudgeType.startsWith("3")){
    			b = new SqlBuilder("QcmJhyPhyresultDAL.QueryZgColligate");
    		}
    	}else{
    		b = new SqlBuilder("QcmJhyPhyresultDAL.Query");
    	}
    	String processCode = "";
		if(strJudgeType.startsWith("1")){
			processCode = "A";
		}else if(strJudgeType.startsWith("2")){
			processCode = "B";
		}else if(strJudgeType.startsWith("3")){
			processCode = "D";
		}else if(strJudgeType.startsWith("4")){
			processCode = "E";
		}
		b.AppendLn("AND T.JUDGE_STOVE_NO = {1}", !strJudgeType.startsWith("1") && !strJudgeType.startsWith("2"));
		b.AppendLn("AND T.STOVE_NO = {1}", strJudgeType.startsWith("1") || strJudgeType.startsWith("2"));
		b.AppendLn("ORDER BY T.PHY_NAME_MAX, T.PHY_NAME_MIN, T.PHY_NAME, T.SAMPLE_TYPE", !strJudgeType.endsWith("3"));
		b.AppendLn(" ORDER BY A.PHY_NAME_MAX, A.PHY_NAME_MIN, A.PHY_NAME", strJudgeType.endsWith("3"));
        b.Fill(new String[]{ processCode, stoveNo });
        return this.getData(b.toString());
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String sampleId, String sampleNo, String indexSeq){
        return this.getData(QueryByPkSql(sampleId, sampleNo, indexSeq));
    }
    
    public String QueryByPkSql(String sampleId, String sampleNo, String indexSeq){
    	return new SqlBuilder("QcmJhyPhyresultDAL.QueryByPk").Fill(
        		new String[]{sampleId, sampleNo, indexSeq}).toString();
    }
    
    /**
     * 查询理化工艺评审记录
     * @param designKey
     * @param pic
     * @param specFlags
     * @return
     */
    public CoreReturnObject queryCraftResultPic(String designKey, String mscPline, 
    		String judgeType, String pic, String[] specFlags){
    	return this.getData(queryCraftResultPicSql(designKey, mscPline, judgeType, pic, specFlags));
    }
    
    public String queryCraftResultPicSql(String designKey, String mscPline, String judgeType,
    		String pic, String[] specFlags){
    	SqlBuilder b = new SqlBuilder("QcmJhyPhyresultDAL.queryCraftResultPic");
    	b.AppendLn(CoreConverter.toSqlConditionString("SPECL_FL", specFlags), specFlags.length > 0);
		//内控有可能有多套， 但只能取一套。
    	String process = "";
    	if(judgeType.startsWith("1")){
    		process = "A";
    	}else if(judgeType.startsWith("2")){
    		process = "B";
    	}else if(judgeType.startsWith("3")){
    		process = "D";
    	}else if(judgeType.startsWith("4")){
    		process = "E";
    	}
    	
    	b.AppendLn("AND DECODE(SPECL_FL, '2', MSC_PLINE, 1) = DECODE(SPECL_FL, '2', {1}, 1)", !mscPline.equals(""));
    	b.AppendLn("AND DECODE(SPECL_FL, '2', PROCESS_CODE, 1) = DECODE(SPECL_FL, '2', {2}, 1)", !process.equals(""));
		b.AppendLn("AND DECODE(SPECL_FL, '2', PIC, 1) = DECODE(SPECL_FL, '2', {3}, 1)", !pic.equals(""));
		b.Fill(new String[]{designKey, mscPline, process, pic});
		return b.toString();
    }
    
    /**
     * 通过主键查询合同行
     * @param ordLnPk
     * @return
     */
    public SlmOrderLineEntity queryOrderLineByPk(String ordLnPk){
    	SqlBuilder b = new SqlBuilder("QcmJhyPhyresultDAL.queryOrderLineByPk");
    	b.Fill(new String[]{ordLnPk});
    	Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    	return EntityTool.getEntityBySql(b.toString(), con, SlmOrderLineEntity.class);
    }
    
    /**
     * 通过主键查询合同行
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject queryOrderLineByPk2(String ordLnPk)
    {
    	SqlBuilder b = new SqlBuilder("QcmJhyPhyresultDAL.queryOrderLineByPk");
    	b.Fill(new String[]{ordLnPk});
    	return this.getData(b.toString());
    }
    
    /**
     * 通过炉号信息查询最大的C元素值
     * @param stoveNo
     * @return
     */
    public String getMaxCByStoveNo(String stoveNo, String judgeType){
    	SqlBuilder b = new SqlBuilder("QcmJhyPhyresultDAL.getMaxCByStoveNo");
    	b.AppendLn("AND T.STOVE_NO = {0}", judgeType.startsWith("1") || judgeType.startsWith("2"));
    	b.AppendLn("AND T.JUDGE_STOVE_NO = {0}", judgeType.startsWith("3"));
    	b.Fill(new String[]{stoveNo});
    	Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    	return EntityTool.getEntityBySql(b.toString(), con, GenericEntity.class).getMaxValue();
    }
    
    /**
     * 通过炉号信息查询最大的C元素值
     * @param stoveNo
     * @return
     */
    public CoreReturnObject getMaxCByStoveNo2(String stoveNo, String judgeType){
    	
    	SqlBuilder b = new SqlBuilder("QcmJhyPhyresultDAL.getMaxCByStoveNo");
    	b.AppendLn("AND T.STOVE_NO = {0}", judgeType.startsWith("1") || judgeType.startsWith("2"));
    	b.AppendLn("AND T.JUDGE_STOVE_NO = {0}", judgeType.startsWith("3"));
    	b.Fill(new String[]{stoveNo});
    	return this.getData(b.toString());
    }
}
