EhsdPurchaseFlow.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package com.fjhx.sale.flow;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  5. import com.baomidou.mybatisplus.core.toolkit.IdWorker;
  6. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.fjhx.area.utils.CustomizeAreaUtil;
  9. import com.fjhx.common.entity.InOutBo;
  10. import com.fjhx.common.enums.CodingRuleEnum;
  11. import com.fjhx.common.enums.FlowStatusEnum1;
  12. import com.fjhx.common.service.coding.CodingRuleService;
  13. import com.fjhx.common.utils.Assert;
  14. import com.fjhx.flow.core.FlowDelegate;
  15. import com.fjhx.flow.enums.FlowStatusEnum;
  16. import com.fjhx.item.enums.ProductAvailableRecordType;
  17. import com.fjhx.item.service.product.ProductInfoService;
  18. import com.fjhx.purchase.entity.purchase.enums.PurchaseDataResourceEnum;
  19. import com.fjhx.purchase.entity.purchase.enums.PurchaseStatusEnum;
  20. import com.fjhx.sale.entity.contract.po.Contract;
  21. import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseDto;
  22. import com.fjhx.sale.entity.purchase.po.EhsdPurchase;
  23. import com.fjhx.sale.entity.purchase.po.EhsdPurchaseProduct;
  24. import com.fjhx.sale.entity.purchase.po.EhsdPurchaseProject;
  25. import com.fjhx.sale.entity.sample.po.Sample;
  26. import com.fjhx.sale.service.contract.ContractService;
  27. import com.fjhx.sale.service.purchase.EhsdPurchaseProductService;
  28. import com.fjhx.sale.service.purchase.EhsdPurchaseProjectService;
  29. import com.fjhx.sale.service.purchase.EhsdPurchaseService;
  30. import com.fjhx.sale.service.sample.SampleProductService;
  31. import com.fjhx.sale.service.sample.SampleService;
  32. import com.fjhx.wms.entity.stock.emums.JournalType;
  33. import com.fjhx.wms.entity.stock.po.StockWait;
  34. import com.fjhx.wms.entity.stock.po.StockWaitDetails;
  35. import com.fjhx.wms.service.stock.StockWaitDetailsService;
  36. import com.fjhx.wms.service.stock.StockWaitService;
  37. import com.ruoyi.common.annotation.LogicIgnore;
  38. import com.ruoyi.common.core.domain.BasePo;
  39. import com.ruoyi.common.exception.ServiceException;
  40. import com.ruoyi.common.utils.SecurityUtils;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.stereotype.Component;
  43. import java.util.ArrayList;
  44. import java.util.Date;
  45. import java.util.List;
  46. import java.util.Objects;
  47. /**
  48. * EHSD采购流程
  49. *
  50. * @Author:caozj
  51. * @DATE:2023/4/3 17:38
  52. */
  53. @Component
  54. public class EhsdPurchaseFlow extends FlowDelegate {
  55. @Autowired
  56. private EhsdPurchaseService purchaseService;
  57. @Autowired
  58. private EhsdPurchaseProductService purchaseProductService;
  59. @Autowired
  60. private EhsdPurchaseProjectService purchaseProjectService;
  61. @Autowired
  62. private SampleProductService sampleProductService;
  63. @Autowired
  64. private CodingRuleService codingRuleService;
  65. @Autowired
  66. private ContractService contractService;
  67. @Autowired
  68. private SampleService sampleService;
  69. @Autowired
  70. private ProductInfoService productInfoService;
  71. @Autowired
  72. private StockWaitService stockWaitService;
  73. @Autowired
  74. private StockWaitDetailsService stockWaitDetailsService;
  75. @Override
  76. public String getFlowKey() {
  77. return "purchase_flow";
  78. }
  79. /**
  80. * 发起流程
  81. *
  82. * @param flowId 流程ID
  83. * @param submitData 采购数据
  84. * @return
  85. */
  86. @Override
  87. public Long start(Long flowId, JSONObject submitData) {
  88. EhsdPurchaseDto purchase = submitData.toJavaObject(EhsdPurchaseDto.class);
  89. if (ObjectUtil.isEmpty(purchase.getCurrency())) {
  90. purchase.setCurrency("CNY");
  91. }
  92. purchase.setId(null);//清空id防止前端误传
  93. purchase.setFlowId(flowId);//赋值流程id
  94. purchase.setProcessInstanceId(getFlowKey());//赋值流程key
  95. //赋值产品归属公司
  96. Long companyId = purchase.getCompanyId();
  97. if (ObjectUtil.isEmpty(companyId)) {
  98. purchase.setCompanyId(SecurityUtils.getCompanyId());
  99. }
  100. Integer dataResource = purchase.getDataResource();
  101. if (ObjectUtils.isEmpty(dataResource)) {
  102. throw new ServiceException("数据来源类型不能为空!");
  103. }
  104. //采购单编号生成
  105. if (0 == dataResource) {
  106. //手动创建 编号规则
  107. purchase.setCode(codingRuleService.createCode(CodingRuleEnum.EHSD_PURCHASE.getKey(), null));
  108. } else if (1 == dataResource) {
  109. //合同 编号规则
  110. Contract contract = contractService.getById(purchase.getDataResourceId());
  111. Assert.notEmpty(contract, "查询不到合同信息,无法生成编号");
  112. long count = purchaseService.count(q -> q.eq(EhsdPurchase::getDataResourceId, contract.getId()));
  113. purchase.setCode(contract.getCode() + "-" + (count + 1));
  114. } else if (2 == dataResource) {
  115. //样品单 编号规则
  116. Sample sample = sampleService.getById(purchase.getDataResourceId());
  117. Assert.notEmpty(sample, "查询不到样品单信息,无法生成编号");
  118. long count = purchaseService.count(q -> q.eq(EhsdPurchase::getDataResourceId, sample.getId()));
  119. purchase.setCode(sample.getCode() + "-" + (count + 1));
  120. } else {
  121. throw new ServiceException("未知数据来源类型");
  122. }
  123. //公共代码
  124. purchase = connStart(purchase);
  125. //回传单号给流程引擎
  126. submitData.put("code", purchase.getCode());
  127. submitData.put("currency", purchase.getCurrency());
  128. return purchase.getId();
  129. }
  130. /**
  131. * 公共代码块--发起
  132. */
  133. public EhsdPurchaseDto connStart(EhsdPurchaseDto purchase) {
  134. //赋值城市省份信息
  135. CustomizeAreaUtil.setAreaId(purchase);
  136. purchase.setSellCityId(purchase.getCityId());
  137. purchase.setSellCountryId(purchase.getCountryId());
  138. purchase.setSellProvinceId(purchase.getProvinceId());
  139. purchase.setStatus(PurchaseStatusEnum.UNDER_REVIEW.getKey());
  140. String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
  141. purchase.setUserName(nickName);
  142. purchaseService.saveOrUpdate(purchase);
  143. List<EhsdPurchaseProduct> purchaseProductList = purchase.getPurchaseProductList();
  144. List<EhsdPurchaseProject> purchaseProjectList = purchase.getPurchaseProjectList();
  145. //防止空指针
  146. purchaseProjectList = (purchaseProjectList == null ? new ArrayList<>() : purchaseProjectList);
  147. purchaseProductList = (purchaseProductList == null ? new ArrayList<>() : purchaseProductList);
  148. //产品(采购明细)
  149. for (EhsdPurchaseProduct s : purchaseProductList) {
  150. s.setId(ObjectUtils.isNotEmpty(s.getId()) ? s.getId() : IdWorker.getId());
  151. s.setPurchaseId(purchase.getId());
  152. }
  153. //修改或删除数据
  154. purchaseProductService.editLinked(purchaseProductList, EhsdPurchaseProduct::getPurchaseId, purchase.getId());
  155. //收费项目
  156. for (EhsdPurchaseProject s : purchaseProjectList) {
  157. s.setPurchaseId(purchase.getId());
  158. }
  159. purchaseProjectService.editLinked(purchaseProjectList, EhsdPurchaseProject::getPurchaseId, purchase.getId());
  160. return purchase;
  161. }
  162. /**
  163. * 结束流程
  164. *
  165. * @param flowId 流程ID
  166. * @param businessId 业务ID
  167. * @param submitData 数据
  168. */
  169. @Override
  170. public void end(Long flowId, Long businessId, JSONObject submitData) {
  171. //通过业务ID查询采购数据
  172. EhsdPurchase purchase = purchaseService.getById(businessId);
  173. if (ObjectUtils.isEmpty(purchase)) {
  174. throw new ServiceException("采购单不存在,或已被删除");
  175. }
  176. //查询采购产品
  177. List<EhsdPurchaseProduct> purchaseProductList = purchaseProductService.list(Wrappers.<EhsdPurchaseProduct>query().lambda().eq(EhsdPurchaseProduct::getPurchaseId, businessId));
  178. //修改采购状态为审批通过
  179. purchase.setStatus(PurchaseStatusEnum.PASS.getKey());
  180. purchase.setApprovedDate(new Date());
  181. purchaseService.updateById(purchase);
  182. //修改申购明细状态
  183. purchaseService.updateSubscribeStatus(businessId);
  184. List<InOutBo> inOutBoList = new ArrayList<>();
  185. for (EhsdPurchaseProduct purchaseProduct : purchaseProductList) {
  186. //如果数据来源是申购 操作可用库存(可用库存 = 当前可用库存 + 采购明细数量)
  187. if (Objects.equals(purchaseProduct.getDataResource(), PurchaseDataResourceEnum.DATA_RESOURCE_0)) {
  188. InOutBo inOutBo = new InOutBo();
  189. inOutBo.setProductId(purchaseProduct.getProductId());
  190. inOutBo.setQuantity(purchaseProduct.getQuantity());
  191. inOutBoList.add(inOutBo);
  192. }
  193. }
  194. productInfoService.editAvailableQuantity(inOutBoList, businessId, ProductAvailableRecordType.PURCHASE_PASS, purchase.getCompanyId());
  195. //采购审批通过 生成待入库数据
  196. StockWait stockWait = new StockWait();
  197. stockWait.setType(1);
  198. stockWait.setBusinessType(JournalType.PURCHASE_IN.getDetailType());
  199. stockWait.setBusinessId(purchase.getId());
  200. stockWait.setBusinessCode(purchase.getCode());
  201. stockWait.setStatus(0);
  202. stockWait.setPurchaseId(purchase.getId());
  203. stockWaitService.save(stockWait);
  204. List<StockWaitDetails> stockWaitDetailsList = new ArrayList<>();
  205. for (EhsdPurchaseProduct purchaseProduct : purchaseProductList) {
  206. StockWaitDetails stockWaitDetails = new StockWaitDetails();
  207. stockWaitDetails.setStockWaitId(stockWait.getId());
  208. stockWaitDetails.setBusinessDetailsId(purchaseProduct.getId());
  209. stockWaitDetails.setPurchaseDetailId(purchaseProduct.getId());
  210. stockWaitDetails.setProductId(purchaseProduct.getProductId());
  211. stockWaitDetails.setQuantity(purchaseProduct.getQuantity());
  212. stockWaitDetails.setStatus(0);
  213. stockWaitDetailsList.add(stockWaitDetails);
  214. }
  215. stockWaitDetailsService.saveBatch(stockWaitDetailsList);
  216. }
  217. /**
  218. * 重新发起
  219. */
  220. @Override
  221. @LogicIgnore
  222. @DSTransactional
  223. public void relaunch(Long flowId, Long businessId, FlowStatusEnum flowStatus, JSONObject submitData) {
  224. //删除采购合同
  225. EhsdPurchaseDto purchase = submitData.toJavaObject(EhsdPurchaseDto.class);
  226. if (ObjectUtils.isEmpty(purchase)) {
  227. throw new ServiceException("采购数据不能为空");
  228. }
  229. purchase.setFlowId(flowId);
  230. connStart(purchase);
  231. }
  232. /**
  233. * 驳回
  234. */
  235. @Override
  236. public void reject(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
  237. purchaseService.update(q -> q
  238. .eq(EhsdPurchase::getId, businessId)
  239. .set(EhsdPurchase::getStatus, FlowStatusEnum1.REJECT.getKey())
  240. .set(BasePo::getUpdateTime, new Date())
  241. .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
  242. );
  243. }
  244. /**
  245. * 作废
  246. */
  247. @Override
  248. public void cancellation(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
  249. super.cancellation(flowId, businessId, flowStatus);
  250. purchaseService.update(q -> q
  251. .eq(EhsdPurchase::getId, businessId)
  252. .set(EhsdPurchase::getStatus, FlowStatusEnum1.CANCELLATION.getKey())
  253. .set(BasePo::getUpdateTime, new Date())
  254. .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
  255. );
  256. }
  257. }