package com.steering.pss.qcm;

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

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

import com.steering.comm.db.DBCommon;

/**
 * 基础信息管理类
 * @author wq 2014/10/13
 * @version v1.0
 * @modify
 */
public class CoreBaseInfoNew extends DBCommon {

    /**
     * 判断是否允许增加子项
     * @param name
     * @return
     */
    public CoreReturnObject isAddChild(String baseCode) {
        Object[] obj = new Object[] { baseCode };
        return this.getDataBySqlID("CoreBaseInfoNew.isAddChild", obj);
    }

    /**
     * 判断用户名是否重复
     * @param name
     * @return
     */
    public CoreReturnObject GetName(String sortCode, String baseName) {
        Object[] obj = new Object[] { sortCode, baseName };
        return this.getDataBySqlID("CoreBaseInfoNew.GetName", obj);
    }

    /**
     * 获取当前父节点下，最大编号
     * @param parm
     * @return
     */
    public CoreReturnObject GetMaxBaseCode(String sortcode) {
        Object[] obj = new Object[] { sortcode };
        return this.getDataBySqlID("CoreBaseInfoNew.GetMaxBaseCode", obj);
    }

    /**
     * 基础信息新增
     * @param parm
     * @return
     */
    public CoreReturnObject doAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory.getSql("CoreBaseInfoNew.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<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreBaseInfoNew.MODIFY");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException 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("CoreBaseInfoNew.DELETE");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
            }
            else {
                sqlIDList.add("CoreBaseInfoNew.RESUME");
                params.add(new Object[] { plineCodes.get(i).toString() });
            }
        }
        return this.setDataById(sqlIDList, params);
    }

    /**
     * 基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param productionLineName 产线名称
     * @param departmentDesc 管理部门描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getTreeData(ArrayList<Object> params) {

        return this
                .getDataBySqlID("CoreBaseInfoNew.TreeData", params.toArray());
    }


    public CoreReturnObject getTreeDataNew(ArrayList<String> sortCodes) {
        String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseInfoNew.TreeDataNew");

        if(sortCodes.size()>0){
            sql += "  AND ( 1<> 1 ";
            for (int i = 0; i < sortCodes.size() ; i++) {

                sql+= " OR BASECODE LIKE '"  + sortCodes.get(i) + "%'";

            }

            sql += "  ) ";
        }else{



        }

        sql += "        ORDER BY BASECODE";

        return this.getData(sql);
    }

    public CoreReturnObject doQuery(String basecode) {

        Object[] obj = new Object[] { basecode };

        return this.getDataBySqlID("CoreBaseInfoNew.QUERY", obj);
    }

    public CoreReturnObject isExistValidChild(String baseCode) {
        Object[] objs = new Object[] { baseCode };
        return getDataBySqlID("CoreBaseInfoNew.IsExistValidChild", objs);
    }

    public CoreReturnObject isExistValidParent(String sortCode) {
        Object[] objs = new Object[] { sortCode };
        return getDataBySqlID("CoreBaseInfoNew.IsExistValidParent", objs);
    }

}
