云
2026-10-29 0a2206a53d5889b6095f4819c263be858d86ca91
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/materialstock/MesWmMaterialStockServiceImpl.java
@@ -8,6 +8,7 @@
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.framework.operatelog.core.util.OperateLogUtils;
import cn.iocoder.yudao.module.mes.controller.admin.wm.materialstock.vo.MesWmMaterialStockAdjustReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.materialstock.vo.MesWmMaterialStockFreezeReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.materialstock.vo.MesWmMaterialStockImportExcelVO;
@@ -40,6 +41,7 @@
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService;
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService;
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService;
import com.mzt.logapi.starter.annotation.LogRecord;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -57,6 +59,7 @@
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.mes.enums.LogRecordConstants.*;
/**
 * MES 库存台账 Service 实现类
@@ -129,6 +132,8 @@
            MesMdItemDO item = mdItemService.getItem(pageReqVO.getItemId());
            if (item != null && item.getMdmItemId() != null) {
                itemIds = SetUtils.asSet(item.getMdmItemId());
            } else {
                return PageResult.empty();
            }
        }
        // 1.3 解析 virtualFilter:转换为虚拟仓 warehouseId
@@ -396,7 +401,7 @@
        } else {
            // 出库时校验批次存在
            if (batchId != null) {
                batchService.validateBatchExists(batchId, reqVO.getItemId());
                batchCode = batchService.validateBatchExists(batchId, reqVO.getItemId()).getCode();
            }
        }
@@ -454,20 +459,26 @@
     * @return 批次记录(物料未启用批次管理时返回 null)
     */
    private MesWmBatchDO resolveBatch(MesWmMaterialStockAdjustReqVO reqVO, MesMdmItemDO item) {
        // 1. 物料未启用批次管理,返回 null
        if (Boolean.FALSE.equals(item.getIsBatchManaged())) {
            return null;
        // 即使物料未启用普通批次管理,也要保留并校验盘号追溯批次。
        if (reqVO.getBatchId() != null) {
            MesWmBatchDO existing = batchService.validateBatchExists(reqVO.getBatchId(), reqVO.getItemId());
            if (StrUtil.isNotBlank(reqVO.getReelNo())
                    && !Objects.equals(StrUtil.trim(reqVO.getReelNo()), StrUtil.trim(existing.getReelNo()))) {
                throw exception(WM_BATCH_REEL_MISMATCH);
            }
            return existing;
        }
        // 2. 传入 batchId,校验并返回已有批次
        if (reqVO.getBatchId() != null) {
            return batchService.validateBatchExists(reqVO.getBatchId(), reqVO.getItemId());
        // 1. 未指定追溯批次且物料未启用批次管理,返回 null
        if (Boolean.FALSE.equals(item.getIsBatchManaged()) && StrUtil.isBlank(reqVO.getReelNo())) {
            return null;
        }
        // 3. 未传 batchId,自动创建批次
        MesWmBatchGenerateReqVO batchReqVO = new MesWmBatchGenerateReqVO()
                .setItemId(reqVO.getItemId())
                .setBatchCode(reqVO.getBatchCode())
                .setReelNo(StrUtil.trimToNull(reqVO.getReelNo()))
                .setVendorId(reqVO.getVendorId())
                .setProduceDate(reqVO.getProduceDate() != null ? reqVO.getProduceDate().atStartOfDay() : null)
                .setExpireDate(reqVO.getExpireDate() != null ? reqVO.getExpireDate().atStartOfDay() : null)
@@ -593,6 +604,8 @@
    }
    @Override
    @LogRecord(type = MES_WM_MATERIAL_STOCK_TYPE, subType = MES_WM_MATERIAL_STOCK_IMPORT_SUB_TYPE, bizNo = "0",
            success = MES_WM_MATERIAL_STOCK_IMPORT_SUCCESS)
    public MesWmMaterialStockImportRespVO importMaterialStockList(List<MesWmMaterialStockImportExcelVO> importList) {
        // 操作类型取值:增加 / 减少 / 覆盖
        final String OP_ADD = "增加";
@@ -755,6 +768,8 @@
                respVO.getFailureNames().put(key, ex.getMessage());
            }
        }
        // 记录导入结果汇总,供 @LogRecord 的 success 文案引用
        OperateLogUtils.putImportSummary(respVO.getCreateNames(), respVO.getUpdateNames(), respVO.getFailureNames());
        return respVO;
    }