| | |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | |
| | |
| | | MesWmMaterialStockDO materialStock; |
| | | MesWmTransactionTypeEnum transactionType = MesWmTransactionTypeEnum.valueOf(reqDTO.getType()); |
| | | boolean isInbound = transactionType != null && transactionType.isInbound(); |
| | | boolean forceCreateNewStock = Boolean.TRUE.equals(reqDTO.getForceCreateNewStock()); |
| | | |
| | | if (reqDTO.getMaterialStockId() != null) { |
| | | // 传入了 materialStockId,直接使用 |
| | | materialStock = materialStockService.getMaterialStock(reqDTO.getMaterialStockId()); |
| | | if (materialStock == null) { |
| | | throw exception(WM_MATERIAL_STOCK_NOT_EXISTS); |
| | | } |
| | | // 校验物料、批次、仓库等维度一致(防止传入错误的 materialStockId) |
| | | // 校验物料、批次、仓库等维度一致(防止传入错误的 materialStockId)。 |
| | | // 若此处不一致,通常是该库存已被移库/合并/拆分,需退回重新拣货,故给出针对性提示 |
| | | if (ObjUtil.notEqual(materialStock.getItemId(), reqDTO.getItemId()) |
| | | || ObjUtil.notEqual(materialStock.getWarehouseId(), reqDTO.getWarehouseId()) |
| | | || ObjUtil.notEqual(materialStock.getLocationId(), reqDTO.getLocationId()) |
| | | || ObjUtil.notEqual(materialStock.getAreaId(), reqDTO.getAreaId())) { |
| | | throw exception(WM_MATERIAL_STOCK_SELECTION_MISMATCH); |
| | | throw new ServiceException(WM_MATERIAL_STOCK_SELECTION_MISMATCH.getCode(), |
| | | "该库存记录已变动(可能被移库/合并/拆分),与出库明细不一致,无法扣减,请退回重新拣货"); |
| | | } |
| | | } else if (isInbound) { |
| | | // 入库事务:找不到则自动创建库存记录 |
| | | materialStock = materialStockService.getOrCreateMaterialStock( |
| | | reqDTO.getItemId(), reqDTO.getWarehouseId(), reqDTO.getLocationId(), reqDTO.getAreaId(), |
| | | reqDTO.getBatchId(), reqDTO.getBatchCode(), reqDTO.getVendorId(), reqDTO.getReceiptTime()); |
| | | // 入库事务:根据 forceCreateNewStock 标志决定是创建新记录还是查找/创建 |
| | | if (forceCreateNewStock) { |
| | | materialStock = materialStockService.createMaterialStock( |
| | | reqDTO.getItemId(), reqDTO.getWarehouseId(), reqDTO.getLocationId(), reqDTO.getAreaId(), |
| | | reqDTO.getBatchId(), reqDTO.getBatchCode(), reqDTO.getVendorId(), reqDTO.getReceiptTime()); |
| | | } else { |
| | | materialStock = materialStockService.getOrCreateMaterialStock( |
| | | reqDTO.getItemId(), reqDTO.getWarehouseId(), reqDTO.getLocationId(), reqDTO.getAreaId(), |
| | | reqDTO.getBatchId(), reqDTO.getBatchCode(), reqDTO.getVendorId(), reqDTO.getReceiptTime()); |
| | | } |
| | | } else { |
| | | // 出库事务:库存记录必须已存在,不允许自动创建(除非 checkFlag=false 允许负库存) |
| | | materialStock = materialStockService.getMaterialStockByCompositeKey( |