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.springframework.util.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.isEmpty(mesConfig.getCfgCode())) {
            DdlPreparedPack.getStringLikerPack(mesConfig.getCfgCode(), "cfgCode", packBean);
        }
        if (!StringUtils.isEmpty(mesConfig.getCfgName())) {
            DdlPreparedPack.getStringLikerPack(mesConfig.getCfgName(), "cfgName", packBean);
        }
        if (!StringUtils.isEmpty(mesConfig.getIsValid())) {
            DdlPreparedPack.getNumEqualPack(mesConfig.getIsValid(), "isValid", packBean);
        }
        return packBean;
    }

    /**
     * 根据打印模板的模板代码查询模板信息
     *
     * @param mesLabelTemplate
     * @return
     */
    public static DdlPackBean getLabelTemplate( MesLabelTemplate mesLabelTemplate) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.getStringEqualPack(mesLabelTemplate.getTemplateCode(), "templateCode", 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.getNumberBiggerPackContainsZero(0, "rawQty", packBean);
        return packBean;
    }


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

    /**
     * 生产工单查询条件封装
     *
     * @param mesWorkOrder
     * @return
     */
    public static DdlPackBean getWorkOrderCondition(MesWorkOrder mesWorkOrder, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (!StringUtils.isEmpty(mesWorkOrder.getOrderNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getOrderNo(), "orderNo", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getPartNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getPartNo(), "partNo", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getWorkOrderStatus()) && mesWorkOrder.getWorkOrderStatusVal() != 0) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getWorkOrderStatus(), "workOrderStatus", packBean);
        } else {
            if (!StringUtils.isEmpty(mesWorkOrder.getPageType()) && "1".equals(mesWorkOrder.getPageType())) {
                DdlPreparedPack.getInPackArray(new Object[]{MesPcnEnumUtil.WORK_ORDER_STATUS.OPEN.getValue(), MesPcnEnumUtil.WORK_ORDER_STATUS.PAUSE.getValue(),
                        MesPcnEnumUtil.WORK_ORDER_STATUS.COMPLETE.getValue()}, "workOrderStatus", packBean);
            }
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getWorkCenterCode())) {
            DdlPreparedPack.getStringEqualPack(mesWorkOrder.getWorkCenterCode(), "workCenterCode", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getApprovalStatus())) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getApprovalStatus(), "approvalStatus", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getWorkOrderType() + "") && mesWorkOrder.getWorkOrderTypeVal() != 0) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getWorkOrderType(), "workOrderType", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getCustOrderNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getCustOrderNo(), "custOrderNo", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getPlanOrderNo())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getPlanOrderNo(), "planOrderNo", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getCreateUser())) {
            DdlPreparedPack.getStringLikerPack(mesWorkOrder.getCreateUser(), "createUser", packBean);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getIsValid())) {
            DdlPreparedPack.getNumEqualPack(mesWorkOrder.getIsValid(), "isValid", packBean);
        }

        // 时间段查询
        if (!StringUtils.isEmpty(mesWorkOrder.getStartTimeStart()) || !StringUtils.isEmpty(mesWorkOrder.getStartTimeEnd())) {
            DdlPreparedPack.timeBuilder(
                    mesWorkOrder.getStartTimeStart(),
                    mesWorkOrder.getStartTimeEnd(),
                    "startTime", packBean, true);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getEndTimeStart()) || !StringUtils.isEmpty(mesWorkOrder.getEndTimeEnd())) {
            DdlPreparedPack.timeBuilder(
                    mesWorkOrder.getEndTimeStart(),
                    mesWorkOrder.getEndTimeEnd(),
                    "endTime", packBean, true);
        }
        if (!StringUtils.isEmpty(mesWorkOrder.getCreateDateTimeStart()) || !StringUtils.isEmpty(mesWorkOrder.getCreateDateTimeEnd())) {
            DdlPreparedPack.timeBuilder(
                    mesWorkOrder.getCreateDateTimeStart(),
                    mesWorkOrder.getCreateDateTimeEnd(),
                    "createDatetime", packBean, true);
        }
        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.isEmpty(mesPart.getPartNo())) {
            DdlPreparedPack.getStringLikerPack(mesPart.getPartNo(), "partNo", packBean);
        }
        if (!StringUtils.isEmpty(mesPart.getPartName())) {
            DdlPreparedPack.getStringLikerPack(mesPart.getPartName(), "partName", packBean);
        }
        if (!StringUtils.isEmpty(mesPart.getCategoryCode1())) {
            DdlPreparedPack.getStringEqualPack(mesPart.getCategoryCode1(), "categoryCode1", packBean);
        }
        if (!StringUtils.isEmpty(mesPart.getCategoryCode2())) {
            DdlPreparedPack.getStringEqualPack(mesPart.getCategoryCode2(), "categoryCode2", packBean);
        }
        if (!StringUtils.isEmpty(mesPart.getCategoryCode3())) {
            DdlPreparedPack.getStringEqualPack(mesPart.getCategoryCode3(), "categoryCode3", packBean);
        }
        return packBean;
    }

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

        return packBean;
    }

    /**
     * MES PCN系维修信息条件查询
     *
     * @param mesRepair
     * @return
     */
    public static DdlPackBean getMesRepair(MesRepair mesRepair, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (!StringUtils.isEmpty(mesRepair.getRepairCode())) {
            DdlPreparedPack.getStringLikerPack(mesRepair.getRepairCode(), "repairCode", packBean);
        }
        if (!StringUtils.isEmpty(mesRepair.getRepairName())) {
            DdlPreparedPack.getStringLikerPack(mesRepair.getRepairName(), "repairName", packBean);
        }
        if (!StringUtils.isEmpty(mesRepair.getRepairType())) {
            DdlPreparedPack.getNumEqualPack(mesRepair.getRepairType(), "repairType", packBean);
        }
        return packBean;
    }

    /**
     * MES PCN系维修信息条件查询
     *
     * @param mesScrap
     * @return
     */
    public static DdlPackBean getMesScrap(MesScrap mesScrap, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (!StringUtils.isEmpty(mesScrap.getScrapCode())) {
            DdlPreparedPack.getStringLikerPack(mesScrap.getScrapCode(), "scrapCode", packBean);
        }
        if (!StringUtils.isEmpty(mesScrap.getScrapName())) {
            DdlPreparedPack.getStringLikerPack(mesScrap.getScrapName(), "scrapName", packBean);
        }
        if (!StringUtils.isEmpty(mesScrap.getScrapType())) {
            DdlPreparedPack.getStringLikerPack(mesScrap.getScrapType(), "scrapType", packBean);
        }
        return packBean;
    }

    /**
     * MES PCN缺陷原因信息条件查询
     *
     * @param mesDefectCause
     * @return
     */
    public static DdlPackBean getMesDefectCause(MesDefectCause mesDefectCause, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (!StringUtils.isEmpty(mesDefectCause.getDcCode())) {
            DdlPreparedPack.getStringLikerPack(mesDefectCause.getDcCode(), "dcCode", packBean);
        }
        if (!StringUtils.isEmpty(mesDefectCause.getDcName())) {
            DdlPreparedPack.getStringLikerPack(mesDefectCause.getDcName(), "dcName", packBean);
        }
        if (!StringUtils.isEmpty(mesDefectCause.getDcType())) {
            DdlPreparedPack.getNumEqualPack(mesDefectCause.getDcType(), "dcType", packBean);
        }
        return packBean;
    }

    /**
     * 按条件分页查询产品条码补打印数据
     * @param mesProduceSn
     * @return
     */
    public static DdlPackBean getProdSnPrint(MesProduceSn mesProduceSn) {
        DdlPackBean packBean = getAllBaseData(mesProduceSn.getOrganizeCode());
        if (!StringUtils.isEmpty(mesProduceSn.getSerialNumber())) {
            DdlPreparedPack.getStringLikerPack(mesProduceSn.getSerialNumber(), "serialNumber", packBean);
        }

        if (!StringUtils.isEmpty(mesProduceSn.getProductSn())) {
            DdlPreparedPack.getStringLikerPack(mesProduceSn.getProductSn(), "productSn", packBean);
        }

        if (!StringUtils.isEmpty(mesProduceSn.getPartNo())) {
            DdlPreparedPack.getStringLikerPack(mesProduceSn.getPartNo(), "partNo", packBean);
        }

        if (!StringUtils.isEmpty(mesProduceSn.getSnStatus())) {
            DdlPreparedPack.getNumEqualPack(mesProduceSn.getSnStatus(), "snStatus", packBean);
        }

        // 时间段查询
        if (!StringUtils.isEmpty(mesProduceSn.getCreateDateTimeStart()) || !StringUtils.isEmpty(mesProduceSn.getCreateDateTimeEnd())) {
            DdlPreparedPack.timeBuilder(
                    mesProduceSn.getCreateDateTimeStart(),
                    mesProduceSn.getCreateDateTimeEnd(),
                    "createDatetime", packBean, true);
        }
        if (!StringUtils.isEmpty(mesProduceSn.getOutWorkCenterTimeStart()) || !StringUtils.isEmpty(mesProduceSn.getOutWorkCenterTimeEnd())) {
            DdlPreparedPack.timeBuilder(
                    mesProduceSn.getOutWorkCenterTimeStart(),
                    mesProduceSn.getOutWorkCenterTimeEnd(),
                    "outWorkCenterTime", packBean, true);
        }
        return packBean;
    }

    /**
     * 按条件分页查询包装条码补打印数据
     * @param mesPackage
     * @return
     */
    public static DdlPackBean getPackageSnPrint(MesPackage mesPackage) {

        DdlPackBean packBean = getAllBaseData(mesPackage.getOrganizeCode());
        if (!StringUtils.isEmpty(mesPackage.getPartNo())) {
            DdlPreparedPack.getStringLikerPack(mesPackage.getPartNo(), "partNo", packBean);
        }

        if (!StringUtils.isEmpty(mesPackage.getPackLevel())) {
            DdlPreparedPack.getNumEqualPack(mesPackage.getPackLevel(), "packLevel", packBean);
        }

        if (!StringUtils.isEmpty(mesPackage.getPackageNo())) {
            DdlPreparedPack.getStringLikerPack(mesPackage.getPackageNo(), "packageNo", packBean);
        }

        if (!StringUtils.isEmpty(mesPackage.getWorkCenterCode())) {
            DdlPreparedPack.getStringLikerPack(mesPackage.getWorkCenterCode(), "workCenterCode", packBean);
        }

        // 时间段查询
        DdlPreparedPack.timeBuilder(
                mesPackage.getCreateDateTimeStart(),
                mesPackage.getCreateDateTimeEnd(),
                "createDatetime", packBean, true);

        return packBean;
    }


    /**
     * MES班组信息查询条件封装
     *
     * @param shiftGroup
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getMesShiftGroup(MesShiftGroup shiftGroup, String organizeCode) {

        DdlPackBean packBean = getAllBaseDataByNormalPro(shiftGroup, organizeCode);
        if (!StringUtils.isEmpty(shiftGroup.getGroupCode())) {
            DdlPreparedPack.getStringLikerPack(shiftGroup.getGroupCode(), "groupCode", packBean);
        }
        if (!StringUtils.isEmpty(shiftGroup.getGroupName())) {
            DdlPreparedPack.getStringLikerPack(shiftGroup.getGroupName(), "groupName", packBean);
        }
        if (!StringUtils.isEmpty(shiftGroup.getWorkCenterCode())) {
            DdlPreparedPack.getStringLikerPack(shiftGroup.getWorkCenterCode(), "workCenterCode", packBean);
        }
        return packBean;
    }

    public static DdlPackBean getMesEventByButtonCode(String buttonCode, String org) {
        DdlPackBean packBean = getAllBaseData(org);
        DdlPreparedPack.getStringEqualPack(buttonCode, "buttonCode", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", packBean);
        return packBean;
    }



    /**
     * 按条件分页查询类型信息数据
     * @param typeCfg
     * @return
     */
    public static DdlPackBean getTypeCfg(MesTypeCfg typeCfg) {

        DdlPackBean packBean = getAllBaseData(typeCfg.getOrganizeCode());
        if (!StringUtils.isEmpty(typeCfg.getBusinessTypeCode())) {
            DdlPreparedPack.getStringLikerPack(typeCfg.getBusinessTypeCode(), "businessTypeCode", packBean);
        }
        if (!StringUtils.isEmpty(typeCfg.getBusinessTypeName())) {
            DdlPreparedPack.getStringLikerPack(typeCfg.getBusinessTypeName(), "businessTypeName", packBean);
        }
        if (!StringUtils.isEmpty(typeCfg.getTypeCode())) {
            DdlPreparedPack.getStringLikerPack(typeCfg.getTypeCode(), "typeCode", packBean);
        }

        return packBean;
    }
}
