diff --git a/ruoyi-admin/src/main/java/com/ruoyi/purchase/mapper/PurchasePlanChildMapper.java b/ruoyi-admin/src/main/java/com/ruoyi/purchase/mapper/PurchasePlanChildMapper.java index 72e8c520..930451b9 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/purchase/mapper/PurchasePlanChildMapper.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/purchase/mapper/PurchasePlanChildMapper.java @@ -81,4 +81,9 @@ public interface PurchasePlanChildMapper List getPurchasePlanChildByPlanCodes(String[] purchasePlanCodes); + /* + * + * 通过物料号和采购计划单号查询采购计划子表数据 + * */ + PurchasePlanChild selectPurchasePlanChildByCodeAndNo(PurchasePlanChild purchasePlanChild); } 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 29ecf592..6a0b4fb7 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 @@ -2,9 +2,7 @@ package com.ruoyi.purchase.service.impl; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; import com.ruoyi.common.core.redis.RedisCache; @@ -16,7 +14,9 @@ import com.ruoyi.purchase.domain.PurchasePlanChild; import com.ruoyi.purchase.mapper.PurchasePlanChildMapper; import com.ruoyi.system.domain.SysMakeOrder; import com.ruoyi.system.domain.SysMakeorderBom; +import com.ruoyi.system.domain.SysSalesOrderChild; import com.ruoyi.system.mapper.SysMakeOrderMapper; +import com.ruoyi.system.mapper.SysSalesOrderChildMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.purchase.mapper.PurchasePlanMapper; @@ -45,6 +45,9 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService @Autowired private RedisCache redisCache; + @Autowired + private SysSalesOrderChildMapper salesOrderChildMapper; + /** * 查询采购计划单 * @@ -164,7 +167,6 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService // 假设此处有一个生成唯一采购计划单号的方法 String purchasePlanCode = generateUniquePurchasePlanCode(); - //物料相同的只保留一条 List uniqueBoms = insertedSysMakeorderBoms.stream() // 分组依据为MaterialNo .collect(Collectors.groupingBy(SysMakeorderBom::getMaterialNo)) @@ -175,29 +177,95 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService .filter(Objects::nonNull) .collect(Collectors.toList()); + //过滤掉 只有采购的类型 + List filterUniqueBoms = uniqueBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); + //统计物料种类的数量 + Integer materialAmount = filterUniqueBoms.size(); + + // 仅查询一次,因为所有SysMakeorderBom都关联同一生产单号 - SysMakeOrder sysMakeOrder = sysMakeOrderMapper.selectMakeOrderByMakeNo(uniqueBoms.get(0).getMakeNo()); + SysMakeOrder sysMakeOrder = sysMakeOrderMapper.selectMakeOrderByMakeNo(insertedSysMakeorderBoms.get(0).getMakeNo()); //保存所有加工方式为采购的物料 - List filterSysMakeorderBoms = uniqueBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); - //统计物料种类的数量 - Integer materialAmount = filterSysMakeorderBoms.size(); + List filterSysMakeorderBoms = insertedSysMakeorderBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); + + + //所有数据的销售订单号相同,只需查一条的销售订单号 + String quoteId = filterSysMakeorderBoms.get(0).getSalesOrderCode(); + + // 第一步:收集所有不同的salesOrderMaterialNo 利用set key值不重复的特性 + Set uniqueSalesOrderMaterialNos = new HashSet<>(); + for (SysMakeorderBom filterSysMakeorderBom : filterSysMakeorderBoms) { + uniqueSalesOrderMaterialNos.add(filterSysMakeorderBom.getSalesOrderMaterialNo()); + } + + // 第二步:查询每个salesOrderMaterialNo对应的SalesOrder数据 + Map salesOrderChildByMaterialNo = new HashMap<>(); + for (String salesOrderMaterialNo : uniqueSalesOrderMaterialNos) { + SysSalesOrderChild sysSalesOrderChild = new SysSalesOrderChild(); + sysSalesOrderChild.setMaterialCode(salesOrderMaterialNo); + sysSalesOrderChild.setQuoteId(quoteId); + SysSalesOrderChild salesOrderChild = salesOrderChildMapper.selectSalesOrderChildByCodeAndNo(sysSalesOrderChild); + salesOrderChildByMaterialNo.put(salesOrderMaterialNo, salesOrderChild); + } + + Long purchasePlanMaterialSum = 0L; + // 第三步:计算purchasePlanNum + for (SysMakeorderBom filterSysMakeorderBom : filterSysMakeorderBoms) { + String salesOrderMaterialNo = filterSysMakeorderBom.getSalesOrderMaterialNo(); + SysSalesOrderChild sysSalesOrderChild = salesOrderChildByMaterialNo.get(salesOrderMaterialNo); + if (sysSalesOrderChild != null) { + //物料数量 + long materialNum = sysSalesOrderChild.getMaterialNum(); + //用量 + long useNum = filterSysMakeorderBom.getUseNum(); + + //采购计划数 + long purchasePlanNum = materialNum * useNum; + + // 立即创建PurchasePlanChild对象 + PurchasePlanChild purchasePlanChild = new PurchasePlanChild(); + purchasePlanChild.setPurchasePlanCode(purchasePlanCode); + purchasePlanChild.setMaterialCode(filterSysMakeorderBom.getMaterialNo()); + + PurchasePlanChild tempPurchasePlanChild = purchasePlanChildMapper.selectPurchasePlanChildByCodeAndNo(purchasePlanChild); + if (tempPurchasePlanChild != null){ + + purchasePlanChild.setPurchasePlanChildId(tempPurchasePlanChild.getPurchasePlanChildId()); + long addMaterialNum = tempPurchasePlanChild.getMaterialNum() + purchasePlanNum; + //设置计划采购数 + purchasePlanChild.setMaterialNum(addMaterialNum); + purchasePlanChildMapper.updatePurchasePlanChild(purchasePlanChild); + }else { + purchasePlanChild.setMaterialName(filterSysMakeorderBom.getMaterialName()); + purchasePlanChild.setMaterialType(filterSysMakeorderBom.getMaterialType()); + purchasePlanChild.setWarehouseDept(filterSysMakeorderBom.getWarehouseDept()); + purchasePlanChild.setProcessMethod("0"); + purchasePlanChild.setBrand(filterSysMakeorderBom.getBrand()); + purchasePlanChild.setDescribe(filterSysMakeorderBom.getDescribe()); + purchasePlanChild.setUnit(filterSysMakeorderBom.getUnit()); + purchasePlanChild.setWarehouseDept(filterSysMakeorderBom.getWarehouseDept()); + // 物料的计划采购数,使用计算出的purchasePlanNum + purchasePlanChild.setMaterialNum(purchasePlanNum); + purchasePlanChild.setCreateBy(ShiroUtils.getLoginName()); + purchasePlanChild.setCreateTime(new Date()); + purchasePlanChild.setUnit(filterSysMakeorderBom.getUnit()); + + // 直接保存PurchasePlanChild对象 + purchasePlanChildMapper.insertPurchasePlanChild(purchasePlanChild); + } + + // 更新总和 + purchasePlanMaterialSum += purchasePlanNum; + } + } - //统计数量合计 - //生产订单中的订单数量 - Long makeOrderMaterialSum = sysMakeOrder.getMaterialSum(); - //加工方式为采购的所有物料用量合计 - Long sum = filterSysMakeorderBoms.stream().mapToLong(SysMakeorderBom::getUseNum).sum(); - Long purchasePlanMaterialSum = makeOrderMaterialSum * sum; // 创建采购计划主记录 PurchasePlan purchasePlan = buildPurchasePlan(sysMakeOrder, purchasePlanCode,materialAmount,purchasePlanMaterialSum); - purchasePlanMapper.insertPurchasePlan(purchasePlan); - // 批量创建采购计划子项 - insertPurchasePlanChildren(uniqueBoms, purchasePlanCode, sysMakeOrder); } /*假设自生成的采购计划单号*/ @@ -228,44 +296,4 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService return purchasePlan; } - 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 : uniqueBoms) { - if ("0".equals(bom.getProcessMethod())) { - PurchasePlanChild purchasePlanChild = new PurchasePlanChild(); - purchasePlanChild.setPurchasePlanCode(purchasePlanCode); - purchasePlanChild.setMaterialCode(bom.getMaterialNo()); - purchasePlanChild.setMaterialName(bom.getMaterialName()); - purchasePlanChild.setMaterialType(bom.getMaterialType()); - //新增物料入库部门 - purchasePlanChild.setWarehouseDept(bom.getWarehouseDept()); - purchasePlanChild.setProcessMethod("0"); - purchasePlanChild.setBrand(bom.getBrand()); - purchasePlanChild.setDescribe(bom.getDescribe()); - purchasePlanChild.setUnit(bom.getUnit()); - purchasePlanChild.setWarehouseDept(bom.getWarehouseDept()); - //物料用量 - Long useNum = bom.getUseNum(); - //物料的计划采购数 - purchasePlanChild.setMaterialNum(useNum * makeOrderMaterialSum); - purchasePlanChild.setCreateBy(ShiroUtils.getLoginName()); - purchasePlanChild.setCreateTime(new Date()); - purchasePlanChild.setUnit(bom.getUnit()); - purchasePlanChildMapper.insertPurchasePlanChild(purchasePlanChild); - } - } - } - } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysSalesOrderChildMapper.java b/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysSalesOrderChildMapper.java index 9d455049..8cdb9959 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysSalesOrderChildMapper.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysSalesOrderChildMapper.java @@ -23,4 +23,8 @@ public interface SysSalesOrderChildMapper { int deleteSysSalesOrderChildByQuoteIds(String[] ids); SysSalesOrderChild selectOneByQuoteIdAndMaterialCode(@Param("quoteId") String quoteId, @Param("materialCode") String materialCode); + /* + * 根据物料号和销售订单号查询销售订单子表数据 + * */ + SysSalesOrderChild selectSalesOrderChildByCodeAndNo(SysSalesOrderChild sysSalesOrderChild); } diff --git a/ruoyi-admin/src/main/resources/mapper/purchase/PurchasePlanChildMapper.xml b/ruoyi-admin/src/main/resources/mapper/purchase/PurchasePlanChildMapper.xml index 656f4fe1..6faa76ab 100644 --- a/ruoyi-admin/src/main/resources/mapper/purchase/PurchasePlanChildMapper.xml +++ b/ruoyi-admin/src/main/resources/mapper/purchase/PurchasePlanChildMapper.xml @@ -73,6 +73,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + + + insert into purchase_plan_child diff --git a/ruoyi-admin/src/main/resources/mapper/system/SysSalesOrderChildMapper.xml b/ruoyi-admin/src/main/resources/mapper/system/SysSalesOrderChildMapper.xml index 958b9309..862af4b3 100644 --- a/ruoyi-admin/src/main/resources/mapper/system/SysSalesOrderChildMapper.xml +++ b/ruoyi-admin/src/main/resources/mapper/system/SysSalesOrderChildMapper.xml @@ -75,6 +75,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where a.quoteId = #{quoteId} and a.materialCode = #{materialCode} + + + + insert into sys_sales_order_child