8 天以前 7ac5fb62d3ac852ec9505eb5c1999064aafee85f
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/batch/MesWmBatchServiceImpl.java
@@ -6,9 +6,14 @@
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchGenerateReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchProcessTraceRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchPurchaseSourceRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchSalesTargetRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.mdm.MesMdmItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.batch.MesWmBatchDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.sn.MesWmSnDO;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.batch.MesWmBatchMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.sn.MesWmSnMapper;
import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants;
import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum;
import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService;
@@ -24,6 +29,7 @@
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -34,7 +40,7 @@
/**
 * 批次管理 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -51,6 +57,8 @@
    @Resource
    private MesMdmItemService mdmItemService;
    @Resource
    private MesWmSnMapper snMapper;
    @Resource
    private MdmItemBatchConfigApi itemBatchConfigApi;
    @Resource
@@ -97,122 +105,107 @@
        // 2. 根据配置校验必填参数并清空不需要的参数
        MesWmBatchDO batch = MesWmBatchDO.builder().itemId(reqVO.getItemId()).build();
        if (config != null) {
            // ========== 必填校验(flag=true 表示该字段为必填) ==========
            // 生产日期
            if (Boolean.TRUE.equals(config.getProduceDateFlag())) {
                if (ObjUtil.isNull(reqVO.getProduceDate())) {
                    throw exception(WM_BATCH_PRODUCE_DATE_REQUIRED);
                }
                batch.setProduceDate(reqVO.getProduceDate());
            if (Boolean.TRUE.equals(config.getProduceDateFlag()) && ObjUtil.isNull(reqVO.getProduceDate())) {
                throw exception(WM_BATCH_PRODUCE_DATE_REQUIRED);
            }
            // 入库日期
            if (Boolean.TRUE.equals(config.getReceiptDateFlag())) {
                if (ObjUtil.isNull(reqVO.getReceiptDate())) {
                    throw exception(WM_BATCH_RECEIPT_DATE_REQUIRED);
                }
                batch.setReceiptDate(reqVO.getReceiptDate());
            if (Boolean.TRUE.equals(config.getReceiptDateFlag()) && ObjUtil.isNull(reqVO.getReceiptDate())) {
                throw exception(WM_BATCH_RECEIPT_DATE_REQUIRED);
            }
            // 有效期
            if (Boolean.TRUE.equals(config.getExpireDateFlag())) {
                if (ObjUtil.isNull(reqVO.getExpireDate())) {
                    throw exception(WM_BATCH_EXPIRE_DATE_REQUIRED);
                }
                batch.setExpireDate(reqVO.getExpireDate());
            if (Boolean.TRUE.equals(config.getExpireDateFlag()) && ObjUtil.isNull(reqVO.getExpireDate())) {
                throw exception(WM_BATCH_EXPIRE_DATE_REQUIRED);
            }
            // 供应商
            if (Boolean.TRUE.equals(config.getVendorFlag())) {
                if (ObjUtil.isNull(reqVO.getVendorId())) {
                    throw exception(WM_BATCH_VENDOR_REQUIRED);
                }
                batch.setVendorId(reqVO.getVendorId());
            if (Boolean.TRUE.equals(config.getVendorFlag()) && ObjUtil.isNull(reqVO.getVendorId())) {
                throw exception(WM_BATCH_VENDOR_REQUIRED);
            }
            // 客户
            if (Boolean.TRUE.equals(config.getClientFlag())) {
                if (ObjUtil.isNull(reqVO.getClientId())) {
                    throw exception(WM_BATCH_CLIENT_REQUIRED);
                }
                batch.setClientId(reqVO.getClientId());
            if (Boolean.TRUE.equals(config.getClientFlag()) && ObjUtil.isNull(reqVO.getClientId())) {
                throw exception(WM_BATCH_CLIENT_REQUIRED);
            }
            // 采购订单编号
            if (Boolean.TRUE.equals(config.getPurchaseOrderCodeFlag())) {
                if (ObjUtil.isNull(reqVO.getPurchaseOrderCode())) {
                    throw exception(WM_BATCH_PURCHASE_ORDER_CODE_REQUIRED);
                }
                batch.setPurchaseOrderCode(reqVO.getPurchaseOrderCode());
            if (Boolean.TRUE.equals(config.getPurchaseOrderCodeFlag()) && ObjUtil.isNull(reqVO.getPurchaseOrderCode())) {
                throw exception(WM_BATCH_PURCHASE_ORDER_CODE_REQUIRED);
            }
            // 销售订单编号
            if (Boolean.TRUE.equals(config.getSalesOrderCodeFlag())) {
                if (ObjUtil.isNull(reqVO.getSalesOrderCode())) {
                    throw exception(WM_BATCH_CUSTOMER_ORDER_CODE_REQUIRED);
                }
                batch.setSalesOrderCode(reqVO.getSalesOrderCode());
            if (Boolean.TRUE.equals(config.getSalesOrderCodeFlag()) && ObjUtil.isNull(reqVO.getSalesOrderCode())) {
                throw exception(WM_BATCH_CUSTOMER_ORDER_CODE_REQUIRED);
            }
            // 生产工单
            if (Boolean.TRUE.equals(config.getWorkOrderFlag())) {
                if (ObjUtil.isNull(reqVO.getWorkOrderId())) {
                    throw exception(WM_BATCH_WORK_ORDER_REQUIRED);
                }
                batch.setWorkOrderId(reqVO.getWorkOrderId());
            if (Boolean.TRUE.equals(config.getWorkOrderFlag()) && ObjUtil.isNull(reqVO.getWorkOrderId())) {
                throw exception(WM_BATCH_WORK_ORDER_REQUIRED);
            }
            // 生产任务
            if (Boolean.TRUE.equals(config.getTaskFlag())) {
                if (ObjUtil.isNull(reqVO.getTaskId())) {
                    throw exception(WM_BATCH_TASK_REQUIRED);
                }
                batch.setTaskId(reqVO.getTaskId());
            if (Boolean.TRUE.equals(config.getTaskFlag()) && ObjUtil.isNull(reqVO.getTaskId())) {
                throw exception(WM_BATCH_TASK_REQUIRED);
            }
            // 工作站
            if (Boolean.TRUE.equals(config.getWorkstationFlag())) {
                if (ObjUtil.isNull(reqVO.getWorkstationId())) {
                    throw exception(WM_BATCH_WORKSTATION_REQUIRED);
                }
                batch.setWorkstationId(reqVO.getWorkstationId());
            if (Boolean.TRUE.equals(config.getWorkstationFlag()) && ObjUtil.isNull(reqVO.getWorkstationId())) {
                throw exception(WM_BATCH_WORKSTATION_REQUIRED);
            }
            // 工具
            if (Boolean.TRUE.equals(config.getToolFlag())) {
                if (ObjUtil.isNull(reqVO.getToolId())) {
                    throw exception(WM_BATCH_TOOL_REQUIRED);
                }
                batch.setToolId(reqVO.getToolId());
            if (Boolean.TRUE.equals(config.getToolFlag()) && ObjUtil.isNull(reqVO.getToolId())) {
                throw exception(WM_BATCH_TOOL_REQUIRED);
            }
            // 模具
            if (Boolean.TRUE.equals(config.getMoldFlag())) {
                if (ObjUtil.isNull(reqVO.getMoldId())) {
                    throw exception(WM_BATCH_MOLD_REQUIRED);
                }
                batch.setMoldId(reqVO.getMoldId());
            if (Boolean.TRUE.equals(config.getMoldFlag()) && ObjUtil.isNull(reqVO.getMoldId())) {
                throw exception(WM_BATCH_MOLD_REQUIRED);
            }
            // 生产批号
            if (Boolean.TRUE.equals(config.getLotNumberFlag())) {
                if (ObjUtil.isNull(reqVO.getLotNumber())) {
                    throw exception(WM_BATCH_LOT_NUMBER_REQUIRED);
                }
                batch.setLotNumber(reqVO.getLotNumber());
            if (Boolean.TRUE.equals(config.getLotNumberFlag()) && ObjUtil.isNull(reqVO.getLotNumber())) {
                throw exception(WM_BATCH_LOT_NUMBER_REQUIRED);
            }
            // 质量状态
            if (Boolean.TRUE.equals(config.getQualityStatusFlag())) {
                if (ObjUtil.isNull(reqVO.getQualityStatus())) {
                    throw exception(WM_BATCH_QUALITY_STATUS_REQUIRED);
                }
                batch.setQualityStatus(reqVO.getQualityStatus());
            if (Boolean.TRUE.equals(config.getQualityStatusFlag()) && ObjUtil.isNull(reqVO.getQualityStatus())) {
                throw exception(WM_BATCH_QUALITY_STATUS_REQUIRED);
            }
        }
        // 无批次配置时,使用传入的参数(宽松模式,适用于自定义入库等场景)
        else {
            if (reqVO.getProduceDate() != null) {
                batch.setProduceDate(reqVO.getProduceDate());
            }
            if (reqVO.getReceiptDate() != null) {
                batch.setReceiptDate(reqVO.getReceiptDate());
            }
            if (reqVO.getExpireDate() != null) {
                batch.setExpireDate(reqVO.getExpireDate());
            }
            if (reqVO.getVendorId() != null) {
                batch.setVendorId(reqVO.getVendorId());
            }
            if (reqVO.getClientId() != null) {
                batch.setClientId(reqVO.getClientId());
            }
        // ========== 保存字段(无论有无配置,只要调用方传了就保存) ==========
        if (reqVO.getProduceDate() != null) {
            batch.setProduceDate(reqVO.getProduceDate());
        }
        if (reqVO.getReceiptDate() != null) {
            batch.setReceiptDate(reqVO.getReceiptDate());
        }
        if (reqVO.getExpireDate() != null) {
            batch.setExpireDate(reqVO.getExpireDate());
        }
        if (reqVO.getVendorId() != null) {
            batch.setVendorId(reqVO.getVendorId());
        }
        if (reqVO.getClientId() != null) {
            batch.setClientId(reqVO.getClientId());
        }
        if (reqVO.getPurchaseOrderCode() != null) {
            batch.setPurchaseOrderCode(reqVO.getPurchaseOrderCode());
        }
        if (reqVO.getSalesOrderCode() != null) {
            batch.setSalesOrderCode(reqVO.getSalesOrderCode());
        }
        if (reqVO.getWorkOrderId() != null) {
            batch.setWorkOrderId(reqVO.getWorkOrderId());
        }
        if (reqVO.getTaskId() != null) {
            batch.setTaskId(reqVO.getTaskId());
        }
        if (reqVO.getWorkstationId() != null) {
            batch.setWorkstationId(reqVO.getWorkstationId());
        }
        if (reqVO.getToolId() != null) {
            batch.setToolId(reqVO.getToolId());
        }
        if (reqVO.getMoldId() != null) {
            batch.setMoldId(reqVO.getMoldId());
        }
        if (reqVO.getLotNumber() != null) {
            batch.setLotNumber(reqVO.getLotNumber());
        }
        if (reqVO.getQualityStatus() != null) {
            batch.setQualityStatus(reqVO.getQualityStatus());
        }
        // 3.1 情况一:传入了 batchCode,查询是否存在该批次号
@@ -387,4 +380,39 @@
        return batchMapper.selectCountByToolId(toolId);
    }
    @Override
    public List<MesWmBatchPurchaseSourceRespVO> getPurchaseSourceList(String code) {
        return batchMapper.selectPurchaseSourceList(code);
    }
    @Override
    public List<MesWmBatchSalesTargetRespVO> getSalesTargetList(String code) {
        return batchMapper.selectSalesTargetList(code);
    }
    @Override
    public List<MesWmBatchProcessTraceRespVO> getProcessTraceList(String code) {
        List<MesWmBatchProcessTraceRespVO> list = batchMapper.selectProcessTraceList(code);
        if (CollUtil.isEmpty(list)) {
            return list;
        }
        // 按工单、工序序号、事件时间排序(sort 可能为空,空值兜底)
        list.sort(Comparator.comparing(MesWmBatchProcessTraceRespVO::getWorkOrderId,
                        Comparator.nullsLast(Comparator.naturalOrder()))
                .thenComparing(MesWmBatchProcessTraceRespVO::getProcessSort,
                        Comparator.nullsLast(Comparator.naturalOrder()))
                .thenComparing(MesWmBatchProcessTraceRespVO::getEventTime,
                        Comparator.nullsLast(Comparator.naturalOrder())));
        return list;
    }
    @Override
    public MesWmBatchDO getBatchBySn(String sn) {
        MesWmSnDO snDO = snMapper.selectOne(MesWmSnDO::getCode, sn);
        if (snDO == null || StrUtil.isBlank(snDO.getBatchCode())) {
            return null;
        }
        return batchMapper.selectByCode(snDO.getBatchCode());
    }
}