|
|
@ -38,6 +38,7 @@ import com.ruoyi.purchase.mapper.PurchasePlanMapper; |
|
|
|
import com.ruoyi.purchase.service.IPurchaseOrderService; |
|
|
|
import com.ruoyi.system.domain.SysAttachFile; |
|
|
|
import com.ruoyi.system.domain.SysCompanyInformation; |
|
|
|
import com.ruoyi.system.domain.SysSalesOrderChild; |
|
|
|
import com.ruoyi.system.domain.SysSupplier; |
|
|
|
import com.ruoyi.system.mapper.SysCompanyInformationMapper; |
|
|
|
import com.ruoyi.system.mapper.SysUserMapper; |
|
|
@ -69,6 +70,7 @@ import java.net.URLEncoder; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
import java.util.function.Function; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
@ -92,6 +94,9 @@ public class PurchaseOrderServiceImpl implements IPurchaseOrderService |
|
|
|
@Autowired |
|
|
|
private PurchasePlanChildMapper purchasePlanChildMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private PurchasePlanMapper purchasePlanMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisCache redisCache; |
|
|
|
|
|
|
@ -249,6 +254,13 @@ public class PurchaseOrderServiceImpl implements IPurchaseOrderService |
|
|
|
List<PurchasePlanSelectSupplierVo> purchasePlanSelectSupplierVoList = addPurchaseOrder.getPurchasePlanSelectSupplierVoList(); |
|
|
|
List<PurchasePlanSelectMaterialVo> purchasePlanSelectMaterialVoList = addPurchaseOrder.getPurchasePlanSelectMaterialVoList(); |
|
|
|
|
|
|
|
//先不判断处理本次占用库存数
|
|
|
|
//判断本次采购数
|
|
|
|
boolean anyNullThisPurchaseNum = purchasePlanSelectMaterialVoList.stream().anyMatch(item -> item.getThisPurchaseNum() == null); |
|
|
|
if (anyNullThisPurchaseNum) { |
|
|
|
throw new BusinessException("请填写本次采购数量"); |
|
|
|
} |
|
|
|
|
|
|
|
PurchaseOrder purchaseOrder = new PurchaseOrder(); |
|
|
|
purchaseOrder.setPurchaseOrderCode(redisCache.generateBillNo("CG")); |
|
|
|
purchaseOrder.setPurchasePlanCode(purchasePlanOne.getPurchasePlanCodes()); |
|
|
@ -270,6 +282,11 @@ public class PurchaseOrderServiceImpl implements IPurchaseOrderService |
|
|
|
purchaseOrder.setCreateTime(new Date()); |
|
|
|
purchaseOrder.setCreateBy(loginName); |
|
|
|
|
|
|
|
//处理采购计划子表
|
|
|
|
List<PurchasePlanChild> purchasePlanChildList = updatePurchasePlanChildByAddPurchaseOrder(addPurchaseOrder); |
|
|
|
|
|
|
|
//处理采购计划主表
|
|
|
|
updatePurchasePlanByAddPurchaseOrder(addPurchaseOrder,purchasePlanChildList); |
|
|
|
|
|
|
|
buildPurchaseChild(purchasePlanSelectMaterialVoList, purchaseOrder); |
|
|
|
int result = purchaseOrderMapper.insertPurchaseOrder(purchaseOrder); |
|
|
@ -279,6 +296,181 @@ public class PurchaseOrderServiceImpl implements IPurchaseOrderService |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
//处理采购计划子表
|
|
|
|
private List<PurchasePlanChild> updatePurchasePlanChildByAddPurchaseOrder(AddPurchaseOrder addPurchaseOrder) { |
|
|
|
|
|
|
|
|
|
|
|
List<PurchasePlanChild> purchasePlanChildren = new ArrayList<>(); |
|
|
|
|
|
|
|
//暂时不处理共享库存占用总数和作废数
|
|
|
|
List<PurchasePlanSelectMaterialVo> purchasePlanSelectMaterialVoList = addPurchaseOrder.getPurchasePlanSelectMaterialVoList(); |
|
|
|
|
|
|
|
//收集前端传入的所有的物料号
|
|
|
|
List<String> collectMaterialNo = purchasePlanSelectMaterialVoList.stream().map(PurchasePlanSelectMaterialVo::getMaterialNo).collect(Collectors.toList()); |
|
|
|
|
|
|
|
PurchasePlanOne purchasePlanOne = addPurchaseOrder.getPurchasePlanOne(); |
|
|
|
String purchasePlanCodes = purchasePlanOne.getPurchasePlanCodes(); |
|
|
|
//得到本次采购的所有采购计划单号
|
|
|
|
List<String> purchasePlanCodeList = new ArrayList<>(); |
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(purchasePlanCodes)){ |
|
|
|
String[] splitSalesOrderCode = purchasePlanCodes.split(","); |
|
|
|
purchasePlanCodeList = Arrays.asList(splitSalesOrderCode); |
|
|
|
} |
|
|
|
|
|
|
|
for (String purchasePlanCode : purchasePlanCodeList) { |
|
|
|
PurchasePlanChild purchasePlanChild = new PurchasePlanChild(); |
|
|
|
purchasePlanChild.setPurchasePlanCode(purchasePlanCode); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<PurchasePlanChild> allPurchasePlanChildList = purchasePlanChildMapper.selectPurchasePlanChildPlanCodeList(purchasePlanCodeList); |
|
|
|
|
|
|
|
// 过滤 allPurchasePlanChildList,只保留和前端 collectMaterialNo 相等的数据
|
|
|
|
List<PurchasePlanChild> oldPurchasePlanChildList = allPurchasePlanChildList.stream() |
|
|
|
.filter(child -> collectMaterialNo.contains(child.getMaterialNo())) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
// List<Long> collectPurchasePlanChildId = purchasePlanSelectMaterialVoList.stream().map(PurchasePlanSelectMaterialVo::getPurchasePlanChildId).collect(Collectors.toList());
|
|
|
|
|
|
|
|
//List<PurchasePlanChild> oldPurchasePlanChildList = purchasePlanChildMapper.selectBatchPurchasePlanChildByIdList(collectPurchasePlanChildId);
|
|
|
|
|
|
|
|
// 将旧数据按物料编号分组
|
|
|
|
Map<String, List<PurchasePlanChild>> oldDataGroupedByMaterialNo = oldPurchasePlanChildList.stream() |
|
|
|
.collect(Collectors.groupingBy(PurchasePlanChild ::getMaterialNo)); |
|
|
|
// 当前时间
|
|
|
|
Date now = new Date(); |
|
|
|
|
|
|
|
for (PurchasePlanSelectMaterialVo purchasePlanSelectMaterialVo : purchasePlanSelectMaterialVoList) { |
|
|
|
String materialNo = purchasePlanSelectMaterialVo.getMaterialNo(); |
|
|
|
// Integer thisPurchaseNum = purchasePlanSelectMaterialVo.getThisPurchaseNum();
|
|
|
|
|
|
|
|
// 获取旧数据中相同物料编号的数据
|
|
|
|
List<PurchasePlanChild> oldChildren = oldDataGroupedByMaterialNo.get(materialNo); |
|
|
|
|
|
|
|
if (oldChildren != null && !oldChildren.isEmpty()) { |
|
|
|
// 按照 订单创建时间 远离当前时间排序
|
|
|
|
oldChildren.sort(Comparator.comparing(oldChild -> -Math.abs(now.getTime() - oldChild.getCreateTime().getTime()))); |
|
|
|
|
|
|
|
for (PurchasePlanChild oldChild : oldChildren) { |
|
|
|
|
|
|
|
Integer thisPurchaseNum = purchasePlanSelectMaterialVo.getThisPurchaseNum(); |
|
|
|
//本次采购数也是实际采购数
|
|
|
|
oldChild.setActualPurchaseNum(thisPurchaseNum); |
|
|
|
//待采购数
|
|
|
|
Integer purchaseNum = oldChild.getPurchaseNum(); |
|
|
|
if (purchaseNum >= thisPurchaseNum){ |
|
|
|
purchaseNum = purchaseNum - thisPurchaseNum; |
|
|
|
oldChild.setPurchaseNum(purchaseNum); |
|
|
|
}else { |
|
|
|
throw new BusinessException("本次采购数量不能大于待采购数量"); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 部分申请:0<(实际采购数+共享库存占用数+作废数)<计划采购数;可添加采购订单 |
|
|
|
* 全部申请:0<(实际采购数+共享库存占用数+作废数)=计划采购数,作废数<计划采购数;不可添加采购订单 |
|
|
|
* */ |
|
|
|
//计划采购数
|
|
|
|
Integer planPurchaseNum = oldChild.getPlanPurchaseNum(); |
|
|
|
if (thisPurchaseNum < planPurchaseNum){ |
|
|
|
oldChild.setPurchasePlanStatus("1");//部分申请
|
|
|
|
} |
|
|
|
if (thisPurchaseNum.equals(planPurchaseNum)){ |
|
|
|
oldChild.setPurchasePlanStatus("2");//全部申请
|
|
|
|
} |
|
|
|
oldChild.setUpdateTime(new Date()); |
|
|
|
oldChild.setUpdateBy(ShiroUtils.getLoginName()); |
|
|
|
oldChild.setThisPurchaseNum(thisPurchaseNum); |
|
|
|
//收集更新后的子表数据
|
|
|
|
purchasePlanChildren.add(oldChild); |
|
|
|
int result = purchasePlanChildMapper.updatePurchasePlanChild(oldChild); |
|
|
|
if (result <= 0){ |
|
|
|
throw new BusinessException("更新采购计划子表数据失败"); |
|
|
|
} |
|
|
|
// 更新完成后,跳出循环
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return purchasePlanChildren; |
|
|
|
} |
|
|
|
|
|
|
|
//处理采购计划主表
|
|
|
|
private void updatePurchasePlanByAddPurchaseOrder(AddPurchaseOrder addPurchaseOrder,List<PurchasePlanChild> purchasePlanChildList) { |
|
|
|
|
|
|
|
//本次出货的所有采购计划子表的数据,把purchasePlanChildList按照物料采购计划单号进行分组
|
|
|
|
Map<String, List<PurchasePlanChild>> groupedByPurchasePlanCode = purchasePlanChildList.stream() |
|
|
|
.collect(Collectors.groupingBy(PurchasePlanChild::getPurchasePlanCode)); |
|
|
|
|
|
|
|
|
|
|
|
PurchasePlanOne purchasePlanOne = addPurchaseOrder.getPurchasePlanOne(); |
|
|
|
String purchasePlanCodes = purchasePlanOne.getPurchasePlanCodes(); |
|
|
|
//得到本次采购的所有采购计划单号
|
|
|
|
List<String> purchasePlanCodeList = new ArrayList<>(); |
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(purchasePlanCodes)){ |
|
|
|
String[] splitSalesOrderCode = purchasePlanCodes.split(","); |
|
|
|
purchasePlanCodeList = Arrays.asList(splitSalesOrderCode); |
|
|
|
} |
|
|
|
|
|
|
|
//本次出货的所有采购计划单数据库中的数据
|
|
|
|
List<PurchasePlan> purchasePlans = purchasePlanMapper.selectPurchaseOrderByPlanCodeList(purchasePlanCodeList); |
|
|
|
|
|
|
|
// 创建一个 map 用于快速查找主表数据
|
|
|
|
Map<String, PurchasePlan> purchasePlanMap = purchasePlans.stream() |
|
|
|
.collect(Collectors.toMap(PurchasePlan::getPurchasePlanCode, Function.identity())); |
|
|
|
|
|
|
|
|
|
|
|
// 更新主表数据
|
|
|
|
for (Map.Entry<String, List<PurchasePlanChild>> entry : groupedByPurchasePlanCode.entrySet()) { |
|
|
|
String purchasePlanCode = entry.getKey(); |
|
|
|
List<PurchasePlanChild> children = entry.getValue(); |
|
|
|
|
|
|
|
// 获取主表数据
|
|
|
|
PurchasePlan purchasePlan = purchasePlanMap.get(purchasePlanCode); |
|
|
|
if (purchasePlan != null) { |
|
|
|
// 初始化 actualPurchaseSum
|
|
|
|
if (purchasePlan.getActualPurchaseSum() == null) { |
|
|
|
purchasePlan.setActualPurchaseSum(0); |
|
|
|
} |
|
|
|
|
|
|
|
// 累加 actualPurchaseSum
|
|
|
|
int thisPurchaseSum = children.stream() |
|
|
|
.mapToInt(PurchasePlanChild::getThisPurchaseNum) |
|
|
|
.sum(); |
|
|
|
purchasePlan.setActualPurchaseSum(purchasePlan.getActualPurchaseSum() + thisPurchaseSum); |
|
|
|
|
|
|
|
// 更新 purchaseSum
|
|
|
|
int currentPurchaseSum = purchasePlan.getPurchaseSum(); |
|
|
|
purchasePlan.setPurchaseSum(currentPurchaseSum - thisPurchaseSum); |
|
|
|
|
|
|
|
// 更新采购计划状态
|
|
|
|
if (purchasePlan.getActualPurchaseSum() < purchasePlan.getPlanPurchaseSum()) { |
|
|
|
purchasePlan.setPurchasePlanStatus("1"); |
|
|
|
} else if (purchasePlan.getActualPurchaseSum().equals(purchasePlan.getPlanPurchaseSum())) { |
|
|
|
purchasePlan.setPurchasePlanStatus("2"); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新其他字段
|
|
|
|
purchasePlan.setUpdateTime(new Date()); |
|
|
|
purchasePlan.setUpdateBy(ShiroUtils.getLoginName()); |
|
|
|
|
|
|
|
// 更新主表数据到数据库
|
|
|
|
int result = purchasePlanMapper.updatePurchasePlan(purchasePlan); |
|
|
|
if (result <= 0) { |
|
|
|
throw new BusinessException("更新采购计划主表数据失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//新增采购订单子表
|
|
|
|
private void buildPurchaseChild(List<PurchasePlanSelectMaterialVo> purchasePlanSelectMaterialVoList, PurchaseOrder purchaseOrder) { |
|
|
|
List<PurchaseOrderChild> purchaseOrderChildren = new ArrayList<>(); |
|
|
|