|
|
@ -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<SysMakeorderBom> 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<SysMakeorderBom> 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<SysMakeorderBom> filterSysMakeorderBoms = uniqueBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); |
|
|
|
//统计物料种类的数量
|
|
|
|
Integer materialAmount = filterSysMakeorderBoms.size(); |
|
|
|
List<SysMakeorderBom> filterSysMakeorderBoms = insertedSysMakeorderBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
//所有数据的销售订单号相同,只需查一条的销售订单号
|
|
|
|
String quoteId = filterSysMakeorderBoms.get(0).getSalesOrderCode(); |
|
|
|
|
|
|
|
// 第一步:收集所有不同的salesOrderMaterialNo 利用set key值不重复的特性
|
|
|
|
Set<String> uniqueSalesOrderMaterialNos = new HashSet<>(); |
|
|
|
for (SysMakeorderBom filterSysMakeorderBom : filterSysMakeorderBoms) { |
|
|
|
uniqueSalesOrderMaterialNos.add(filterSysMakeorderBom.getSalesOrderMaterialNo()); |
|
|
|
} |
|
|
|
|
|
|
|
// 第二步:查询每个salesOrderMaterialNo对应的SalesOrder数据
|
|
|
|
Map<String, SysSalesOrderChild> 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<SysMakeorderBom> uniqueBoms, String purchasePlanCode, SysMakeOrder sysMakeOrder) { |
|
|
|
|
|
|
|
// List<SysMakeorderBom> 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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|