diff --git a/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchasePlanServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchasePlanServiceImpl.java index 8aa6a405..dbd40f0d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchasePlanServiceImpl.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/purchase/service/impl/PurchasePlanServiceImpl.java @@ -4,6 +4,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import com.ruoyi.common.core.redis.RedisCache; @@ -161,11 +162,22 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService // 假设此处有一个生成唯一采购计划单号的方法 String purchasePlanCode = generateUniquePurchasePlanCode(); + //物料相同的只保留一条 + List uniqueBoms = insertedSysMakeorderBoms.stream() + // 分组依据为MaterialNo + .collect(Collectors.groupingBy(SysMakeorderBom::getMaterialNo)) + // 把每个分组转为流,然后取第一个元素 + .values().stream() + .map(groupedBoms -> groupedBoms.stream().findFirst().orElse(null)) + // 过滤掉null值,以防万一某些分组是空的 + .filter(Objects::nonNull) + .collect(Collectors.toList()); + // 仅查询一次,因为所有SysMakeorderBom都关联同一生产单号 - SysMakeOrder sysMakeOrder = sysMakeOrderMapper.selectMakeOrderByMakeNo(insertedSysMakeorderBoms.get(0).getMakeNo()); + SysMakeOrder sysMakeOrder = sysMakeOrderMapper.selectMakeOrderByMakeNo(uniqueBoms.get(0).getMakeNo()); //保存所有加工方式为采购的物料 - List filterSysMakeorderBoms = insertedSysMakeorderBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); + List filterSysMakeorderBoms = uniqueBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); //统计物料种类的数量 Integer materialAmount = filterSysMakeorderBoms.size(); @@ -183,7 +195,7 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService purchasePlanMapper.insertPurchasePlan(purchasePlan); // 批量创建采购计划子项 - insertPurchasePlanChildren(insertedSysMakeorderBoms, purchasePlanCode, sysMakeOrder); + insertPurchasePlanChildren(uniqueBoms, purchasePlanCode, sysMakeOrder); } /*假设自生成的采购计划单号*/ @@ -214,11 +226,21 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService return purchasePlan; } - private void insertPurchasePlanChildren(List insertedSysMakeorderBoms, String purchasePlanCode, SysMakeOrder sysMakeOrder) { + private void insertPurchasePlanChildren(List uniqueBoms, String purchasePlanCode, SysMakeOrder sysMakeOrder) { + +// List uniqueBoms = insertedSysMakeorderBoms.stream() +// // 分组依据为MaterialNo +// .collect(Collectors.groupingBy(SysMakeorderBom::getMaterialNo)) +// // 把每个分组转为流,然后取第一个元素 +// .values().stream() +// .map(groupedBoms -> groupedBoms.stream().findFirst().orElse(null)) +// // 过滤掉null值,以防万一某些分组是空的 +// .filter(Objects::nonNull) +// .collect(Collectors.toList()); //生产订单中的订单数量 Long makeOrderMaterialSum = sysMakeOrder.getMaterialSum(); - for (SysMakeorderBom bom : insertedSysMakeorderBoms) { + for (SysMakeorderBom bom : uniqueBoms) { if ("0".equals(bom.getProcessMethod())) { PurchasePlanChild purchasePlanChild = new PurchasePlanChild(); purchasePlanChild.setPurchasePlanCode(purchasePlanCode);