package com.steering.pss.qcm;

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

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

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

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

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

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

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

    /**
     * 基础信息查询
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getTreeData(ArrayList<Object> params) {

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


    public CoreReturnObject getTreeDataNew(ArrayList<String> sortCodes) {
        String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseInfoCost.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("CoreBaseInfoCost.QUERY", obj);
    }

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

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

}
