package com.steering.mes.mcp.common;

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

/**
 * 仓库权限
 * @author Administrator PDY 2015/08/11
 */
public class WarehousePermissions extends DBCommon {
    /**
     * 获取用户所拥有的仓库权限
     * @param storageType 仓库类别
     * @param storageAttr 仓库类型
     * @param validDataPurviewIds 用户数据权限
     * @return CoreReturnObject
     */
    public CoreReturnObject getWarehousePermissions(String storageType,
            String storageAttr, String[] validDataPurviewIds) {
        Object[] params = new Object[] { storageType, storageAttr };
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getWarehousePermissions");
        sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO",
                validDataPurviewIds);
        return this.getData(sql, params);

    }

    public CoreReturnObject getWarehousePermissionsStore(
            String[] validDataPurviewIds) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getWarehousePermissionsStore");
        sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO",
                validDataPurviewIds);
        sql += " ORDER BY T.STORAGE_NAME ASC";
        return this.getData(sql);
    }

    /**
     * 查询所以仓库
     * @param validDataPurviewIds
     * @return
     */
    public CoreReturnObject getWarehousePermissionsStore() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getWarehousePermissionsStore");   
        sql += " ORDER BY T.STORAGE_NAME,T.STORAGE_ATTR ASC";
        return this.getData(sql);
    }

    /**
     * 物料编码信息
     * @return
     */
    public CoreReturnObject getMaterialInfo() {
        return this.getDataBySqlID("WarehousePermissions.getMaterialInfo");
    }

    public CoreReturnObject getSection(String[] validDataPurviewIds) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getSection");
        sql += CoreConverter.toSqlConditionString("T.DEPARTID",
                validDataPurviewIds);
        return this.getData(sql);
    }

    public CoreReturnObject getBaseSection(String[] validDataPurviewIds) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getBaseSection");
        sql += CoreConverter.toSqlConditionString("T.DEPARTID",
                validDataPurviewIds);
        return this.getData(sql);
    }

    /**
     * 订货单位（销售组织权限）
     * @param salgPermisson 销售组织权限
     * @return CoreReturnObject
     */
    public CoreReturnObject getOrderUnitBySalg(String[] salgPermisson) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getOrderUnitBySalg");
        sql += CoreConverter
                .toSqlConditionString(" T2.SALE_ORG", salgPermisson);
        return this.getData(sql);
    }

    /**
     * 收货单位（销售组织权限）
     * @param salgPermisson 销售组织权限
     * @return CoreReturnObject
     */
    public CoreReturnObject getReceivingUnitBySalg(String[] salgPermisson) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getReceivingUnitBySalg");
        sql += CoreConverter
                .toSqlConditionString(" T2.SALE_ORG", salgPermisson);
        return this.getData(sql);
    }

    /**
     * 销售组织权限
     * @param validDataPurviewIds 数据权限
     * @return CoreReturnObject
     */
    public CoreReturnObject getSalgPermissions(String[] validDataPurviewIds) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getSalgPermissions");
        sql += CoreConverter.toSqlConditionString("T.DEPARTID",
                validDataPurviewIds);
        return this.getData(sql);
    }

    /***
     *根据数据权限查找仓库科室
     * @return
     */
    public CoreReturnObject getStoreNo(String storeType, String storeAtrr,
            String[] arr) {
        String condition = CoreConverter.toSqlConditionString("MANAGEMENT_NO",
                arr);
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getStoreNo");
        sql = sql.replaceAll("#CONDITION#", condition);
        // if (!storeType.equals("")) {
        // sql += " and t.STORAGE_TYPE_NO='" + storeType + "'";
        // }
        // if (!storeAtrr.equals("")) {
        // sql += " and t.STORAGE_ATTR ='" + storeAtrr + "' ";
        // }
        sql += "  order by t.STORAGE_NAME asc ";
        return this.getData(sql);
    }

    /***
     * 获取所有接箍的目标库
     * @param storeNo
     * @return
     */
    public CoreReturnObject getStoreAll(String storeNo,String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getStoreAll");
        sql += CoreConverter.nOtoSqlConditionString("T.STORAGE_NO", arr);
        return this.getData(sql, new Object[] { storeNo });
    }
    
    /***
     * 获取所有接箍的目标库
     * @param storeNo
     * @return
     */
    public CoreReturnObject getStoreAll(String storeNo, String storAllNo,
            String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getStoreAll2");
        sql += CoreConverter.nOtoSqlConditionString("T.STORAGE_NO", arr);
        return this.getData(sql, new Object[] { storeNo, storAllNo });
    }


    public CoreReturnObject getStoreAll(String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("WarehousePermissions.getStoreAll1");
        sql += " AND T.STORAGE_NO = '"+arr[0]+"'";
        sql += " OR T.STORAGE_NO = '"+arr[1]+"'";
        sql += " OR T.STORAGE_NO = '"+arr[2]+"'";
        return this.getData(sql);
    }


    /***
     * 获取所有接箍框号
     * @param storeNo
     * @return
     */
    public CoreReturnObject getKepNo() {
    	return this.getDataBySqlID("WarehousePermissions.getKepNo");
//        String sql = CoreXmlSqlParsersFactory
//                .getSql("WarehousePermissions.getKepNo");
//        return this.getData(sql);
    }

    /***
     * 根据数据权限查询部门
     * @return
     */
    public CoreReturnObject getWarehousePermissionsDepart(String[] validDataPurviewIds)
    {
        String sql = CoreXmlSqlParsersFactory.getSql("WarehousePermissions.getWarehousePermissionsDepart");
        sql += CoreConverter.toSqlConditionString("T.DEPARTID",validDataPurviewIds);
        return  this.getData(sql);
    }
}
