ContractUpdateFlow.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.fjhx.sale.flow;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  5. import com.fjhx.common.enums.FlowStatusEnum;
  6. import com.fjhx.flow.core.FlowDelegate;
  7. import com.fjhx.sale.entity.contract.dto.ContractDto;
  8. import com.fjhx.sale.entity.contract.po.Contract;
  9. import com.fjhx.sale.entity.contract.po.ContractProduct;
  10. import com.fjhx.sale.service.contract.ContractProductService;
  11. import com.fjhx.sale.service.contract.ContractService;
  12. import com.ruoyi.common.core.domain.BaseIdPo;
  13. import com.ruoyi.common.exception.ServiceException;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Component;
  16. import java.math.BigDecimal;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.function.Function;
  20. import java.util.stream.Collectors;
  21. /**
  22. * 合同变更流程
  23. */
  24. @Component
  25. public class ContractUpdateFlow extends FlowDelegate {
  26. @Autowired
  27. private ContractFlow contractFlow;
  28. @Autowired
  29. private ContractService contractService;
  30. @Autowired
  31. private ContractProductService contractProductService;
  32. @Override
  33. public String getFlowKey() {
  34. return "contract_update_flow";
  35. }
  36. /**
  37. * 发起流程
  38. *
  39. * @param flowId 流程ID
  40. * @param submitData 采购付款数据
  41. * @return
  42. */
  43. @Override
  44. public Long start(Long flowId, JSONObject submitData) {
  45. ContractDto contract = submitData.toJavaObject(ContractDto.class);
  46. // 原合同id不能为空
  47. Long oldContractId = contract.getOldContractId();
  48. if (oldContractId == null) {
  49. throw new ServiceException("原合同id不能为空");
  50. }
  51. List<ContractProduct> list = contractProductService.list(q -> q.eq(ContractProduct::getContractId, oldContractId));
  52. // 赋值待处理数量
  53. if (CollectionUtils.isNotEmpty(list)) {
  54. List<ContractProduct> contractProductList = contract.getContractProductList();
  55. if (ObjectUtil.isEmpty(contractProductList)) {
  56. throw new ServiceException("没有合同产品");
  57. }
  58. Map<Long, ContractProduct> contractProductMap = contractProductList
  59. .stream()
  60. .peek(item -> {
  61. if (item.getId() == null) {
  62. throw new ServiceException("合同产品id不能为空");
  63. }
  64. })
  65. .collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
  66. for (ContractProduct item : list) {
  67. ContractProduct contractProduct = contractProductMap.get(item.getId());
  68. if (contractProduct == null) {
  69. throw new ServiceException("产品id为" + item.getId() + "未上传");
  70. }
  71. BigDecimal expendQuantity = item.getExpendQuantity().subtract(item.getQuantity().subtract(contractProduct.getQuantity()));
  72. contractProduct.setExpendQuantity(expendQuantity);
  73. }
  74. }
  75. return contractFlow.start(contract);
  76. }
  77. /**
  78. * 结束流程
  79. *
  80. * @param flowId 流程ID
  81. * @param businessId 业务ID
  82. * @param submitData 数据
  83. */
  84. @Override
  85. public void end(Long flowId, Long businessId, JSONObject submitData) {
  86. contractFlow.end(flowId, businessId, submitData);
  87. // 通过业务id查询合同数据
  88. Contract contract = contractService.getById(businessId);
  89. // 原合同改为作废状态
  90. Long oldContractId = contract.getOldContractId();
  91. Contract oldContract = contractService.getById(oldContractId);
  92. if (oldContract == null) {
  93. throw new ServiceException("原合同不存在");
  94. }
  95. oldContract.setStatus(FlowStatusEnum.UPDATE.getKey());
  96. oldContract.setIsChange("1");
  97. contractService.updateById(oldContract);
  98. }
  99. }