PurchaseFlow.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.fjhx.sale.flow;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  4. import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
  5. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  6. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.fjhx.common.constant.SourceConstant;
  9. import com.fjhx.common.enums.CodingRuleEnum;
  10. import com.fjhx.common.service.coding.CodingRuleService;
  11. import com.fjhx.flow.core.FlowDelegate;
  12. import com.fjhx.purchase.entity.purchase.enums.PurchaseDataResourceEnum;
  13. import com.fjhx.purchase.entity.purchase.enums.PurchaseDetailStatusEnum;
  14. import com.fjhx.purchase.entity.purchase.enums.PurchaseStatusEnum;
  15. import com.fjhx.purchase.entity.purchase.po.Purchase;
  16. import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
  17. import com.fjhx.purchase.entity.purchase.po.PurchaseOtherFee;
  18. import com.fjhx.purchase.entity.purchase.vo.PurchaseVo;
  19. import com.fjhx.purchase.service.purchase.PurchaseDetailService;
  20. import com.fjhx.purchase.service.purchase.PurchaseOtherFeeService;
  21. import com.fjhx.purchase.service.purchase.PurchaseService;
  22. import com.fjhx.sale.entity.contract.po.ContractProduct;
  23. import com.fjhx.sale.entity.sample.po.SampleProduct;
  24. import com.fjhx.sale.service.contract.ContractProductService;
  25. import com.fjhx.sale.service.sample.SampleProductService;
  26. import com.ruoyi.common.exception.ServiceException;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Component;
  29. import java.math.BigDecimal;
  30. import java.util.ArrayList;
  31. import java.util.Date;
  32. import java.util.List;
  33. /**
  34. * EHSD采购流程
  35. *
  36. * @Author:caozj
  37. * @DATE:2023/4/3 17:38
  38. */
  39. @Component
  40. public class PurchaseFlow extends FlowDelegate {
  41. @Autowired
  42. private PurchaseService purchaseService;
  43. @Autowired
  44. private SampleProductService sampleProductService;
  45. @Autowired
  46. private PurchaseDetailService purchaseDetailService;
  47. @Autowired
  48. private ContractProductService contractProductService;
  49. @Autowired
  50. private CodingRuleService codingRuleService;
  51. @Autowired
  52. private PurchaseOtherFeeService purchaseOtherFeeService;
  53. @Override
  54. public String getFlowKey() {
  55. return "purchase_flow";
  56. }
  57. /**
  58. * 发起流程
  59. *
  60. * @param flowId 流程ID
  61. * @param submitData 采购数据
  62. * @return
  63. */
  64. @Override
  65. public Long start(Long flowId, JSONObject submitData) {
  66. DynamicDataSourceContextHolder.push(SourceConstant.PURCHASE);
  67. PurchaseVo purchase = submitData.toJavaObject(PurchaseVo.class);
  68. // purchase.setCode(CodeEnum.PURCHASE.getCode());
  69. purchase.setCode(codingRuleService.createCode(CodingRuleEnum.PURCHASE.getKey(), null));
  70. purchase.setPurchaseStatus(PurchaseStatusEnum.UNDER_REVIEW.getKey());
  71. purchase.setFlowId(flowId);
  72. purchaseService.save(purchase);
  73. List<PurchaseDetail> purchaseDetailList = purchase.getPurchaseDetailList();
  74. if (CollectionUtils.isNotEmpty(purchaseDetailList)) {
  75. for (PurchaseDetail s : purchaseDetailList) {
  76. s.setPurchaseId(purchase.getId());
  77. }
  78. purchaseDetailService.saveBatch(purchaseDetailList);
  79. }
  80. //保存其他费用信息
  81. List<PurchaseOtherFee> otherFeeList = purchase.getOtherFeeList();
  82. if (ObjectUtils.isNotEmpty(otherFeeList)) {
  83. otherFeeList.forEach(item -> item.setPurchaseId(purchase.getId()));
  84. purchaseOtherFeeService.saveBatch(otherFeeList);
  85. }
  86. DynamicDataSourceContextHolder.poll();
  87. return purchase.getId();
  88. }
  89. /**
  90. * 结束流程
  91. *
  92. * @param flowId 流程ID
  93. * @param businessId 业务ID
  94. * @param submitData 数据
  95. */
  96. @Override
  97. @DSTransactional
  98. public void end(Long flowId, Long businessId, JSONObject submitData) {
  99. //通过业务ID查询采购数据
  100. Purchase purchase = purchaseService.getById(businessId);
  101. if (ObjectUtils.isEmpty(purchase)) {
  102. throw new ServiceException("采购单不存在");
  103. }
  104. //查询采购产品
  105. List<PurchaseDetail> purchaseDetailList = purchaseDetailService.list(Wrappers.<PurchaseDetail>query().lambda().eq(PurchaseDetail::getPurchaseId, businessId));
  106. List<ContractProduct> upContractProduct = new ArrayList<>();
  107. List<SampleProduct> upSampleProduct = new ArrayList<>();
  108. for (PurchaseDetail p : purchaseDetailList) {
  109. if (ObjectUtils.isNotEmpty(p.getDataResourceId()) &&
  110. p.getDataResource() == PurchaseDataResourceEnum.DATA_RESOURCE_1.getKey()) {//如果采购的是外销合同
  111. ContractProduct contractProduct = contractProductService.getById(p.getDataResourceId());
  112. BigDecimal expendQuantity = contractProduct.getExpendQuantity().subtract(p.getCount());
  113. if (expendQuantity.compareTo(BigDecimal.ZERO) < 1) {//小于0不让继续执行
  114. throw new ServiceException("采购数量不得大于合同剩余采购数量");
  115. }
  116. contractProduct.setExpendQuantity(expendQuantity);
  117. upContractProduct.add(contractProduct);
  118. }
  119. if (ObjectUtils.isNotEmpty(p.getDataResourceId()) &&
  120. p.getDataResource() == PurchaseDataResourceEnum.DATA_RESOURCE_2.getKey()) {//如果采购的是样品单
  121. SampleProduct sampleProduct = sampleProductService.getById(p.getDataResourceId());
  122. BigDecimal expendQuantity = sampleProduct.getExpendQuantity().subtract(p.getCount());
  123. if (expendQuantity.compareTo(BigDecimal.ZERO) < 1) {//小于0不让继续执行
  124. throw new ServiceException("采购数量不得大于合同剩余采购数量");
  125. }
  126. sampleProduct.setExpendQuantity(expendQuantity);
  127. upSampleProduct.add(sampleProduct);
  128. }
  129. }
  130. if (CollectionUtils.isNotEmpty(upContractProduct)) {//扣减销售合同数量
  131. contractProductService.updateBatchById(upContractProduct);
  132. }
  133. if (CollectionUtils.isNotEmpty(upSampleProduct)) {//扣减样品单数量
  134. sampleProductService.updateBatchById(upSampleProduct);
  135. }
  136. //修改采购状态为审批通过
  137. purchase.setPurchaseStatus(PurchaseStatusEnum.PASS.getKey());
  138. purchase.setApprovedDate(new Date());
  139. purchaseService.updateById(purchase);
  140. //修改采购明细为待采购
  141. PurchaseDetail detail = new PurchaseDetail();
  142. detail.setStatus(PurchaseDetailStatusEnum.PASS.getKey());
  143. purchaseDetailService.update(detail, Wrappers.<PurchaseDetail>query()
  144. .lambda().eq(PurchaseDetail::getPurchaseId, purchase.getId()));
  145. }
  146. }