package com.steering.pss.qcm;

import java.util.ArrayList;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;

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


/**
 * 冶金码与钢种关系维护管理
 * @author Administrator
 *
 */
public class CoreInnerMSCRGRD2 extends DBCommon {
    
    public CoreReturnObject queryMscStdCic(ArrayList<String> list){
        return getDataBySqlID("CoreInnerMSCRGRD2.QueryMscStdCic", list.toArray());
    }
    
    public CoreReturnObject queryMscStdChem(ArrayList<String> list){
        return getDataBySqlID("CoreInnerMSCRGRD2.QueryMscStdChem", list.toArray());
    }
    
    public CoreReturnObject queryMscGrade(ArrayList<String> list){
        return getDataBySqlID("CoreInnerMSCRGRD2.QueryMscGrade", list.toArray());
    }
    
    public CoreReturnObject insertMscGrade(ArrayList<ArrayList<String>> list) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.InsertMscGrade");
        for (int i = 0; i < list.size(); i++) {
            sqllist.add(sql);
            parmlist.add(list.get(i).toArray());
        }
        return getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, parmlist);
    }
    
    /**
     * 是否被使用
     * @param msc
     * @param mscPline
     * @param gradeCode
     * @return
     */
    public CoreReturnObject isUsedGrade(String msc, String mscPline, String gradeCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.isUsedGrade");
    	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
    	sql = StringTool.ReplaceAll(sql, "#MSC_PLINE#", mscPline);
    	sql = StringTool.ReplaceAll(sql, "#GRADECODE#", gradeCode);
    	
    	return this.getData(sql);
    }
    
    /**
     * 根据内控交付标准的索引码，查询所对应的标准个数。
     * @param cic 成分索引
     * @param pic 理化索引
     * @param dic 公差索引
     * @param sic 探伤索引
     * @param wic 水压索引
     * @return obj
     */
    public CoreReturnObject queryStdNkCnt(ArrayList<String> cic, ArrayList<String> pic, 
            ArrayList<String> dic, ArrayList<String> sic, ArrayList<String> wic){
        String strPic = "''";
        String strCic = "''";
        String strDic = "''";
        String strSic = "''";
        String strWic = "''";
        for(int i = 0; i < pic.size(); i++){
            strPic += ", '" + pic.get(i) + "'";
        }
        for (int i = 0; i < cic.size(); i++) {
            strCic += ", '" + cic.get(i) + "'";
        }
        for (int i = 0; i < dic.size(); i++) {
            strDic += ", '" + dic.get(i) + "'";
        }
        for (int i = 0; i < sic.size(); i++) {
            strSic += ", '" + sic.get(i) + "'";
        }
        for (int i = 0; i < wic.size(); i++) {
            strWic += ", '" + wic.get(i) + "'";
        }
        String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.queryStdNkCnt");
        sql = sql.replaceFirst(":PIC:", strPic);
        sql = sql.replaceFirst(":CIC:", strCic);
        sql = sql.replaceFirst(":DIC:", strDic);
        sql = sql.replaceFirst(":SIC:", strSic);
        sql = sql.replaceFirst(":WIC:", strWic);
        return this.getData(sql);
    }
    
    public CoreReturnObject TestEntity(GenericEntity gEntity){
    	return null;
    }
    
    /**
     * 复制交付标准
     * @param msc
     * @param stdCodeNk
     * @param stdNameNk
     * @param processCode
     * @param processName
     * @param createName
     * @return
     */
    public CoreReturnObject copyStandPay(String msc, String mscPline, String stdCodeNk,
    		String stdNameNk, String processCode, String processName, String gradeCode,
    		String gradeName, String createName){
    	TransctionTool transction = new TransctionTool();
    	try{
    		String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.copyStandPay");
        	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
        	sql = StringTool.ReplaceAll(sql, "#MSC_PLINE#", mscPline);
        	sql = StringTool.ReplaceAll(sql, "#STD_CODE_NK#", stdCodeNk);
        	sql = StringTool.ReplaceAll(sql, "#STD_NAME_NK#", stdNameNk);
        	sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", processCode);
        	sql = StringTool.ReplaceAll(sql, "#PROCESS_DESC#", processName);
        	sql = StringTool.ReplaceAll(sql, "#GRADE_CODE#", gradeCode);
        	sql = StringTool.ReplaceAll(sql, "#GRADE_NAME#", gradeName);
        	sql = StringTool.ReplaceAll(sql, "#CREATE_NAME#", createName);
        	if(!transction.ExcuteTransction(sql)){
        		CoreReturnObject cro = transction.getReturnObject();
        		if(cro.getV_errMsg().contains("2000")){
        			cro.setV_errMsg("请至少选择一个制程钢种！");
        		}
        	}
        	return transction.commit();
    	}catch(Exception ex){
    		CoreReturnObject cro = transction.rollBack();
    		cro.setV_errMsg(ex.getMessage());
    		return cro;
    	}
    }
    
    /**
     * 更新工序表中的内控索引号
     */
    public CoreReturnObject updateMscProcessNk(String msc, String mscPline, String processCode,
    		ArrayList<String> cics, ArrayList<String> pics, ArrayList<String> sics,
    		ArrayList<String> dics, ArrayList<String> wics, ArrayList<String> gics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.updateMscProcessNk");
    	String strCic = "";
    	for(String cic : cics){
    		if(strCic.equals("")){
    			strCic = cic;
    		}else{
    			strCic += "," + cic;
    		}
    		
    	}
    	
    	String strPic = "";
    	for(String pic : pics){
    		if(strPic.equals("")){
    			strPic = pic;
    		}else{
    			strPic += "," + pic;
    		}
    	}
    	
    	String strSic = "";
    	for(String sic : sics){
    		if(strSic.equals("")){
    			strSic = sic;
    		}else{
    			strSic += "," + sic;
    		}
    	}
    	
    	String strDic = "";
    	for(String dic : dics){
    		if(strDic.equals("")){
    			strDic = dic;
    		}else{
    			strDic += "," + dic;
    		}
    	}
    	
    	String strWic = "";
    	for(String wic : wics){
    		if(strWic.equals("")){
    			strWic = wic;
    		}else{
    			strWic += "," + wic;
    		}
    	}
    	
    	String strGic = "";
    	for(String gic : gics){
    		if(strGic.equals("")){
    			strGic = gic;
    		}else{
    			strGic += "," + gic;
    		}
    	}
    	
    	sql = StringTool.ReplaceAll(sql, "#CIC#", strCic);
    	sql = StringTool.ReplaceAll(sql, "#PIC#", strPic);
    	sql = StringTool.ReplaceAll(sql, "#SIC#", strSic);
    	sql = StringTool.ReplaceAll(sql, "#DIC#", strDic);
    	sql = StringTool.ReplaceAll(sql, "#WIC#", strWic);
    	sql = StringTool.ReplaceAll(sql, "#GIC#", strGic);
    	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
    	sql = StringTool.ReplaceAll(sql, "#MSC_PLINE#", mscPline);
    	sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", processCode);
    	return this.setData(sql);
    }
    
    /**
     * 查询内控适用工序
     * @param stdCode
     * @return
     */
    public CoreReturnObject getProcessByStdCode(String stdCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getProcessByStdCode");
    	sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
    	return this.getData(sql);
    }
    
    /*
     * 
     * 查询工序关联的内控索引号
     */
    public CoreReturnObject getProcessNkIndex(String msc, String mscPline, String processCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getProcessNkIndex");
    	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
    	sql = StringTool.ReplaceAll(sql, "#MSC_PLINE#", mscPline);
    	sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", processCode);
    	return this.getData(sql);
    }
    
    /**
     * 查询成分标准索引号
     * @param cics
     * @return
     */
    public CoreReturnObject getStdCic(String cics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getStdCic");
    	sql = sql.replaceAll("#CICS#", cics);
    	return this.getData(sql);
    }
    
    /**
     * 查询成理化准索引号
     * @param pics
     * @return
     */
    public CoreReturnObject getStdPic(String pics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getStdPic");
    	sql = sql.replaceAll("#PICS#", pics);
    	return this.getData(sql);
    }
    
    /**
     * 查询公差标准索引号
     * @param sics
     * @return
     */
    public CoreReturnObject getStdSic(String sics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getStdSic");
    	sql =sql.replaceAll("#SICS#", sics);
    	return this.getData(sql);
    }
    
    /**
     * 查询探伤标准索引号
     * @param dics
     * @return
     */
    public CoreReturnObject getStdDic(String dics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getStdDic");
    	sql =sql.replaceAll("#DICS#", dics);
    	return this.getData(sql);
    }
    
    /**
     * 查询水压标准索引号
     * @param wics
     * @return
     */
    public CoreReturnObject getStdWic(String wics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getStdWic");
    	sql =sql.replaceAll("#WICS#", wics);
    	return this.getData(sql);
    }
    
    /**
     * 查询加工标准索引号
     * @param gics
     * @return
     */
    public CoreReturnObject getStdGic(String gics){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreInnerMSCRGRD2.getStdGic");
    	sql =sql.replaceAll("#GICS#", gics);
    	return this.getData(sql);
    }
    
    /**
     * 获取标准通过主键
     * @param stdCode
     * @return
     */
    public CoreReturnObject getStdByPk(String stdCode){
    	SqlBuilder b = new SqlBuilder("CoreInnerMSCRGRD2.getStdByPk");
    	b.Fill(new String[]{stdCode});
    	return this.getData(b.toString());
    }
    
    public CoreReturnObject getOrderLineByPk(String ordLnPk){
    	SqlBuilder b = new SqlBuilder("CoreInnerMSCRGRD2.getOrderLineByPk");
    	b.Fill(new String[]{ordLnPk});
    	return this.getData(b.toString());
    }
    
    public CoreReturnObject getBlInfoByPk(String blCode){
    	SqlBuilder b = new SqlBuilder("CoreInnerMSCRGRD2.getBlInfoByPk");
    	b.Fill(new String[]{blCode});
    	return this.getData(b.toString());
    }
    
    /**
     * 获取相同钢种的标准数量
     * @param gradeCode
     * @return
     */
    public CoreReturnObject getSameGradeCnt(String gradeCode, String stdCodeNk){
    	SqlBuilder b = new SqlBuilder("CoreInnerMSCRGRD2.getSameGradeCnt");
    	b.Fill(new String[]{ gradeCode, stdCodeNk });
    	return this.getData(b.toString());
    }
    
    /**
     * 获取制程向下的钢种
     * @param msc
     * @param mscPline
     * @return
     */
    public CoreReturnObject getMscGrade(String msc, String mscPline){
    	SqlBuilder b = new SqlBuilder("CoreInnerMSCRGRD2.getMscGrade");
    	b.Fill(new String[]{ msc, mscPline });
    	
    	return this.getData(b.toString());
    }
}
