|
|
@ -7,6 +7,7 @@ import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache; |
|
|
|
import com.ruoyi.common.exception.BusinessException; |
|
|
|
import com.ruoyi.common.utils.DateUtils; |
|
|
|
import com.ruoyi.common.utils.ShiroUtils; |
|
|
|
import com.ruoyi.common.utils.StringUtils; |
|
|
@ -26,6 +27,7 @@ import com.ruoyi.purchase.mapper.PurchaseQuoteMapper; |
|
|
|
import com.ruoyi.system.domain.*; |
|
|
|
import com.ruoyi.system.domain.Vo.SysSupplierVo; |
|
|
|
import com.ruoyi.system.mapper.SysMakeOrderMapper; |
|
|
|
import com.ruoyi.system.mapper.SysMakeorderBomMapper; |
|
|
|
import com.ruoyi.system.mapper.SysSalesOrderChildMapper; |
|
|
|
import com.ruoyi.system.mapper.SysSupplierMapper; |
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
@ -75,6 +77,10 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService |
|
|
|
@Autowired |
|
|
|
private SysSupplierMapper sysSupplierMapper; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
private SysMakeorderBomMapper sysMakeorderBomMapper; |
|
|
|
|
|
|
|
/** |
|
|
|
* 查询采购计划单 |
|
|
|
* |
|
|
@ -337,106 +343,85 @@ public class PurchasePlanServiceImpl implements IPurchasePlanService |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* 生产订单工程审核后生成采购计划 |
|
|
|
* 生产订单采购审核后生成采购计划 |
|
|
|
* */ |
|
|
|
@Override |
|
|
|
public void generatePurchasePlanByMakeOrder(List<SysMakeorderBom> insertedSysMakeorderBoms) { |
|
|
|
// 假设此处有一个生成唯一采购计划单号的方法
|
|
|
|
public void generatePurchasePlanByMakeOrder(SysMakeOrder makeOrder) { |
|
|
|
// 生成唯一采购计划单号的方法
|
|
|
|
String purchasePlanCode = generateUniquePurchasePlanCode(); |
|
|
|
String loginName = ShiroUtils.getLoginName(); |
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
//过滤掉 只有采购的类型
|
|
|
|
List<SysMakeorderBom> filterUniqueBoms = uniqueBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); |
|
|
|
//统计物料种类的数量
|
|
|
|
Integer materialAmount = filterUniqueBoms.size(); |
|
|
|
|
|
|
|
// 仅查询一次,因为所有SysMakeorderBom都关联同一生产单号
|
|
|
|
SysMakeOrder sysMakeOrder = sysMakeOrderMapper.selectMakeOrderByMakeNo(insertedSysMakeorderBoms.get(0).getMakeNo()); |
|
|
|
|
|
|
|
//保存所有加工方式为采购的物料
|
|
|
|
List<SysMakeorderBom> filterSysMakeorderBoms = insertedSysMakeorderBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); |
|
|
|
|
|
|
|
if(StringUtils.isNotEmpty(filterUniqueBoms)){ |
|
|
|
//所有数据的销售订单号相同,只需查一条的销售订单号
|
|
|
|
String quoteId = filterSysMakeorderBoms.get(0).getSalesOrderCode(); |
|
|
|
PurchasePlan purchasePlan = new PurchasePlan(); |
|
|
|
|
|
|
|
// 第一步:收集所有不同的salesOrderMaterialNo 利用set key值不重复的特性
|
|
|
|
Set<String> uniqueSalesOrderMaterialNos = new HashSet<>(); |
|
|
|
for (SysMakeorderBom filterSysMakeorderBom : filterSysMakeorderBoms) { |
|
|
|
uniqueSalesOrderMaterialNos.add(filterSysMakeorderBom.getSalesOrderMaterialNo()); |
|
|
|
} |
|
|
|
String makeNo = makeOrder.getMakeNo(); |
|
|
|
|
|
|
|
// 第二步:查询每个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); |
|
|
|
} |
|
|
|
List<SysMakeorderBom> sysMakeorderBoms = sysMakeorderBomMapper.selectSysMakeorderBomByMakeNo(makeNo); |
|
|
|
|
|
|
|
Integer purchasePlanMaterialSum = 0; |
|
|
|
// 第三步:计算purchasePlanNum
|
|
|
|
for (SysMakeorderBom filterSysMakeorderBom : filterSysMakeorderBoms) { |
|
|
|
String salesOrderMaterialNo = filterSysMakeorderBom.getSalesOrderMaterialNo(); |
|
|
|
SysSalesOrderChild sysSalesOrderChild = salesOrderChildByMaterialNo.get(salesOrderMaterialNo); |
|
|
|
if (sysSalesOrderChild != null) { |
|
|
|
//物料数量
|
|
|
|
Integer materialNum = sysSalesOrderChild.getMaterialNum(); |
|
|
|
//用量
|
|
|
|
Integer useNum = filterSysMakeorderBom.getUseNum(); |
|
|
|
//保留 只有采购的类型
|
|
|
|
List<SysMakeorderBom> filterUniqueBoms = sysMakeorderBoms.stream().filter(bom -> "0".equals(bom.getProcessMethod())).collect(Collectors.toList()); |
|
|
|
|
|
|
|
//采购计划数
|
|
|
|
Integer purchasePlanNum = materialNum * useNum; |
|
|
|
List<PurchasePlanChild> purchasePlanChildList = new ArrayList<>(); |
|
|
|
|
|
|
|
// 立即创建PurchasePlanChild对象
|
|
|
|
for (SysMakeorderBom filterUniqueBom : filterUniqueBoms) { |
|
|
|
PurchasePlanChild purchasePlanChild = new PurchasePlanChild(); |
|
|
|
purchasePlanChild.setPurchasePlanCode(purchasePlanCode); |
|
|
|
purchasePlanChild.setMaterialNo(filterSysMakeorderBom.getMaterialNo()); |
|
|
|
|
|
|
|
PurchasePlanChild tempPurchasePlanChild = purchasePlanChildMapper.selectPurchasePlanChildByCodeAndNo(purchasePlanChild); |
|
|
|
if (tempPurchasePlanChild != null){ |
|
|
|
|
|
|
|
purchasePlanChild.setPurchasePlanChildId(tempPurchasePlanChild.getPurchasePlanChildId()); |
|
|
|
//设置计划采购数
|
|
|
|
purchasePlanChildMapper.updatePurchasePlanChild(purchasePlanChild); |
|
|
|
String salesOrderMaterialNo = filterUniqueBom.getSalesOrderMaterialNo(); |
|
|
|
String tempMakeNo = filterUniqueBom.getMakeNo(); |
|
|
|
|
|
|
|
int orderNum = 0; |
|
|
|
if (StringUtils.isNotEmpty(salesOrderMaterialNo)){ |
|
|
|
SysMakeorderBom tempMakeorderBom = new SysMakeorderBom(); |
|
|
|
tempMakeorderBom.setMaterialNo(salesOrderMaterialNo); |
|
|
|
tempMakeorderBom.setMakeNo(tempMakeNo); |
|
|
|
SysMakeorderBom oldMakeorderBom = sysMakeorderBomMapper.selectSysMakeorderBomByNo(tempMakeorderBom); |
|
|
|
orderNum = oldMakeorderBom.getOrderNum(); |
|
|
|
|
|
|
|
purchasePlanChild.setPlanPurchaseNum(orderNum * filterUniqueBom.getUseNum()); |
|
|
|
purchasePlanChild.setPurchaseNum(orderNum * filterUniqueBom.getUseNum()); |
|
|
|
}else { |
|
|
|
purchasePlanChild.setMaterialName(filterSysMakeorderBom.getMaterialName()); |
|
|
|
purchasePlanChild.setMaterialType(filterSysMakeorderBom.getMaterialType()); |
|
|
|
purchasePlanChild.setWarehouseDept(filterSysMakeorderBom.getWarehouseDept()); |
|
|
|
purchasePlanChild.setMaterialProcessMethod("0"); |
|
|
|
purchasePlanChild.setMaterialBrand(filterSysMakeorderBom.getBrand()); |
|
|
|
purchasePlanChild.setMaterialDescribe(filterSysMakeorderBom.getDescribe()); |
|
|
|
purchasePlanChild.setMaterialUnit(filterSysMakeorderBom.getUnit()); |
|
|
|
purchasePlanChild.setWarehouseDept(filterSysMakeorderBom.getWarehouseDept()); |
|
|
|
// 物料的计划采购数,使用计算出的purchasePlanNum
|
|
|
|
purchasePlanChild.setCreateBy(ShiroUtils.getLoginName()); |
|
|
|
purchasePlanChild.setPlanPurchaseNum(filterUniqueBom.getOrderNum()); |
|
|
|
purchasePlanChild.setPurchaseNum(filterUniqueBom.getOrderNum()); |
|
|
|
} |
|
|
|
purchasePlanChild.setPurchasePlanStatus("0"); |
|
|
|
purchasePlanChild.setPurchasePlanCode(purchasePlanCode); |
|
|
|
purchasePlanChild.setMaterialNo(filterUniqueBom.getMaterialNo()); |
|
|
|
purchasePlanChild.setMaterialName(filterUniqueBom.getMaterialName()); |
|
|
|
purchasePlanChild.setMaterialUnit(filterUniqueBom.getUnit()); |
|
|
|
purchasePlanChild.setMaterialDescribe(filterUniqueBom.getDescribe()); |
|
|
|
purchasePlanChild.setMaterialProcessMethod(filterUniqueBom.getProcessMethod()); |
|
|
|
purchasePlanChild.setMaterialBrand(filterUniqueBom.getBrand()); |
|
|
|
purchasePlanChild.setMaterialType(filterUniqueBom.getMaterialType()); |
|
|
|
purchasePlanChild.setMaterialPhotourl(filterUniqueBom.getPhotoUrl()); |
|
|
|
purchasePlanChild.setMaterialModel(filterUniqueBom.getMaterialModel()); |
|
|
|
purchasePlanChild.setWarehouseDept(filterUniqueBom.getWarehouseDept()); |
|
|
|
purchasePlanChild.setCreateTime(new Date()); |
|
|
|
// 直接保存PurchasePlanChild对象
|
|
|
|
purchasePlanChildMapper.insertPurchasePlanChild(purchasePlanChild); |
|
|
|
purchasePlanChild.setCreateBy(loginName); |
|
|
|
purchasePlanChildList.add(purchasePlanChild); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新总和
|
|
|
|
purchasePlanMaterialSum += purchasePlanNum; |
|
|
|
} |
|
|
|
int batchInsertPurchasePlanChildResult = purchasePlanChildMapper.batchInsertPurchasePlanChild(purchasePlanChildList); |
|
|
|
if (batchInsertPurchasePlanChildResult <= 0){ |
|
|
|
throw new BusinessException("插入采购计划子表数据失败"); |
|
|
|
} |
|
|
|
|
|
|
|
//计算计划采购总数
|
|
|
|
int planPurchaseSum = purchasePlanChildList.stream().mapToInt(PurchasePlanChild::getPlanPurchaseNum).sum(); |
|
|
|
|
|
|
|
// 创建采购计划主记录
|
|
|
|
PurchasePlan purchasePlan = buildPurchasePlan(sysMakeOrder, purchasePlanCode,materialAmount,purchasePlanMaterialSum); |
|
|
|
purchasePlan.setPurchaseSum(planPurchaseSum); |
|
|
|
purchasePlan.setPlanPurchaseSum(planPurchaseSum); |
|
|
|
|
|
|
|
purchasePlanMapper.insertPurchasePlan(purchasePlan); |
|
|
|
purchasePlan.setMaterialSum(filterUniqueBoms.size()); |
|
|
|
purchasePlan.setPurchasePlanType("1");//生产单
|
|
|
|
purchasePlan.setPurchasePlanCode(purchasePlanCode); |
|
|
|
purchasePlan.setPurchasePlanStatus("0"); |
|
|
|
purchasePlan.setCorrelationCode(makeOrder.getMakeNo()); |
|
|
|
purchasePlan.setCreateTime(new Date()); |
|
|
|
purchasePlan.setCreateBy(loginName); |
|
|
|
|
|
|
|
int insertPurchasePlanResult = purchasePlanMapper.insertPurchasePlan(purchasePlan); |
|
|
|
if (insertPurchasePlanResult <= 0){ |
|
|
|
throw new BusinessException("插入采购计划数据失败"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|