| | |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemTypeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.batch.MesWmBatchService; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchGenerateReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.batch.MesWmBatchDO; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; |
| | | import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; |
| | |
| | | @Resource |
| | | @Lazy |
| | | private MesWmTransactionService wmTransactionService; |
| | | @Resource |
| | | @Lazy |
| | | private MesWmBatchService batchService; |
| | | |
| | | @Override |
| | | public MesWmMaterialStockDO getMaterialStock(Long id) { |
| | |
| | | // 1. 校验物料存在且启用 |
| | | MesMdmItemDO item = mdmItemService.validateItemExistsAndEnable(reqVO.getItemId()); |
| | | |
| | | // 2. 校验库位混放规则 |
| | | checkAreaMixingRule(reqVO.getAreaId(), reqVO.getItemId(), reqVO.getBatchId()); |
| | | // 2. 处理批次(仅入库时需要处理批次) |
| | | Long batchId = reqVO.getBatchId(); |
| | | String batchCode = reqVO.getBatchCode(); |
| | | if (isInbound) { |
| | | MesWmBatchDO batch = resolveBatch(reqVO, item); |
| | | if (batch != null) { |
| | | batchId = batch.getId(); |
| | | batchCode = batch.getCode(); |
| | | } |
| | | } else { |
| | | // 出库时校验批次存在 |
| | | if (batchId != null) { |
| | | batchService.validateBatchExists(batchId, reqVO.getItemId()); |
| | | } |
| | | } |
| | | |
| | | // 3. 获取或创建库存记录 |
| | | // 3. 校验库位混放规则 |
| | | checkAreaMixingRule(reqVO.getAreaId(), reqVO.getItemId(), batchId); |
| | | |
| | | // 4. 获取或创建库存记录 |
| | | MesWmMaterialStockDO stock = getOrCreateMaterialStock( |
| | | reqVO.getItemId(), |
| | | reqVO.getWarehouseId(), |
| | | reqVO.getLocationId(), |
| | | reqVO.getAreaId(), |
| | | reqVO.getBatchId(), |
| | | reqVO.getBatchCode(), |
| | | batchId, |
| | | batchCode, |
| | | reqVO.getVendorId(), |
| | | LocalDateTime.now() |
| | | ); |
| | | |
| | | // 4. 计算变动数量:入库为正,出库为负 |
| | | // 5. 计算变动数量:入库为正,出库为负 |
| | | BigDecimal quantity = isInbound ? reqVO.getQuantity() : reqVO.getQuantity().negate(); |
| | | |
| | | // 5. 更新库存数量(出库时校验库存充足) |
| | | updateMaterialStockQuantity(stock.getId(), quantity, !isInbound); |
| | | |
| | | // 6. 创建库存事务流水 |
| | | // 6. 创建库存事务流水(内部会更新库存数量) |
| | | int bizType = isInbound ? MesBizTypeConstants.WM_STOCK_ADJUST_IN : MesBizTypeConstants.WM_STOCK_ADJUST_OUT; |
| | | MesWmTransactionSaveReqDTO transaction = new MesWmTransactionSaveReqDTO() |
| | | .setType(isInbound ? MesWmTransactionTypeEnum.IN.getType() : MesWmTransactionTypeEnum.OUT.getType()) |
| | |
| | | .setBizLineId(stock.getId()) |
| | | .setItemId(reqVO.getItemId()) |
| | | .setQuantity(quantity) |
| | | .setBatchId(reqVO.getBatchId()) |
| | | .setBatchCode(reqVO.getBatchCode()) |
| | | .setBatchId(batchId) |
| | | .setBatchCode(batchCode) |
| | | .setWarehouseId(reqVO.getWarehouseId()) |
| | | .setLocationId(reqVO.getLocationId()) |
| | | .setAreaId(reqVO.getAreaId()) |
| | |
| | | return stock.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 解析批次 |
| | | * <p> |
| | | * 入库时的批次处理逻辑: |
| | | * 1. 物料未启用批次管理:返回 null |
| | | * 2. 传入 batchId:校验并使用已有批次 |
| | | * 3. 未传 batchId:根据 batchCode 等参数自动创建批次 |
| | | * |
| | | * @param reqVO 入库请求 |
| | | * @param item 物料信息 |
| | | * @return 批次记录(物料未启用批次管理时返回 null) |
| | | */ |
| | | private MesWmBatchDO resolveBatch(MesWmMaterialStockAdjustReqVO reqVO, MesMdmItemDO item) { |
| | | // 1. 物料未启用批次管理,返回 null |
| | | if (Boolean.FALSE.equals(item.getIsBatchManaged())) { |
| | | return null; |
| | | } |
| | | |
| | | // 2. 传入 batchId,校验并返回已有批次 |
| | | if (reqVO.getBatchId() != null) { |
| | | return batchService.validateBatchExists(reqVO.getBatchId(), reqVO.getItemId()); |
| | | } |
| | | |
| | | // 3. 未传 batchId,自动创建批次 |
| | | MesWmBatchGenerateReqVO batchReqVO = new MesWmBatchGenerateReqVO() |
| | | .setItemId(reqVO.getItemId()) |
| | | .setBatchCode(reqVO.getBatchCode()) |
| | | .setVendorId(reqVO.getVendorId()) |
| | | .setProduceDate(reqVO.getProduceDate() != null ? reqVO.getProduceDate().atStartOfDay() : null) |
| | | .setExpireDate(reqVO.getExpireDate() != null ? reqVO.getExpireDate().atStartOfDay() : null) |
| | | .setReceiptDate(LocalDateTime.now()) |
| | | .setLotNumber(reqVO.getLotNumber()) |
| | | .setQualityStatus(parseQualityStatus(reqVO.getQualityStatus())); |
| | | return batchService.getOrGenerateBatchCode(batchReqVO); |
| | | } |
| | | |
| | | /** |
| | | * 解析质量状态(支持数字或字符串) |
| | | */ |
| | | private Integer parseQualityStatus(Object qualityStatus) { |
| | | if (qualityStatus == null) { |
| | | return null; |
| | | } |
| | | if (qualityStatus instanceof Integer) { |
| | | return (Integer) qualityStatus; |
| | | } |
| | | if (qualityStatus instanceof String) { |
| | | String str = (String) qualityStatus; |
| | | // 支持数字字符串 |
| | | if (str.matches("\\d+")) { |
| | | return Integer.parseInt(str); |
| | | } |
| | | // 支持常见字符串映射 |
| | | switch (str.toUpperCase()) { |
| | | case "PENDING": |
| | | case "待检验": |
| | | case "待检": |
| | | return 0; |
| | | case "PASS": |
| | | case "QUALIFIED": |
| | | case "合格": |
| | | return 1; |
| | | case "FAIL": |
| | | case "UNQUALIFIED": |
| | | case "不合格": |
| | | return 2; |
| | | default: |
| | | throw new IllegalArgumentException("无效的质量状态: " + str); |
| | | } |
| | | } |
| | | throw new IllegalArgumentException("无效的质量状态类型: " + qualityStatus.getClass().getName()); |
| | | } |
| | | |
| | | } |