package com.steering.pss.qcm;

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

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

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

/**
 * 生产产线适用规格管理类
 * @author Administrator pdy 2014/11/01
 * @version v1.0
 */
public class CoreComMSCPlineRSpec extends DBCommon {

    /**
     * 生产产线适用规格 查询
     * @param params 查询条件集合
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject getComMSC(ArrayList<Object> params) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreComMSCPlineRSpec.QUERY");
        sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", params.get(0)
                .toString());
        sql = StringTool.ReplaceAll(sql, "#PLINE_NAME#", params.get(1)
                .toString());
        sql = StringTool.ReplaceAll(sql, "#PASS_NAME#", params.get(2)
                .toString());
        sql = StringTool.ReplaceAll(sql, "#WELL_NAME#", params.get(3)
                .toString());
        sql = StringTool.ReplaceAll(sql, "#PLINE_CODE#", params.get(4)
                .toString());
        sql = sql.replaceAll("#PLINE_CODE2#", params.get(4).toString());
        sql = StringTool.ReplaceAll(sql, "#SPEC_CODE#", params.get(5)
                .toString());
        sql = StringTool.ReplaceAll(sql, "#DIMATER#", params.get(6).toString());
        sql = StringTool.ReplaceAll(sql, "#HEIGHT#", params.get(7).toString());
        return this.getData(sql);
    }

    public CoreReturnObject getSpecName() {
        return this.getDataBySqlID("CoreComMSCPlineRSpec.GetSpec");
    }

    /**
     * 生产产线适用规格 增加
     * @param params 增加字段集合
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject addComMSC(ArrayList<String> params, ArrayList<String[]> specs) {
    	ArrayList<String> sqllist = new ArrayList<String>();
    	ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
    	String sqlGetMaxIndex = CoreXmlSqlParsersFactory.getSql("CoreComMSCPlineRSpec.getMaxIndex");
    	Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    	ArrayList<GenericEntity> listMaxIndex = EntityTool.getEntitysBySql(sqlGetMaxIndex, con, GenericEntity.class);
    	
        sqllist.add(CoreXmlSqlParsersFactory.getSql("CoreComMSCPlineRSpec.ADD"));
        params.add(listMaxIndex.get(0).getMaxValue());
        parmlist.add(params.toArray());
        
        
    	String sqlDeleteJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.deleteJgRSpec");
    	sqlDeleteJgRSpec = StringTool.ReplaceAll(sqlDeleteJgRSpec, "#CODE_JG#", listMaxIndex.get(0).getMaxValue());
    	sqllist.add(sqlDeleteJgRSpec);
        parmlist.add(null);
        
        for(String[] strs : specs){
	        String sqlInsertJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.insertJgRSpec");
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CODE_JG#", listMaxIndex.get(0).getMaxValue());
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_CODE#", strs[0]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_NAME#", strs[1]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CREATE_NAME#", strs[2]);
	        sqllist.add(sqlInsertJgRSpec);
	        parmlist.add(null);
        }
        
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, parmlist);
    }

    /**
     * 生产产线适用规格 修改
     * @param params 修改字段集合
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject updateComMSC(ArrayList<String> params, ArrayList<String[]> specs) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        sqllist.add(CoreXmlSqlParsersFactory.getSql("CoreComMSCPlineRSpec.UPDATE"));
        parmlist.add(params.toArray());
        
    	String sqlDeleteJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.deleteJgRSpec");
    	sqlDeleteJgRSpec = StringTool.ReplaceAll(sqlDeleteJgRSpec, "#CODE_JG#", params.get(params.size() - 1));
    	sqllist.add(sqlDeleteJgRSpec);
        parmlist.add(null);
        
        for(String[] strs : specs){
	        String sqlInsertJgRSpec = CoreXmlSqlParsersFactory.getSql("JGCodeManage.insertJgRSpec");
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CODE_JG#", params.get(params.size() - 1));
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_CODE#", strs[0]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#SPEC_NAME#", strs[1]);
	        sqlInsertJgRSpec = StringTool.ReplaceAll(sqlInsertJgRSpec, "#CREATE_NAME#", strs[2]);
	        sqllist.add(sqlInsertJgRSpec);
	        parmlist.add(null);
        }
        
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, parmlist);
    }

    /**
     * 生产产线适用规格 删除
     * @param params 删除字段集合
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject deleteComMSC(ArrayList<Object> params) {
        return this.setDataBySqlID("CoreComMSCPlineRSpec.DELETE",
                params.toArray());
    }

    /**
     * 生产产线适用规格 恢复
     * @param params 恢复字段集合
     * @return 返回CoreReturnObject
     */
    public CoreReturnObject resumeComMSC(ArrayList<Object> params) {
        return this.setDataBySqlID("CoreComMSCPlineRSpec.RESUME",
                params.toArray());
    }

    public CoreReturnObject getStationId(String pLineCode) {
        Object[] obj = new Object[] { pLineCode };
        return this.getDataBySqlID("CoreComMSCPlineRSpec.GetStationID", obj);
    }

    /**
     * 根据工序查找产线
     * @return obj
     */
    public CoreReturnObject getMscPlines(String process) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreComMSCPlineRSpec.getMscPlines");
        sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", process);
        return this.getData(sql);
    }

    public CoreReturnObject getProcessStyle() {
        return this.getDataBySqlID("CoreComMSCPlineRSpec.getProcessStyle");
    }

    public CoreReturnObject getModelStyle() {
        return this.getDataBySqlID("CoreComMSCPlineRSpec.getModelStyle");
    }

    public CoreReturnObject isReapt(ArrayList<Object> list) {
        Object[] obj = new Object[] { list.get(0), list.get(2), list.get(4),
                list.get(6), list.get(6), list.get(6), list.get(6),
                list.get(7), list.get(7), list.get(7), list.get(7),
                list.get(8), list.get(8), list.get(8), list.get(8),
                list.get(9), list.get(9), list.get(9), list.get(9),
                list.get(12), list.get(16), list.get(18), list.get(19),
                list.get(21) };
        return this.getDataBySqlID("CoreComMSCPlineRSpec.IsReapt", obj);
    }

    public CoreReturnObject isReaptUpdate(ArrayList<Object> list) {
        Object[] obj = new Object[] { list.get(0), list.get(2), list.get(4),
                list.get(6), list.get(6), list.get(6), list.get(6),
                list.get(7), list.get(7), list.get(7), list.get(7),
                list.get(8), list.get(8), list.get(8), list.get(8),
                list.get(9), list.get(9), list.get(9), list.get(9),
                list.get(12), list.get(16), list.get(18), list.get(19),
                list.get(21), list.get(22) };
        return this.getDataBySqlID("CoreComMSCPlineRSpec.IsReaptUpdate", obj);
    }

}
