PurchaseFlow.java 7.5 KB

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