EhsdPurchaseFlow.java 15 KB

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