package cn.estsh.i3plus.pojo.mes.pcn.sqlpack;

import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.pcn.bean.*;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description:
 * @Reference: Mes 条件封装
 * @Author: Crish
 * @CreateDate:2019-04-22-14:58
 * @Modify:
 **/
public class MesHqlPack {


    /**
     * 通用查询条件
     *
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getAllBaseData(String organizeCode) {
        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", packBean);
        return packBean;
    }

    /**
     * 通用查询条件
     *
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getAllBaseDataByNormalPro(BaseBean baseBean, String organizeCode) {
        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", packBean);
        DdlPreparedPack.getNumEqualPack(baseBean.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", packBean);
        return packBean;
    }

    /**
     * MES PCN系统配置信息条件查询
     *
     * @param mesConfig
     * @return
     */
    public static DdlPackBean getMesConfig(MesConfig mesConfig, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (StringUtils.isNotEmpty(mesConfig.getCfgCode())) {
            DdlPreparedPack.getStringLikerPack(mesConfig.getCfgCode(), "cfgCode", packBean);
        }
        if (StringUtils.isNotEmpty(mesConfig.getCfgName())) {
            DdlPreparedPack.getStringLikerPack(mesConfig.getCfgName(), "cfgName", packBean);
        }
        if (mesConfig.getIsValid() != null) {
            DdlPreparedPack.getNumEqualPack(mesConfig.getIsValid(), "isValid", packBean);
        }
        return packBean;
    }

    /**
     * 根据打印模板的模板代码查询模板信息
     *
     * @param organizeCode
     * @param mesLabelTemplate
     * @return
     */
    public static DdlPackBean getLabelTemplate(String organizeCode, MesLabelTemplate mesLabelTemplate) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.getStringEqualPack(mesLabelTemplate.getTemplateCode(), "templateCode", packBean);
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", packBean);
        return packBean;
    }

    /**
     * 获取工作中心下所有的投料数据
     * @param mesCellFeed
     * @param org
     * @return
     */
    public static DdlPackBean getMesCellFeed(MesCellFeed mesCellFeed, String org) {
        DdlPackBean packBean = getAllBaseData(org);
        DdlPreparedPack.getStringEqualPack(mesCellFeed.getWorkCenterCode(), "workCenterCode", packBean);
        DdlPreparedPack.getNumberBiggerPack(0, "rawQty", packBean);
        return packBean;
    }


    /**
     * 获取原材料扫描信息的对应工序BOM
     * @param mesProcessBom
     * @param mesRawPartSn
     * @return
     */
    public static DdlPackBean getMesProcessBomRawPartSn(MesProcessBom mesProcessBom, MesRawPartSn mesRawPartSn) {
        DdlPackBean packBean = getAllBaseData(mesProcessBom.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesProcessBom.getWorkCenterCode(), "workCenterCode", packBean);
        DdlPreparedPack.getStringEqualPack(mesProcessBom.getWorkCellCode(), "workCellCode", packBean);
        DdlPreparedPack.getStringEqualPack(mesProcessBom.getPartNo(), "partNo", packBean);
        DdlPreparedPack.getNumEqualPack(MesPcnEnumUtil.PROCESS_BOM_ISFEED.FEED.getValue(),"isFeed",packBean);
        DdlPreparedPack.getStringEqualPack(mesRawPartSn.getPartNo(), "itemPartNo", packBean);
        return packBean;
    }

    /**
     * 生产工单查询条件封装
     *
     * @param mesWorkOrder
     * @return
     */
    public static DdlPackBean getWorkOrderCondition(MesWorkOrder mesWorkOrder, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (StringUtils.isNotEmpty(mesWorkOrder.getOrderNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getOrderNo(), "orderNo", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getPartNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getPartNo(), "partNo", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getWorkOrderStatus() + "") && mesWorkOrder.getWorkOrderStatusVal() != 0) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getWorkOrderStatus(), "workOrderStatus", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getWorkCenterCode())) {
            DdlPreparedPack.getStringEqualPack(mesWorkOrder.getWorkCenterCode(), "workCenterCode", packBean);
        }
        if (mesWorkOrder.getApprovalStatus() != null) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getApprovalStatus(), "approvalStatus", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getWorkOrderType() + "") && mesWorkOrder.getWorkOrderTypeVal() != 0) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getWorkOrderType(), "workOrderType", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getCustOrderNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getCustOrderNo(), "custOrderNo", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getPlanOrderNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getPlanOrderNo(), "planOrderNo", packBean);
        }
        if (StringUtils.isNotEmpty(mesWorkOrder.getCreateUser())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getCreateUser(), "createUser", packBean);
        }
        if (mesWorkOrder.getIsValid() != null) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getIsValid(), "isValid", packBean);
        }

        // 时间段查询
        DdlPreparedPack.timeBuilder(
                mesWorkOrder.getStartTimeStart(),
                mesWorkOrder.getStartTimeEnd(),
                "startTime", packBean, true);
        DdlPreparedPack.timeBuilder(
                mesWorkOrder.getEndTimeStart(),
                mesWorkOrder.getEndTimeEnd(),
                "endTime", packBean, true);
        DdlPreparedPack.timeBuilder(
                mesWorkOrder.getCreateDateTimeStart(),
                mesWorkOrder.getCreateDateTimeEnd(),
                "createDatetime", packBean, true);
        DdlPreparedPack.getOrderDefault(mesWorkOrder);
        DdlPreparedPack.getOrderByPack(new Object[]{"2"}, new String[]{mesWorkOrder.getOrderByParam()}, packBean);
        return packBean;
    }

    /**
     * 物料信息查询条件封装
     *
     * @param mesPart
     * @return
     */
    public static DdlPackBean getPartCondition(MesPart mesPart, String organizeCode) {
        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", packBean);

        if (StringUtils.isNotEmpty(mesPart.getPartNo())) {
            DdlPreparedPack.getStringLikerPack(mesPart.getPartNo(), "partNo", packBean);
        }
        if (StringUtils.isNotEmpty(mesPart.getPartName())) {
            DdlPreparedPack.getStringLikerPack(mesPart.getPartName(), "partName", packBean);
        }
        if (StringUtils.isNotEmpty(mesPart.getCategoryCode1())) {
            DdlPreparedPack.getStringEqualPack(mesPart.getCategoryCode1(), "categoryCode1", packBean);
        }
        if (StringUtils.isNotEmpty(mesPart.getCategoryCode2())) {
            DdlPreparedPack.getStringEqualPack(mesPart.getCategoryCode2(), "categoryCode2", packBean);
        }
        if (StringUtils.isNotEmpty(mesPart.getCategoryCode3())) {
            DdlPreparedPack.getStringEqualPack(mesPart.getCategoryCode3(), "categoryCode3", packBean);
        }

        DdlPreparedPack.getOrderDefault(mesPart);
        DdlPreparedPack.getOrderByPack(new Object[]{"2"}, new String[]{mesPart.getOrderByParam()}, packBean);
        return packBean;
    }

    /**
     * MES 客户信息查询条件封装
     *
     * @param customer
     * @return
     */
    public static DdlPackBean getMesCustomer(MesCustomer customer, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(customer, organizeCode);
        if (StringUtils.isNotEmpty(customer.getCustomerCode())) {
            DdlPreparedPack.getStringLikerPack(customer.getCustomerCode(), "customerCode", packBean);
        }
        if (StringUtils.isNotEmpty(customer.getCustomerName())) {
            DdlPreparedPack.getStringLikerPack(customer.getCustomerName(), "customerName", packBean);
        }

        return packBean;
    }

}
