SampleUpdateFlow.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.DS;
  5. import com.baomidou.mybatisplus.core.toolkit.*;
  6. import com.fjhx.area.utils.CustomizeAreaUtil;
  7. import com.fjhx.common.constant.SourceConstant;
  8. import com.fjhx.common.enums.FlowStatusEnum1;
  9. import com.fjhx.file.utils.ObsFileUtil;
  10. import com.fjhx.flow.core.FlowDelegate;
  11. import com.fjhx.sale.entity.sample.dto.SampleDto;
  12. import com.fjhx.sale.entity.sample.po.Sample;
  13. import com.fjhx.sale.entity.sample.po.SampleProduct;
  14. import com.fjhx.sale.entity.sample.po.SampleProject;
  15. import com.fjhx.sale.entity.sample.po.SampleShipment;
  16. import com.fjhx.sale.service.sample.SampleProductService;
  17. import com.fjhx.sale.service.sample.SampleProjectService;
  18. import com.fjhx.sale.service.sample.SampleService;
  19. import com.fjhx.sale.service.sample.SampleShipmentService;
  20. import com.ruoyi.common.exception.ServiceException;
  21. import com.ruoyi.common.utils.SecurityUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Component;
  24. import java.util.*;
  25. import java.util.regex.Matcher;
  26. import java.util.regex.Pattern;
  27. import java.util.stream.Collectors;
  28. /**
  29. * 样品单变更流程
  30. */
  31. @Component
  32. @DS(SourceConstant.SALE)
  33. public class SampleUpdateFlow extends FlowDelegate {
  34. @Autowired
  35. private SampleFlow sampleFlow;
  36. @Autowired
  37. private SampleService sampleService;
  38. @Autowired
  39. private SampleProductService sampleProductService;
  40. @Autowired
  41. private SampleProjectService sampleProjectService;
  42. @Autowired
  43. private SampleShipmentService sampleShipmentService;
  44. @Override
  45. public String getFlowKey() {
  46. return "sample_update_flow";
  47. }
  48. /**
  49. * 发起流程
  50. *
  51. * @param flowId 流程ID
  52. * @param submitData 样品单数据
  53. * @return
  54. */
  55. @Override
  56. public Long start(Long flowId, JSONObject submitData) {
  57. SampleDto newSample = submitData.toJavaObject(SampleDto.class);
  58. // 原样品单id不能为空
  59. Long oldSampleId = newSample.getOldSampleId();
  60. if (oldSampleId == null) {
  61. throw new ServiceException("原样品单id不能为空");
  62. }
  63. //查询原样品单
  64. Sample oldSample = sampleService.getById(newSample.getOldSampleId());
  65. if(ObjectUtil.isEmpty(oldSample)){
  66. throw new ServiceException("查询不到原样品单信息");
  67. }
  68. //更新原样品单状态为变更中
  69. oldSample.setStatus(FlowStatusEnum1.UPDATE_LOADING.getKey());
  70. sampleService.updateById(oldSample);
  71. //查询旧样品单的订单产品
  72. List<SampleProduct> oldSampleProductList = sampleProductService.list(q -> q.eq(SampleProduct::getSampleId, oldSampleId));
  73. if(CollectionUtils.isEmpty(oldSampleProductList)){
  74. throw new ServiceException("原样品单没有产品");
  75. }
  76. Map<Long,List<SampleProduct>> oldSampleMap = oldSampleProductList.stream().collect(Collectors.groupingBy(SampleProduct::getId));
  77. List<SampleProduct> newSampleProductList = newSample.getSampleProductList();
  78. if(CollectionUtils.isEmpty(newSampleProductList)){
  79. throw new ServiceException("变更样品单产品不能为空");
  80. }
  81. /**
  82. * 赋值新的变更样品单号
  83. */
  84. String code = oldSample.getCode();
  85. Matcher matcher = Pattern.compile(".*\\((.*?)\\)$").matcher(code);
  86. int index = 2;
  87. if (matcher.find()) {
  88. index = (Integer.parseInt(matcher.group(1)) + 1);
  89. code = code.substring(0, code.lastIndexOf("("));
  90. }
  91. newSample.setCode(code + "(" + index + ")");
  92. newSample.setIsShow(1);//隐藏样品单
  93. /**
  94. * 计算新样品单的剩余数量
  95. */
  96. for(SampleProduct newCp:newSampleProductList){
  97. newCp.setExpendQuantity(newCp.getQuantity());
  98. if(ObjectUtil.isNotEmpty(newCp.getId())){//如果新样品单产品ID不为空
  99. //取出旧样品单
  100. SampleProduct oldSampleProduct = oldSampleMap.getOrDefault(newCp.getId(),null).get(0);
  101. //取出旧样品单包装方式
  102. JSONObject oldJson = JSONObject.parseObject(oldSampleProduct.getEhsdJson());
  103. String oldPackMethod = oldJson.getOrDefault("packMethod",null)==null?null:oldJson.getOrDefault("packMethod",null).toString();
  104. //取出新样品单包装方式
  105. JSONObject newJson = JSONObject.parseObject(newCp.getEhsdJson());
  106. String newPackMethod = newJson.getOrDefault("packMethod",null)==null?null:oldJson.getOrDefault("packMethod",null).toString();
  107. /**
  108. * 商品英文名、尺寸、包装方式、数量 没有变更---取原本的剩余数量
  109. */
  110. if(oldSampleProduct.getQuantity().compareTo(newCp.getQuantity())==0
  111. && StringUtils.equals(oldSampleProduct.getProductName(), newCp.getProductName())
  112. && StringUtils.equals(oldSampleProduct.getProductModel(),newCp.getProductModel())
  113. && StringUtils.equals(oldPackMethod,newPackMethod)){
  114. //取出旧的剩余数量
  115. newCp.setExpendQuantity(oldSampleProduct.getExpendQuantity());
  116. }
  117. }
  118. }
  119. return update(newSample);
  120. }
  121. /**
  122. * 结束流程
  123. *
  124. * @param flowId 流程ID
  125. * @param businessId 业务ID
  126. * @param submitData 数据
  127. */
  128. @Override
  129. public void end(Long flowId, Long businessId, JSONObject submitData) {
  130. // 通过业务ID查询样品单数据
  131. Sample newSample = sampleService.getById(businessId);
  132. if (ObjectUtils.isEmpty(newSample)) {
  133. throw new ServiceException("样品单不存在");
  134. }
  135. long oldSampleId = newSample.getOldSampleId();//取出旧的样品单ID
  136. Sample oldSample = sampleService.getById(oldSampleId);
  137. if (oldSample == null) {
  138. throw new ServiceException("原样品单不存在");
  139. }
  140. long temOldId = IdWorker.getId();
  141. long temNewId = IdWorker.getId();
  142. Sample temOldUpSample = new Sample();
  143. temOldUpSample.setId(temOldId);
  144. temOldUpSample.setUpId(oldSampleId);
  145. Sample temNewUpSample = new Sample();
  146. temNewUpSample.setId(temNewId);
  147. temNewUpSample.setUpId(businessId);
  148. //替换新旧数据ID为临时ID
  149. sampleService.updateSample(temOldUpSample);
  150. sampleService.updateSample(temNewUpSample);
  151. //查询新数据产品、收费、出货
  152. List<Long> newSampleProductIds = sampleProductService.list(Wrappers.<SampleProduct>query().lambda().select(SampleProduct::getId).eq(SampleProduct::getSampleId,businessId)).stream().map(SampleProduct::getId).collect(Collectors.toList());
  153. List<Long> newSampleProjectIds = sampleProjectService.list(Wrappers.<SampleProject>query().lambda().select(SampleProject::getId).eq(SampleProject::getSampleId,businessId)).stream().map(SampleProject::getId).collect(Collectors.toList());
  154. List<Long> newSampleShipmentIds = sampleShipmentService.list(Wrappers.<SampleShipment>query().lambda().select(SampleShipment::getId).eq(SampleShipment::getSampleId,businessId)).stream().map(SampleShipment::getId).collect(Collectors.toList());
  155. //查询旧数据产品、收费、出货
  156. List<Long> oldSampleProductIds = sampleProductService.list(Wrappers.<SampleProduct>query().lambda().select(SampleProduct::getId).eq(SampleProduct::getSampleId,oldSampleId)).stream().map(SampleProduct::getId).collect(Collectors.toList());
  157. List<Long> oldSampleProjectIds = sampleProjectService.list(Wrappers.<SampleProject>query().lambda().select(SampleProject::getId).eq(SampleProject::getSampleId,oldSampleId)).stream().map(SampleProject::getId).collect(Collectors.toList());
  158. List<Long> oldSampleShipmentIds = sampleShipmentService.list(Wrappers.<SampleShipment>query().lambda().select(SampleShipment::getId).eq(SampleShipment::getSampleId,oldSampleId)).stream().map(SampleShipment::getId).collect(Collectors.toList());
  159. /**
  160. * 处理新样品单---
  161. */
  162. newSample.setId(oldSampleId);//id赋值为旧样品单的id
  163. newSample.setStatus(FlowStatusEnum1.PASS.getKey());
  164. newSample.setApprovedDate(new Date());
  165. newSample.setUpId(temNewId);
  166. newSample.setOldSampleId(businessId);
  167. newSample.setIsShow(0);//显示新样品单
  168. sampleService.updateSample(newSample);
  169. //修改样品单产品相关数据
  170. if(CollectionUtils.isNotEmpty(newSampleProductIds)){
  171. sampleProductService.update(Wrappers.<SampleProduct>update().lambda().set(SampleProduct::getSampleId,oldSampleId).in(SampleProduct::getId,newSampleProductIds));
  172. }
  173. //修改样品单收费相关数据
  174. if(CollectionUtils.isNotEmpty(newSampleProjectIds)){
  175. sampleProjectService.update(Wrappers.<SampleProject>update().lambda().set(SampleProject::getSampleId,oldSampleId).in(SampleProject::getId,newSampleProjectIds));
  176. }
  177. //修改样品单出货相关数据
  178. if(CollectionUtils.isNotEmpty(newSampleShipmentIds)){
  179. sampleShipmentService.update(Wrappers.<SampleShipment>update().lambda().set(SampleShipment::getSampleId,oldSampleId).in(SampleShipment::getId,newSampleShipmentIds));
  180. }
  181. /**
  182. * 处理旧的样品单---
  183. */
  184. oldSample.setId(businessId);
  185. oldSample.setStatus(FlowStatusEnum1.UPDATE.getKey());
  186. oldSample.setIsChange("1");
  187. oldSample.setUpId(temOldId);
  188. oldSample.setIsShow(1);//隐藏旧的样品单
  189. sampleService.updateSample(oldSample);
  190. //修改样品单产品相关数据
  191. if(CollectionUtils.isNotEmpty(oldSampleProductIds)){
  192. sampleProductService.update(Wrappers.<SampleProduct>update().lambda().set(SampleProduct::getSampleId,businessId).in(SampleProduct::getId,oldSampleProductIds));
  193. }
  194. //修改样品单收费相关数据
  195. if(CollectionUtils.isNotEmpty(oldSampleProjectIds)){
  196. sampleProjectService.update(Wrappers.<SampleProject>update().lambda().set(SampleProject::getSampleId,businessId).in(SampleProject::getId,oldSampleProjectIds));
  197. }
  198. //修改样品单出货相关数据
  199. if(CollectionUtils.isNotEmpty(oldSampleShipmentIds)){
  200. sampleShipmentService.update(Wrappers.<SampleShipment>update().lambda().set(SampleShipment::getSampleId,businessId).in(SampleShipment::getId,oldSampleShipmentIds));
  201. }
  202. ObsFileUtil.exchangeBusinessId(oldSampleId,businessId);
  203. }
  204. /**
  205. * 变更数据
  206. * @param sample
  207. * @return
  208. */
  209. public Long update(SampleDto sample){
  210. //赋值城市省份信息
  211. CustomizeAreaUtil.setAreaId(sample);
  212. sample.setBuyCityId(sample.getCityId());
  213. sample.setBuyCountryId(sample.getCountryId());
  214. sample.setBuyProvinceId(sample.getProvinceId());
  215. // sample.setCode(CodeEnum.SAMPLE_CODE.getCode());
  216. sample.setStatus(FlowStatusEnum1.UNDER_REVIEW.getKey());
  217. sample.setUserName(SecurityUtils.getUsername());
  218. sampleService.save(sample);
  219. List<SampleProduct> sampleProductList = sample.getSampleProductList();
  220. if(CollectionUtils.isNotEmpty(sampleProductList)){//保存样品单产品
  221. for(SampleProduct c : sampleProductList){
  222. c.setId(IdWorker.getId());
  223. c.setSampleId(sample.getId());
  224. c.setExpendQuantity(c.getQuantity());
  225. ObsFileUtil.saveFile(c.getFileList(),c.getId());
  226. }
  227. sampleProductService.saveBatch(sampleProductList);
  228. }
  229. List<SampleProject> sampleProjectList = sample.getSampleProjectList();
  230. if(CollectionUtils.isNotEmpty(sampleProjectList)){//保存收费项目
  231. for(SampleProject c : sampleProjectList){
  232. c.setId(IdWorker.getId());
  233. c.setSampleId(sample.getId());
  234. }
  235. sampleProjectService.saveBatch(sampleProjectList);
  236. }
  237. List<SampleShipment> sampleShipmentList = sample.getSampleShipmentList();
  238. if(CollectionUtils.isNotEmpty(sampleShipmentList)){//保存自定义出货
  239. for(SampleShipment c : sampleShipmentList){
  240. c.setId(IdWorker.getId());
  241. c.setSampleId(sample.getId());
  242. }
  243. sampleShipmentService.saveBatch(sampleShipmentList);
  244. }
  245. //交接单附件
  246. ObsFileUtil.copyFileAndSave(sample.getFileList(),sample.getId(),1);
  247. //包装附件
  248. ObsFileUtil.copyFileAndSave(sample.getPackageFileList(),sample.getId(),2);
  249. return sample.getId();
  250. }
  251. }