diff --git a/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialServiceImpl.java b/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialServiceImpl.java index 0a53ad7..34c6dad 100644 --- a/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialServiceImpl.java +++ b/gear-mat/src/main/java/com/gear/mat/service/impl/MatMaterialServiceImpl.java @@ -184,15 +184,19 @@ public class MatMaterialServiceImpl implements IMatMaterialService { // 创建结果列表 List result = new ArrayList<>(); - // 构建采购单查询条件(直接用QueryWrapper,和Service层逻辑保持一致) - QueryWrapper purchaseQw = new QueryWrapper<>(); - if (materialId != null) { - purchaseQw.eq("material_id", materialId); + // 物料ID,先做非空校验 + if (materialId == null) { + return result; } + + // 构建采购单查询条件(materialId + 过滤删除标记) + QueryWrapper purchaseQw = new QueryWrapper<>(); + purchaseQw.eq("material_id", materialId); + purchaseQw.eq("del_flag", 0); // 直接查采购单PO List purchaseListPo = matPurchaseMapper.selectList(purchaseQw); - // 将PO转换为VO,以便后续处理 + // 将PO转换为VO List purchaseList = purchaseListPo.stream() .map(purchase -> { MatPurchaseVo vo = new MatPurchaseVo(); @@ -201,72 +205,58 @@ public class MatMaterialServiceImpl implements IMatMaterialService { }) .collect(Collectors.toList()); - // 按物料ID分组 - Map> purchaseMap = purchaseList.stream() - .collect(Collectors.groupingBy(MatPurchaseVo::getMaterialId)); + // 1. 查询当前物料的详细信息 + MatMaterialVo material = baseMapper.selectVoById(materialId); + // 2. 初始化物料库存VO + MaterialInventoryVo inventory = new MaterialInventoryVo(); + inventory.setMaterialId(materialId); + if (material != null) { + inventory.setMaterialName(material.getMaterialName()); + inventory.setSpec(material.getSpec()); + inventory.setModel(material.getModel()); + inventory.setUnit(material.getUnit()); + inventory.setCurrentStock(material.getCurrentStock()); + } - for (Long matId : purchaseMap.keySet()) { + // 3. 计算该物料的计划总数、已入库总数、在途总数 + BigDecimal planTotal = BigDecimal.ZERO; + BigDecimal receivedTotal = BigDecimal.ZERO; + BigDecimal inTransitTotal = BigDecimal.ZERO; - // 通过采购单中的物料ID查询物料详细信息 - MatMaterialVo material = baseMapper.selectVoById(matId); + for (MatPurchaseVo purchase : purchaseList) { + // 累加计划总数 + BigDecimal planNum = Optional.ofNullable(purchase.getPlanNum()).orElse(BigDecimal.ZERO); + planTotal = planTotal.add(planNum); - MaterialInventoryVo inventory = new MaterialInventoryVo(); - inventory.setMaterialId(matId); + // 查询该采购单下的入库详情 + MatPurchaseInDetailBo detailBo = new MatPurchaseInDetailBo(); + detailBo.setPurchaseId(purchase.getPurchaseId()); + List details = matPurchaseInDetailService.queryList(detailBo); - if (material != null) { - inventory.setMaterialName(material.getMaterialName()); - inventory.setSpec(material.getSpec()); - inventory.setModel(material.getModel()); - inventory.setUnit(material.getUnit()); - inventory.setCurrentStock(material.getCurrentStock()); - } - - List purchases = purchaseMap.get(matId); - - // 计算计划总数 - BigDecimal planTotal = purchases.stream() - .map(MatPurchaseVo::getPlanNum) + // 计算该采购单的已入库数量 + BigDecimal receivedInPurchase = details.stream() + .map(MatPurchaseInDetailVo::getInNum) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); + receivedTotal = receivedTotal.add(receivedInPurchase); - inventory.setPlanNum(planTotal); - - // 计算已入库数量和在途数量 - BigDecimal receivedTotal = BigDecimal.ZERO; - BigDecimal inTransitTotal = BigDecimal.ZERO; - - for (MatPurchaseVo purchase : purchases) { - // 查询该采购单下的入库详情 - MatPurchaseInDetailBo detailBo = new MatPurchaseInDetailBo(); - detailBo.setPurchaseId(purchase.getPurchaseId()); - - List details = matPurchaseInDetailService.queryList(detailBo); - - // 计算该采购单的已入库数量 - BigDecimal receivedInPurchase = details.stream() - .map(MatPurchaseInDetailVo::getInNum) - .filter(Objects::nonNull) - .reduce(BigDecimal.ZERO, BigDecimal::add); - - receivedTotal = receivedTotal.add(receivedInPurchase); - - // 根据采购单状态判断在途数量 - // 1-待入(在途) 2-已完成(全部入库) 3-已取消(归零) 4-部分入库 - if (purchase.getStatus() != null && (purchase.getStatus() == 1 || purchase.getStatus() == 4)) { - // 在途或部分入库,计算未入库数量 - BigDecimal pending = purchase.getPlanNum().subtract(receivedInPurchase); - if (pending.compareTo(BigDecimal.ZERO) > 0) { - inTransitTotal = inTransitTotal.add(pending); - } + // 计算该采购单的在途数量 + if (purchase.getStatus() != null && (purchase.getStatus() == 1 || purchase.getStatus() == 4)) { + BigDecimal pending = planNum.subtract(receivedInPurchase); + if (pending.compareTo(BigDecimal.ZERO) > 0) { + inTransitTotal = inTransitTotal.add(pending); } } - - inventory.setReceivedNum(receivedTotal); - inventory.setInTransitNum(inTransitTotal); - - result.add(inventory); } + // 4. 设置库存VO的数值字段 + inventory.setPlanNum(planTotal); + inventory.setReceivedNum(receivedTotal); + inventory.setInTransitNum(inTransitTotal); + + // 5. 添加到结果列表 + result.add(inventory); + return result; } }