package com.steering.pss.qcm;

import java.sql.SQLException;
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.StringTool;

/**
 * 工序点基础信息管理类
 * @author WQ 2014/10/16
 * @version v1.0
 * @modify
 */
public class CoreControlPointBasics extends DBCommon {

    /**
     *  取消分组
     * @param stationCode 需要取消分组的记录
     * @return 平台统一返回对象
     */
    public CoreReturnObject cancelGroup(ArrayList<Object> stationCode) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<Object> sqlList = new ArrayList<Object>();
        ArrayList<Object[]> parmList = new ArrayList<Object[]>();

        for (int i = 0; i < stationCode.size(); i++) {
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreControlPointBasics.cancelGrouping"));
            parmList.add(new Object[] { stationCode.get(i) });
        }

        cro = this.getDao("TgDao").ExcuteTransction(sqlList, parmList);
        return cro;

    }

    /**
     * 保存
     * @param spbAddParams
     * @param spbUpdateParams
     * @param spbSupplementAddParams
     * @param spbSupplementUpdateParams
     * @return
     */
    public CoreReturnObject doGrouping(
            ArrayList<ArrayList<Object>> groupingParams) {

        CoreReturnObject spbCro = new CoreReturnObject();

        String Sql = CoreXmlSqlParsersFactory
                .getSql("CoreControlPointBasics.doGrouping");

        ArrayList<Object> spbSqlList = new ArrayList<Object>();
        ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();

        if (groupingParams.size() > 0) {
            for (int i = 0; i < groupingParams.size(); i++) {
                spbSqlList.add(Sql);
                spbParmList.add(groupingParams.get(i).toArray());
            }
        }

        try {
            spbCro = this.getDao("TgDao").ExcuteTransction(spbSqlList,
                    spbParmList);
        }
        catch (Exception e) {
            spbCro = spbCro.SetErrInfo(e.getMessage());
        }
        return spbCro;
    }

    /**
     * 获取最大索引号
     * @param name
     * @return
     */
    public CoreReturnObject GetMaxGrouping() {
        return this.getDataBySqlID("CoreControlPointBasics.GetMaxGrouping");
    }

    /**
     * 获取顺序号最大值
     * @param name
     * @return
     */
    public CoreReturnObject GetMaxStation() {
        return this.getDataBySqlID("CoreControlPointBasics.GetMaxStation");
    }

    /**
     * 判断描述信息是否重复
     * @param name
     * @return
     */
    public CoreReturnObject GetName(String name) {
        Object[] obj = new Object[] { name };
        return this.getDataBySqlID("CoreControlPointBasics.GetName", obj);
    }

    /**
     * 获取代码最大值
     * @param name
     * @return
     */
    public CoreReturnObject GetMaxCode() {
        return this.getDataBySqlID("CoreControlPointBasics.GetMaxCode");
    }

    /**
     * 获取监制级别
     * @return
     */
    public CoreReturnObject GetMonitorLv() {
        return this.getDataBySqlID("CoreControlPointBasics.GetMonitorLv");
    }

    /**
     * 获取工序类型代码和描述
     * @return
     */
    public CoreReturnObject GetProcess() {
        return this.getDataBySqlID("CoreControlPointBasics.GetProcess");
    }

    /**
     * 获取工序点类型代码和描述
     * @return
     */
    public CoreReturnObject GetPointType() {
        return this.getDataBySqlID("CoreControlPointBasics.GetPointType");
    }

    /**
     * 工序点基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 工序点描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getLineList(Boolean includeDeleted, String name, String stationType) {
        String deletedFlag = includeDeleted ? "0" : "";
        Object[] params = new Object[] { deletedFlag, name, stationType, stationType, stationType};
        return this.getDataBySqlID("CoreControlPointBasics.QUERY", params);
    }

    /**
     * 工序点基础信息新增
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreControlPointBasics.ADD");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 工序点基础信息修改
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doModify(ArrayList<String> parm) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreControlPointBasics.MODIFY");
        String sqlProcesscRStation = CoreXmlSqlParsersFactory.getSql("CoreControlPointBasics.updateProcessCRStation");
        sqlProcesscRStation = StringTool.ReplaceAll(sqlProcesscRStation, "#STATION_ID#", parm.get(12));
        sqlProcesscRStation = StringTool.ReplaceAll(sqlProcesscRStation, "#STATION_CODE#", parm.get(13));
        
        sqllist.add(strSql);
        sqllist.add(sqlProcesscRStation);
        params.add(parm.toArray());
        params.add(null);
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, params);
    }

    /**
     * 工序点基础信息删除或恢复
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject deleteLineInfo(ArrayList<Object> plineCodes,
            String operator, Boolean isDelete) {
        ArrayList<String> sqlIDList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        for (int i = 0; i < plineCodes.size(); i++) {
            if (isDelete) {
                sqlIDList.add("CoreControlPointBasics.DELETE");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
            }
            else {
                sqlIDList.add("CoreControlPointBasics.RESUME");
                params.add(new Object[] { plineCodes.get(i).toString() });
            }
        }
        return this.setDataById(sqlIDList, params);
    }

    public CoreReturnObject isExistStationId(ArrayList<String> list) {
        return getDataBySqlID("CoreControlPointBasics.IsExistStationId",
                list.toArray());
    }

    public CoreReturnObject isExistStationCode(String oldStationCode,
            String stationCode, String actionType) {
        if (actionType.equals("Insert")) {
            return getDataBySqlID("CoreControlPointBasics.IsExistStationCode2",
                    new Object[] { stationCode });
        }
        else {
            return getDataBySqlID("CoreControlPointBasics.IsExistStationCode",
                    new Object[] { oldStationCode, stationCode });
        }
    }

}
