package com.steering.pss.qcm;

import java.util.ArrayList;

import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.pss.qcm.model.BaseStdObject;
import com.steering.pss.qcm.model.StdCusterObject;

/**
 * 标准管理 (不包含订货标准)
 * @author Administrator PDY 2015/01/26
 */
public class ComStandard extends DBCommon {

    /**
     * 标准基础 查询
     * @param list 查询条件
     * @return 数据集
     */
    public CoreReturnObject getComStandard(ArrayList<String> list) {
        return this.getDataBySqlID("CoreStandard.query", list.toArray());
    }
    
 

    /**
     * 最终客户 查询
     * @param stdCode 标准代码
     * @param valid 有效标识
     * @return 数据集
     */
    public CoreReturnObject getCuster(String stdCode, String valid) {
        Object[] params = new Object[] { stdCode, valid };
        return this.getDataBySqlID("CoreStandard.queryCust", params);
    }

    /**
     * 标准基础 保存
     * @param stdData 标准数据
     * @param custData 客户数据
     * @return 返回执行条数
     */
    public CoreReturnObject saveComStandard(ArrayList<Object> stdData,
            ArrayList<Object> custData) {
        ArrayList<String> sqlIdList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        // 标准基础
        for (int i = 0; i < stdData.size(); i++) {
            JSONObject json = JSONObject.fromObject(stdData.get(i).toString());
            BaseStdObject com = (BaseStdObject) JSONObject.toBean(json,
                    BaseStdObject.class);
            Object[] param = null;
            if (com.getValidflag().equals("")) {
                param = new Object[] { com.getStdName(), com.getStdStyle(),
                        com.getStdNote(), com.getStdStyleCode(),
                        com.getStdStyleName(), com.getStdVersion(),
                        com.getProcessCode(), com.getProcessDesc(),
                        com.getDepartmentCode(), com.getDepartmentDesc(),
                        com.getUnitCode(), com.getUnitDesc(),
                        com.getCreateName(), com.getMemo(), com.getAcceptBid(),
                        com.getPurchaseStd()};
                sqlIdList.add("CoreStandard.add");
                params.add(param);
            }
            else {
                param = new Object[] { com.getStdCode(), com.getStdName(),
                        com.getStdStyle(), com.getStdNote(),
                        com.getStdStyleCode(), com.getStdStyleName(),
                        com.getStdVersion(), com.getProcessCode(),
                        com.getProcessDesc(), com.getDepartmentCode(),
                        com.getDepartmentDesc(), com.getUnitCode(),
                        com.getUnitDesc(), com.getUpdateName(), com.getMemo(),
                        com.getAcceptBid(), com.getPurchaseStd(),
                        com.getStdCode() };
                sqlIdList.add("CoreStandard.update");
                params.add(param);
            }
        }
        // 客戶
        for (int i = 0; i < custData.size(); i++) {
            JSONObject json = JSONObject.fromObject(custData.get(i).toString());
            StdCusterObject com = (StdCusterObject) JSONObject.toBean(json,
                    StdCusterObject.class);
            Object[] param = null;
            if (com.getValidflag().equals("")) {
                param = new Object[] { com.getStdCode(), com.getStdName(),
                        com.getCustCode(), com.getCustName(),
                        com.getCreateName(), com.getMemo() };
                sqlIdList.add("CoreStandard.addCust");
            }
            else {
                param = new Object[] { com.getStdCode(), com.getStdName(),
                        com.getCustCode(), com.getCustName(),
                        com.getCreateName(), com.getMemo(), com.getStdCode(),
                        com.getCustCode() };
                sqlIdList.add("CoreStandard.updateCust");
            }
            params.add(param);
        }
        return this.setDataById(sqlIdList, params);
    }

    /**
     * 标准基础 作废或者恢复
     * @param stdData 标准数据
     * @param custData 客户数据
     * @param flag 有效标识
     * @return 执行条数
     */
    public CoreReturnObject deleteOrResume(ArrayList<Object> stdData,
            ArrayList<Object> custData, Boolean flag) {
        ArrayList<String> sqlIdList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        for (int i = 0; i < stdData.size(); i++) {
            JSONObject json = JSONObject.fromObject(stdData.get(i).toString());
            BaseStdObject com = (BaseStdObject) JSONObject.toBean(json,
                    BaseStdObject.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] { com.getDeleteName(), com.getStdCode() };
                sqlIdList.add("CoreStandard.delete");
            }
            else {
                param = new Object[] { "", com.getStdCode() };
                sqlIdList.add("CoreStandard.resume");
            }
            params.add(param);
        }
        for (int i = 0; i < custData.size(); i++) {
            JSONObject json = JSONObject.fromObject(custData.get(i).toString());
            StdCusterObject com = (StdCusterObject) JSONObject.toBean(json,
                    StdCusterObject.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] { com.getDeleteName(), com.getStdCode(),
                        com.getCustCode() };
                sqlIdList.add("CoreStandard.deleteCust");
            }
            else {
                param = new Object[] { "", com.getStdCode(), com.getCustCode() };
                sqlIdList.add("CoreStandard.resumeCust");
            }
            params.add(param);
        }
        return this.setDataById(sqlIdList, params);
    }

    /**
     * 客户 检查是否重复
     * @param stdCode 标准名称
     * @param custCode 客户代码
     * @return 如果重复返回1，不重复返回NULL
     */
    public CoreReturnObject checkCuster(String stdCode, String custCode) {
        Object[] params = new Object[] { stdCode, custCode };
        return this.getDataBySqlID("CoreStandard.checkCust", params);
    }
    
    /**
     * 标准名称 检查是否被引用
     * @param stdCode 标准代码
     * @return 如果被引用，则返回值大于0
     */
    public CoreReturnObject checkStdName(String stdCode) {
        Object[] params = new Object[] { stdCode, stdCode,stdCode,stdCode,stdCode,stdCode };
        return this.getDataBySqlID("CoreStandard.checkStdName", params);
    }
    
    /**
     * 获取工序点类型
     */
    public CoreReturnObject getStationType(String msc, String mscPline, String processCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreStandard.getStationType");
    	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 stdCode
     * @return
     */
    public CoreReturnObject getProcessByStdCode(String stdCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreStandard.getProcessByStdCode");
    	sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
    	return this.getData(sql);
    }
    
    /**
     * 通过工序代码获取工序名称
     * @param processCode
     * @return
     */
    public CoreReturnObject getProcessDescByCode(String processCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreStandard.getProcessDescByCode");
    	sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", processCode);
    	return this.getData(sql);
    }
    
    public CoreReturnObject getComBaseStdByPk(String stdCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreStandard.getComBaseStdByPk");
    	sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
    	return this.getData(sql);
    }
    
    /**
     * 获取成分索引计划状态
     * @param cic
     * @return
     */
    public CoreReturnObject getPlnCicStatus(String cic){
    	SqlBuilder b = new SqlBuilder("CoreStandard.getPlnCicStatus");
    	b.Fill(new String[]{ cic });
    	
    	return this.getData(b.toString());
    }
}
