package com.steering.pss.qcm;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

/**
 * 品名基础信息管理类
 * @author WQ 2014/10/16
 * @version v1.0
 * @modify
 */
public class CoreProductName extends DBCommon {

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

    /**
     * 品名基础信息新增
     */
   public CoreReturnObject doAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory.getSql("CoreProductName.ADD");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
            /*ArrayList<Object> arrayList = new ArrayList<Object>();
            arrayList.add(parm.get(0));
            arrayList.add(parm.get(1));
            arrayList.add(parm.get(5));
            doAddLimsMat(arrayList);*/
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 品名基础信息修改
     * @param 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */

    public CoreReturnObject doModify(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();

        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreProductName.MODIFY");

        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
            /*ArrayList<Object> arrayList = new ArrayList<Object>();
            arrayList.add(parm.get(10));
            arrayList.add(parm.get(0));
            arrayList.add(parm.get(4));
            arrayList.add(parm.get(10));
            doUpdateLimsMat(arrayList);*/
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }

        return cro;
    }

    /**
     * 品名基础信息删除或恢复
     */
    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("CoreProductName.DELETE");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
/*                ArrayList<Object> arrayList = new ArrayList<Object>();
                arrayList.add("0");
                arrayList.add("cx");
                arrayList.add(operator);
                arrayList.add(new Date());
                arrayList.add(plineCodes.get(i).toString());
                doLimsMatlogicDelete(arrayList);*/
            }
            else {
                sqlIDList.add("CoreProductName.RESUME");
                params.add(new Object[] { plineCodes.get(i).toString() });
                /*ArrayList<Object> arrayList = new ArrayList<Object>();
                arrayList.add("1");
                arrayList.add(null);
                arrayList.add(null);
                arrayList.add(null);
                arrayList.add(plineCodes.get(i).toString());
                doLimsMatlogicDelete(arrayList);*/
            }


        }
        return this.setDataById(sqlIDList, params);
    }


    /*public CoreReturnObject doAddLimsMat(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory.getSql("CoreProductName.doAddLimsMat");
        try {
            cro = this.getDao("LimsDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }*/

/*    public CoreReturnObject doUpdateLimsMat(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();

        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreProductName.doUpdateLimsMat");

        try {
            cro = this.getDao("LimsDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }

        return cro;
    }*/


    /*public CoreReturnObject doLimsMatlogicDelete(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();

        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreProductName.doLimsMatlogicDelete");
        try {
            cro = this.getDao("LimsDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }*/

    /**
     * 获取最大编码
     * @return cro
     */
    public CoreReturnObject QueryMaxCode() {
        return this.getDataBySqlID("CoreProductName.QueryMaxCode");
    }

    public CoreReturnObject IsExist(ArrayList<String> list) {
        return this.getDataBySqlID("CoreProductName.IsExist", list.toArray());
    }

    public CoreReturnObject isRefrenced(String productcode) {
        Object[] obj = new Object[] { productcode };
        return this.getDataBySqlID("CoreProductName.IsRefrenced", obj);
    }

    public CoreReturnObject getModel(String productcode) {
        Object[] obj = new Object[] { productcode, productcode };
        return this.getDataBySqlID("CoreProductName.GETMODEL", obj);
    }

    @SuppressWarnings("unchecked")
    public CoreReturnObject doLink(ArrayList<Object> list, String id,
            String desc) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqlList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();

        sqlList.add(CoreXmlSqlParsersFactory
                .getSql("CoreProductName.MODELDELETE"));
        params.add(new Object[] { id });

        for (int i = 0; i < list.size(); i++) {
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreProductName.MODELINSERT"));
            ArrayList<Object> al = (ArrayList<Object>) list.get(i);
            al.add(id);
            al.add(desc);
            params.add(al.toArray());
        }
        cro = this.setData(sqlList, params);
        return cro;

    }
    
    /**
     * 是否被使用。
     * @param producCode
     * @return obj
     */
    public CoreReturnObject isUsed(String producCode){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreProductName.isUsed");
    	sql = StringTool.ReplaceAll(sql, "#PRODUCCODE#", producCode);
    	
    	return this.getData(sql);
    }

}
