PurchaseFlow.java 9.1 KB

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