EhsdPurchaseFlow.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package com.fjhx.sale.flow;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.dynamic.datasource.annotation.DS;
  4. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  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.constant.SourceConstant;
  10. import com.fjhx.common.enums.CodingRuleEnum;
  11. import com.fjhx.common.service.coding.CodingRuleService;
  12. import com.fjhx.common.utils.Assert;
  13. import com.fjhx.file.utils.ObsFileUtil;
  14. import com.fjhx.flow.core.FlowDelegate;
  15. import com.fjhx.flow.core.FlowThreadLocalUtil;
  16. import com.fjhx.flow.enums.FlowStatusEnum;
  17. import com.fjhx.flow.enums.HandleTypeEnum;
  18. import com.fjhx.purchase.entity.purchase.enums.PurchaseDataResourceEnum;
  19. import com.fjhx.purchase.entity.purchase.enums.PurchaseStatusEnum;
  20. import com.fjhx.purchase.entity.subscribe.enums.SubscribeDetailStatusEnum;
  21. import com.fjhx.purchase.entity.subscribe.po.SubscribeDetail;
  22. import com.fjhx.purchase.service.subscribe.SubscribeDetailService;
  23. import com.fjhx.sale.entity.contract.po.Contract;
  24. import com.fjhx.sale.entity.contract.po.ContractProduct;
  25. import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseDto;
  26. import com.fjhx.sale.entity.purchase.po.*;
  27. import com.fjhx.sale.entity.sample.po.Sample;
  28. import com.fjhx.sale.entity.sample.po.SampleProduct;
  29. import com.fjhx.sale.service.contract.ContractProductService;
  30. import com.fjhx.sale.service.contract.ContractService;
  31. import com.fjhx.sale.service.purchase.*;
  32. import com.fjhx.sale.service.sample.SampleProductService;
  33. import com.fjhx.sale.service.sample.SampleService;
  34. import com.ruoyi.common.annotation.LogicIgnore;
  35. import com.ruoyi.common.annotation.TenantIgnore;
  36. import com.ruoyi.common.core.domain.BasePo;
  37. import com.ruoyi.common.exception.ServiceException;
  38. import com.ruoyi.common.utils.SecurityUtils;
  39. import lombok.experimental.Delegate;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.stereotype.Component;
  42. import org.springframework.transaction.annotation.Transactional;
  43. import java.math.BigDecimal;
  44. import java.util.ArrayList;
  45. import java.util.Date;
  46. import java.util.List;
  47. import java.util.stream.Collectors;
  48. /**
  49. * EHSD采购流程
  50. *
  51. * @Author:caozj
  52. * @DATE:2023/4/3 17:38
  53. */
  54. @Component
  55. @DS(SourceConstant.SALE)
  56. public class EhsdPurchaseFlow extends FlowDelegate {
  57. @Autowired
  58. private EhsdPurchaseService purchaseService;
  59. @Autowired
  60. private EhsdPurchaseProductService purchaseProductService;
  61. @Autowired
  62. private EhsdPurchaseProjectService purchaseProjectService;
  63. @Autowired
  64. private EhsdPurchaseArrivalService purchaseArrivalService;
  65. @Autowired
  66. private EhsdPurchaseProductMountingsService purchaseProductMountingsService;
  67. @Autowired
  68. private ContractProductService contractProductService;
  69. @Autowired
  70. private SampleProductService sampleProductService;
  71. @Autowired
  72. private CodingRuleService codingRuleService;
  73. @Autowired
  74. private SubscribeDetailService subscribeDetailService;
  75. @Autowired
  76. private ContractService contractService;
  77. @Autowired
  78. private SampleService sampleService;
  79. @Override
  80. public String getFlowKey() {
  81. return "ehsd_purchase_flow";
  82. }
  83. /**
  84. * 发起流程
  85. *
  86. * @param flowId 流程ID
  87. * @param submitData 采购数据
  88. * @return
  89. */
  90. @Override
  91. public Long start(Long flowId, JSONObject submitData) {
  92. EhsdPurchaseDto purchase = submitData.toJavaObject(EhsdPurchaseDto.class);
  93. purchase.setFlowId(flowId);
  94. //手动创建 编号规则
  95. if (0 == purchase.getDataResource()) {
  96. purchase.setCode(codingRuleService.createCode(CodingRuleEnum.EHSD_PURCHASE.getKey(), null));
  97. }
  98. //合同 编号规则
  99. if (1 == purchase.getDataResource()) {
  100. Contract contract = contractService.getById(purchase.getDataResourceId());
  101. Assert.notEmpty(contract, "查询不到合同信息,无法生成编号");
  102. long count = purchaseService.count(q -> q.eq(EhsdPurchase::getDataResourceId, contract.getId()));
  103. purchase.setCode(contract.getCode() + "-" + (count + 1));
  104. }
  105. //样品单 编号规则
  106. if (2 == purchase.getDataResource()) {
  107. Sample sample = sampleService.getById(purchase.getDataResourceId());
  108. Assert.notEmpty(sample, "查询不到样品单信息,无法生成编号");
  109. long count = purchaseService.count(q -> q.eq(EhsdPurchase::getDataResourceId, sample.getId()));
  110. purchase.setCode(sample.getCode() + "-" + (count + 1));
  111. }
  112. //公共代码
  113. purchase = connStart(purchase);
  114. return purchase.getId();
  115. }
  116. /**
  117. * 公共代码块--变更
  118. */
  119. public EhsdPurchaseDto updateStart(EhsdPurchaseDto purchase) {
  120. //赋值城市省份信息
  121. CustomizeAreaUtil.setAreaId(purchase);
  122. purchase.setSellCityId(purchase.getCityId());
  123. purchase.setSellCountryId(purchase.getCountryId());
  124. purchase.setSellProvinceId(purchase.getProvinceId());
  125. purchase.setStatus(PurchaseStatusEnum.UNDER_REVIEW.getKey());
  126. String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
  127. purchase.setUserName(nickName);
  128. purchaseService.save(purchase);
  129. List<EhsdPurchaseProduct> purchaseProductList = purchase.getPurchaseProductList();
  130. List<EhsdPurchaseProject> purchaseProjectList = purchase.getPurchaseProjectList();
  131. List<EhsdPurchaseArrival> purchaseArrivalList = purchase.getPurchaseArrivalList();
  132. List<EhsdPurchaseProductMountings> addMountingsList = new ArrayList<>();
  133. if (CollectionUtils.isNotEmpty(purchaseProductList)) {
  134. for (EhsdPurchaseProduct s : purchaseProductList) {//产品
  135. long id ;
  136. if(ObjectUtils.isNotEmpty(s.getId())){
  137. id = s.getId();
  138. }else{
  139. id = IdWorker.getId();
  140. }
  141. s.setId(id);
  142. s.setPurchaseId(purchase.getId());
  143. List<EhsdPurchaseProductMountings> purchaseProductMountingsList = s.getPurchaseProductMountingsList();
  144. if (CollectionUtils.isNotEmpty(purchaseProductMountingsList)) {//产品配件
  145. purchaseProductMountingsList.forEach(obj -> obj.setPurchaseProductId(id));
  146. addMountingsList.addAll(purchaseProductMountingsList);
  147. }
  148. ObsFileUtil.saveFile(s.getFileList(), s.getId());
  149. if ("0".equals(purchase.getDataResource())) {
  150. //计算采购数量是否大于申购数量
  151. List<EhsdPurchaseProduct> purchaseDetailList1 = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getSubscribeDetailId,
  152. s.getSubscribeDetailId()));
  153. //求和
  154. BigDecimal purchaseCount = purchaseDetailList1.stream()
  155. .map(EhsdPurchaseProduct::getQuantity)
  156. .reduce(BigDecimal.ZERO, BigDecimal::add);
  157. //计算历史采购数量+本次采购数量之和
  158. BigDecimal count = purchaseCount.add(s.getQuantity());
  159. //判断采购数量是否大于申购数量
  160. SubscribeDetail subscribeDetail = subscribeDetailService.getById(s.getSubscribeDetailId());
  161. if (count.compareTo(subscribeDetail.getCount()) > 0) {
  162. throw new ServiceException("采购数量不能大于申购数量");
  163. }
  164. }
  165. }
  166. purchaseProductService.saveBatch(purchaseProductList);
  167. purchaseProductMountingsService.saveBatch(addMountingsList);
  168. }
  169. if (CollectionUtils.isNotEmpty(purchaseProjectList)) {//到货
  170. for (EhsdPurchaseProject s : purchaseProjectList) {
  171. s.setPurchaseId(purchase.getId());
  172. }
  173. purchaseProjectService.saveBatch(purchaseProjectList);
  174. }
  175. if (CollectionUtils.isNotEmpty(purchaseArrivalList)) {//收费项目
  176. for (EhsdPurchaseArrival s : purchaseArrivalList) {
  177. s.setPurchaseId(purchase.getId());
  178. }
  179. purchaseArrivalService.saveBatch(purchaseArrivalList);
  180. }
  181. return purchase;
  182. }
  183. /**
  184. * 公共代码块--发起
  185. */
  186. public EhsdPurchaseDto connStart(EhsdPurchaseDto purchase) {
  187. //赋值城市省份信息
  188. CustomizeAreaUtil.setAreaId(purchase);
  189. purchase.setSellCityId(purchase.getCityId());
  190. purchase.setSellCountryId(purchase.getCountryId());
  191. purchase.setSellProvinceId(purchase.getProvinceId());
  192. purchase.setStatus(PurchaseStatusEnum.UNDER_REVIEW.getKey());
  193. String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
  194. purchase.setUserName(nickName);
  195. purchaseService.saveOrUpdate(purchase);
  196. List<EhsdPurchaseProduct> purchaseProductList = purchase.getPurchaseProductList();
  197. List<EhsdPurchaseProject> purchaseProjectList = purchase.getPurchaseProjectList();
  198. List<EhsdPurchaseArrival> purchaseArrivalList = purchase.getPurchaseArrivalList();
  199. List<EhsdPurchaseProductMountings> addMountingsList = new ArrayList<>();
  200. if (CollectionUtils.isNotEmpty(purchaseProductList)) {
  201. for (EhsdPurchaseProduct s : purchaseProductList) {//产品
  202. long id ;
  203. if(ObjectUtils.isNotEmpty(s.getId())){
  204. id = s.getId();
  205. }else{
  206. id = IdWorker.getId();
  207. }
  208. s.setId(id);
  209. s.setPurchaseId(purchase.getId());
  210. List<EhsdPurchaseProductMountings> purchaseProductMountingsList = s.getPurchaseProductMountingsList();
  211. if (CollectionUtils.isNotEmpty(purchaseProductMountingsList)) {//产品配件
  212. purchaseProductMountingsList.forEach(obj -> obj.setPurchaseProductId(id));
  213. addMountingsList.addAll(purchaseProductMountingsList);
  214. }
  215. if ("0".equals(purchase.getDataResource())) {
  216. //计算采购数量是否大于申购数量
  217. List<EhsdPurchaseProduct> purchaseDetailList1 = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getSubscribeDetailId,
  218. s.getSubscribeDetailId()));
  219. //求和
  220. BigDecimal purchaseCount = purchaseDetailList1.stream()
  221. .map(EhsdPurchaseProduct::getQuantity)
  222. .reduce(BigDecimal.ZERO, BigDecimal::add);
  223. //计算历史采购数量+本次采购数量之和
  224. BigDecimal count = purchaseCount.add(s.getQuantity());
  225. //判断采购数量是否大于申购数量
  226. SubscribeDetail subscribeDetail = subscribeDetailService.getById(s.getSubscribeDetailId());
  227. if (count.compareTo(subscribeDetail.getCount()) > 0) {
  228. throw new ServiceException("采购数量不能大于申购数量");
  229. }
  230. }
  231. }
  232. purchaseProductService.saveOrUpdateBatch(purchaseProductList);
  233. purchaseProductMountingsService.saveOrUpdateBatch(addMountingsList);
  234. }
  235. if (CollectionUtils.isNotEmpty(purchaseProjectList)) {//到货
  236. for (EhsdPurchaseProject s : purchaseProjectList) {
  237. s.setPurchaseId(purchase.getId());
  238. }
  239. purchaseProjectService.saveOrUpdateBatch(purchaseProjectList);
  240. }
  241. if (CollectionUtils.isNotEmpty(purchaseArrivalList)) {//收费项目
  242. for (EhsdPurchaseArrival s : purchaseArrivalList) {
  243. s.setPurchaseId(purchase.getId());
  244. }
  245. purchaseArrivalService.saveOrUpdateBatch(purchaseArrivalList);
  246. }
  247. return purchase;
  248. }
  249. /**
  250. * 结束流程
  251. *
  252. * @param flowId 流程ID
  253. * @param businessId 业务ID
  254. * @param submitData 数据
  255. */
  256. @Override
  257. public void end(Long flowId, Long businessId, JSONObject submitData) {
  258. //通过业务ID查询采购数据
  259. EhsdPurchase purchase = purchaseService.getById(businessId);
  260. if (ObjectUtils.isEmpty(purchase)) {
  261. throw new ServiceException("采购单不存在,或已被删除");
  262. }
  263. //查询采购产品
  264. List<EhsdPurchaseProduct> purchaseProductList = purchaseProductService.list(Wrappers.<EhsdPurchaseProduct>query().lambda().eq(EhsdPurchaseProduct::getPurchaseId, businessId));
  265. List<ContractProduct> upContractProduct = new ArrayList<>();
  266. List<SampleProduct> upSampleProduct = new ArrayList<>();
  267. for (EhsdPurchaseProduct p : purchaseProductList) {
  268. if (ObjectUtils.isNotEmpty(p.getDataResourceId()) &&
  269. p.getDataResource() == PurchaseDataResourceEnum.DATA_RESOURCE_1.getKey()) {//如果采购的是外销合同
  270. ContractProduct contractProduct = contractProductService.getById(p.getDataResourceId());
  271. BigDecimal expendQuantity = contractProduct.getExpendQuantity().subtract(p.getQuantity());
  272. if (expendQuantity.compareTo(BigDecimal.ZERO) < 0) {//小于0不让继续执行
  273. throw new ServiceException("采购数量不得大于合同剩余采购数量,请驳回审批修改");
  274. }
  275. contractProduct.setExpendQuantity(expendQuantity);
  276. upContractProduct.add(contractProduct);
  277. }
  278. if (ObjectUtils.isNotEmpty(p.getDataResourceId()) &&
  279. p.getDataResource() == PurchaseDataResourceEnum.DATA_RESOURCE_2.getKey()) {//如果采购的是样品单
  280. SampleProduct sampleProduct = sampleProductService.getById(p.getDataResourceId());
  281. BigDecimal expendQuantity = sampleProduct.getExpendQuantity().subtract(p.getQuantity());
  282. if (expendQuantity.compareTo(BigDecimal.ZERO) < 0) {//小于0不让继续执行
  283. // throw new ServiceException("采购数量不得大于合同剩余采购数量,请驳回审批修改");
  284. }
  285. sampleProduct.setExpendQuantity(expendQuantity);
  286. upSampleProduct.add(sampleProduct);
  287. }
  288. }
  289. if (CollectionUtils.isNotEmpty(upContractProduct)) {//扣减销售合同数量
  290. contractProductService.updateBatchById(upContractProduct);
  291. }
  292. if (CollectionUtils.isNotEmpty(upSampleProduct)) {//扣减样品单数量
  293. sampleProductService.updateBatchById(upSampleProduct);
  294. }
  295. //修改采购状态为审批通过
  296. purchase.setStatus(PurchaseStatusEnum.PASS.getKey());
  297. purchase.setApprovedDate(new Date());
  298. purchaseService.updateById(purchase);
  299. if ("0".equals(purchase.getDataResource())) {
  300. //修改申购明细状态
  301. List<EhsdPurchaseProduct> purchaseDetailList = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getProductId, businessId));
  302. List<Long> subscribeDetailIds = purchaseDetailList.stream().map(EhsdPurchaseProduct::getSubscribeDetailId).collect(Collectors.toList());
  303. List<SubscribeDetail> subscribeDetails = subscribeDetailService.listByIds(subscribeDetailIds);
  304. for (SubscribeDetail subscribeDetail : subscribeDetails) {
  305. //获取申购明细下的所有采购记录 计算已采购数
  306. List<EhsdPurchaseProduct> purchaseDetails = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getSubscribeDetailId,
  307. subscribeDetail.getId()).eq(EhsdPurchaseProduct::getProductId, subscribeDetail.getBussinessId()));
  308. BigDecimal count = purchaseDetails.stream()
  309. .map(EhsdPurchaseProduct::getQuantity)
  310. .reduce(BigDecimal.ZERO, BigDecimal::add);
  311. if (count.compareTo(subscribeDetail.getCount()) >= 0) {
  312. //修改为已采购
  313. subscribeDetail.setStatus(SubscribeDetailStatusEnum.PURCHASED.getKey());
  314. } else {
  315. //修改为部分采购
  316. subscribeDetail.setStatus(SubscribeDetailStatusEnum.LITT_PAID_AMOUNT.getKey());
  317. }
  318. }
  319. subscribeDetailService.updateBatchById(subscribeDetails);
  320. }
  321. }
  322. /**
  323. * 重新发起
  324. * @param flowId
  325. * @param businessId
  326. * @param flowStatus
  327. * @param submitData
  328. */
  329. @Override
  330. @LogicIgnore(tableName = {""},alias = {""})
  331. @Transactional(rollbackFor = Exception.class)
  332. public void relaunch(Long flowId, Long businessId, FlowStatusEnum flowStatus, JSONObject submitData) {
  333. //删除采购合同
  334. EhsdPurchaseDto purchase = submitData.toJavaObject(EhsdPurchaseDto.class);
  335. if(ObjectUtils.isEmpty(purchase)){
  336. throw new ServiceException("采购数据不能为空");
  337. }
  338. purchase.setFlowId(flowId);
  339. connStart(purchase);
  340. }
  341. /**
  342. * 驳回
  343. * @param flowId
  344. * @param businessId
  345. * @param flowStatus
  346. */
  347. @Override
  348. public void reject(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
  349. if (HandleTypeEnum.REJECT.equals(FlowThreadLocalUtil.getHandleTypeEnum())) {
  350. sampleService.update(q -> q
  351. .eq(Sample::getId, FlowThreadLocalUtil.getBusinessId())
  352. .set(Sample::getStatus, 20)//20为驳回
  353. .set(Sample::getUpdateTime, new Date())
  354. .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
  355. );
  356. }
  357. }
  358. }