From 6a223fb82a14ba6de78716f75f09e76d59d0f7d1 Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Sat, 31 Jan 2026 11:46:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(mat):=20=E4=BC=98=E5=8C=96=E7=89=A9?= =?UTF-8?q?=E6=96=99=E5=BA=93=E5=AD=98=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加物料ID非空校验,避免空指针异常 - 移除按物料ID分组的复杂逻辑,简化查询流程 - 统一采购单查询条件,增加删除标记过滤 - 优化计划总数、已入库数和在途数的计算逻辑 - 移除不必要的PO到VO转换注释 - 重构代码结构,提高可读性和性能 --- .../service/impl/MatMaterialServiceImpl.java | 110 ++++++++---------- 1 file changed, 50 insertions(+), 60 deletions(-) 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; } }