package com.steering.pss.qcm;

import java.util.ArrayList;

import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.pss.qcm.model.ComBaseSpecEntity;
import com.steering.pss.qcm.model.ComBaseSpecOrderEntity;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

/**
 * 产品规格主表
 * @author Administrator
 *
 */
public class CoreSpecBasicsNew extends DBCommon {

    /**
     * 产品规格以及订货规格保存
     * @param spbAddParams 产品规格主表新增参数
     * @param spbUpdateParams 产品规格主表修改参数
     * @param spbSubAddParams 订货规格从表新增参数
     * @param spbSubUpdateParams 订货规格从表修改参数
     * @return obj
     */
    public CoreReturnObject doSave(ArrayList<ArrayList<Object>> spbAddParams,
            ArrayList<ArrayList<Object>> spbUpdateParams,
            ArrayList<ArrayList<Object>> spbSubAddParams,
            ArrayList<ArrayList<Object>> spbSubUpdateParams) {

        CoreReturnObject spbCro = new CoreReturnObject();

        String spbAddSql = CoreXmlSqlParsersFactory
                .getSql("CoreSpecificationsBasicsNew.ADD");

        String spbUpdateSql = CoreXmlSqlParsersFactory
                .getSql("CoreSpecificationsBasicsNew.MODIFY");

        String spbSupplementAddSql = CoreXmlSqlParsersFactory
                .getSql("CoreSpecificationsBasicsSupplement.ADD");

        String spbSupplementUpdateSql = CoreXmlSqlParsersFactory
                .getSql("CoreSpecificationsBasicsSupplement.MODIFY");

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

        // 构建主表addsql语句和参数。

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

        // 构建主表修改sql语句和参数。

        if (spbUpdateParams.size() > 0) {
            for (int j = 0; j < spbUpdateParams.size(); j++) {
                spbSqlList.add(spbUpdateSql);
                spbParmList.add(spbUpdateParams.get(j).toArray());
            }
        }
        // 构建从表新增sql语句和参数。

        if (spbSubAddParams.size() > 0) {
            for (int k = 0; k < spbSubAddParams.size(); k++) {
                spbSqlList.add(spbSupplementAddSql);
                spbParmList.add(spbSubAddParams.get(k).toArray());
            }
        }

        // 构建从表修改sql语句和参数。
        if (spbSubUpdateParams.size() > 0) {
            for (int g = 0; g < spbSubUpdateParams.size(); g++) {
                spbSqlList.add(spbSupplementUpdateSql);
                spbParmList.add(spbSubUpdateParams.get(g).toArray());
            }
        }

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

        return spbCro;

    }

    /**
     * 保存产品规格和订货规格。
     * @param specList 产品规格实体类集合
     * @param specOrderList 订货规格实体类集合
     * @return obj
     */
    public CoreReturnObject save(ArrayList<String> specList,
            ArrayList<String> specOrderList) {
        ArrayList<String> sqllist = new ArrayList<String>();
        for (String strObj : specList) {
            JSONObject jsonObject = JSONObject.fromObject(strObj);
            ComBaseSpecEntity comBASESPECEntity = (ComBaseSpecEntity ) JSONObject
                    .toBean(jsonObject, ComBaseSpecEntity .class);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreSpecificationsBasicsNew.Save");
            sql = sql.replaceFirst(":SPEC_CODE", comBASESPECEntity.getSpecCode());
            sql = sql.replaceFirst(":SPEC_NAME", comBASESPECEntity.getSpecName());
            sql = sql.replaceFirst(":DIMATER", comBASESPECEntity.getDimater());
            sql= sql.replaceFirst(":HEIGHT", comBASESPECEntity.getHeight());
            sql = sql.replaceFirst(":WEIGHT", comBASESPECEntity.getWeight());
            sql = sql.replaceFirst(":MSC_STYLE", comBASESPECEntity.getMscStyle());
            sql = sql.replaceFirst(":CREATE_NAME", comBASESPECEntity
                            .getCreateName());
            sql = sql.replaceFirst(":MEMO", comBASESPECEntity.getMemo());
            sqllist.add(sql);
        }

        for (String strObj : specOrderList) {
            JSONObject jsonObject = JSONObject.fromObject(strObj);
            ComBaseSpecOrderEntity comBaseSpecOrderEntity = (ComBaseSpecOrderEntity) JSONObject
                    .toBean(jsonObject, ComBaseSpecOrderEntity.class);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreSpecificationsBasicsSupplement.Save");
            sql = sql.replaceFirst(":SPEC_CODE", comBaseSpecOrderEntity
                    .getSpecCode());
            sql = sql.replaceFirst(":ORDER_SPEC_DESC", comBaseSpecOrderEntity
                    .getOrderSpecDesc());
            sql = sql.replaceFirst(":ORDER_SPEC_CODE", comBaseSpecOrderEntity
                    .getOrderSpecCode());
            sql = sql.replaceFirst(":SPEC_NAME", comBaseSpecOrderEntity
                    .getSpecName());
            sql = sql.replaceFirst(":CREATE_NAME", comBaseSpecOrderEntity
                    .getCreateName());
            sql = sql.replaceFirst(":MEMO", comBaseSpecOrderEntity.getMemo());
            sqllist.add(sql);
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist);
    }

    /**
     * 产品规格基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 产品规格描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getLineList(Boolean includeDeleted, String name, String mscStyle) {
        String deletedFlag = includeDeleted ? "0" : "";
        String sql = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsBasicsNew.QUERY");
        sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", deletedFlag);
        sql = StringTool.ReplaceAll(sql, "#SPEC_NAME#", name);
        sql = StringTool.ReplaceAll(sql, "#MSC_STYLE#", mscStyle);
        
        return this.getData(sql);
    }
    /**
     * 分类型产品规格基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 产品规格描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getLineListPscType(Boolean includeDeleted, String name,String type) {
        String deletedFlag = includeDeleted ? "0" : "";
        Object[] params = new Object[] { deletedFlag, name, type};
        return this.getDataBySqlID("CoreSpecificationsBasicsNew.QUERYSTYLE", params);
    }
    /**
     * 产品规格表新增
     * @param parms 产品规格表新增参数
     * @return obj
     */
    public CoreReturnObject doAdd(ArrayList<ArrayList<Object>> parms) {

        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreSpecificationsBasicsNew.ADD");

        ArrayList<Object> sqllist = new ArrayList<Object>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < parms.size(); i++) {
            sqllist.add(sql);
            ArrayList<Object> parm = new ArrayList<Object>();
            parm = parms.get(i);
            parmlist.add(parm.toArray());
        }

        try {
            cro = this.getDao("TgDao").ExcuteTransction(sqllist, parmlist);
        }
        catch (Exception e) {
            cro = cro.SetErrInfo(e.getMessage());
        }

        return cro;
    }

    /**
     * 产品规格修改
     * @param parms 产品规格修改参数
     * @return obj
     */
    public CoreReturnObject doModify(ArrayList<ArrayList<Object>> parms) {

        CoreReturnObject cro = new CoreReturnObject();

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreSpecificationsBasicsNew.MODIFY");

        ArrayList<Object> sqllist = new ArrayList<Object>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < parms.size(); i++) {
            sqllist.add(sql);
            parmlist.add(parms.get(i).toArray());
        }

        try {
            cro = this.getDao("TgDao").ExcuteTransction(sqllist, parmlist);
        }
        catch (Exception e) {
            cro = cro.SetErrInfo(e.getMessage());
        }

        return cro;

    }

    /**
     * 产品规格基础信息删除或恢复
     * @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("CoreSpecificationsBasics.DELETE");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
            }
            else {
                sqlIDList.add("CoreSpecificationsBasics.RESUME");
                params.add(new Object[] { plineCodes.get(i).toString() });
            }
        }
        return this.setDataById(sqlIDList, params);
    }

    /**
     * 获取最大的Spec_Code
     * @return obj
     */
    public CoreReturnObject getMaxSpecCode() {
        return this.getDataBySqlID("CoreSpecificationsBasicsNew.MAXID");
    }
    
    /**
     * 获取最大的订货规格代码
     * @param list 产品规格代码
     * @return obj
     */
    public CoreReturnObject getMaxOrderSpecCode(ArrayList<String> list) {
        return this.getDataBySqlID(
                "CoreSpecificationsBasicsNew.GetMaxOrder_Spec_Code", list
                        .toArray());
    }

    /**
     * 是否存在产品规格代码
     * @param list 当前Grid行的规格代码
     * @return obj
     */
    public CoreReturnObject isExistSpec(ArrayList<String> list) {
        return this.getDataBySqlID("CoreSpecificationsBasicsNew.IsExistSpec",
                list.toArray());
    }

    /**
     * 是否存在订货规格代码
     * @param list 当前Grid行的订货规格代码
     * @return obj
     */
    public CoreReturnObject isExistOrderSpec(ArrayList<String> list) {
        return this.getDataBySqlID(
                "CoreSpecificationsBasicsNew.IsExistOrderSpec", list.toArray());
    }

    public CoreReturnObject getMeterWeight(String memo) {
        Object []obj=null;
        if(memo.equals("A"))
            obj=new Object[]{"钢管"};
        if(memo.equals("B"))
            obj=new Object[]{"管坯"};
        return this.getDataBySqlID(
                "CoreSpecificationsBasicsNew.getMeterWeight", obj);
    }
    
}
