FlowClient.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.fjhx.feign;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.fjhx.base.BaseEntity;
  5. import com.fjhx.constants.FlowConstant;
  6. import com.fjhx.constants.FlowExplainConstant;
  7. import com.fjhx.constants.StatusConstant;
  8. import com.fjhx.entity.example.ExampleDetails;
  9. import com.fjhx.entity.example.ExampleInfo;
  10. import com.fjhx.entity.process.ProcessInfo;
  11. import com.fjhx.entity.process.ProcessNode;
  12. import com.fjhx.entity.process.ProcessNodeButton;
  13. import com.fjhx.entity.process.ProcessTenant;
  14. import com.fjhx.enums.ButtonNameEnum;
  15. import com.fjhx.enums.HandleResultEnum;
  16. import com.fjhx.enums.ProcessNodeHandleObjectTypeEnum;
  17. import com.fjhx.params.ExampleResult;
  18. import com.fjhx.params.JumpVo;
  19. import com.fjhx.service.example.ExampleDetailsService;
  20. import com.fjhx.service.example.ExampleInfoService;
  21. import com.fjhx.service.process.ProcessInfoService;
  22. import com.fjhx.service.process.ProcessNodeButtonService;
  23. import com.fjhx.service.process.ProcessNodeService;
  24. import com.fjhx.service.process.ProcessTenantService;
  25. import com.fjhx.utils.Assert;
  26. import org.springblade.core.secure.utils.AuthUtil;
  27. import org.springblade.core.tool.api.R;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.web.bind.annotation.GetMapping;
  31. import org.springframework.web.bind.annotation.PostMapping;
  32. import org.springframework.web.bind.annotation.RestController;
  33. import java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.List;
  36. @RestController
  37. public class FlowClient implements IFlowClient {
  38. @Autowired
  39. private ExampleInfoService exampleInfoService;
  40. @Autowired
  41. private ProcessInfoService processInfoService;
  42. @Autowired
  43. private ProcessTenantService processTenantService;
  44. @Autowired
  45. private ProcessNodeService processNodeService;
  46. @Autowired
  47. private ProcessNodeButtonService processNodeButtonService;
  48. @Autowired
  49. private ExampleDetailsService exampleDetailsService;
  50. @Transactional(rollbackFor = Exception.class)
  51. @GetMapping(CREATE)
  52. @Override
  53. public R<ExampleResult> create(Long flowLinkNo, String code, String nodeCode, String title, String remarks, String cacheData) {
  54. ExampleResult exampleResult = new ExampleResult();
  55. Long count = exampleInfoService.count(Wrappers.<ExampleInfo>lambdaQuery()
  56. .eq(ExampleInfo::getFlowLinkNo, flowLinkNo)
  57. .eq(ExampleInfo::getComplete, StatusConstant.NO));
  58. Assert.eqZero(count, FlowExplainConstant.EXPLAIN_NOT_EXIST);
  59. // 查找通用流程
  60. ProcessInfo processInfo = processInfoService.getOne(ProcessInfo::getCode, code);
  61. Assert.notEmpty(processInfo, String.format(FlowExplainConstant.PROCESS_NOT_EXIST, code));
  62. // 获取租户流程
  63. ProcessTenant processTenant = processTenantService.getOne(Wrappers.<ProcessTenant>lambdaQuery()
  64. .eq(ProcessTenant::getProcessInfoId, processInfo.getId())
  65. .eq(ProcessTenant::getBindingTenantId, AuthUtil.getTenantId())
  66. .eq(ProcessTenant::getCurrentVersion, StatusConstant.YES));
  67. // 创建流程实例
  68. ExampleInfo exampleInfo = new ExampleInfo();
  69. exampleInfo.setProcessInfoId(processInfo.getId());
  70. exampleInfo.setTitle(title);
  71. exampleInfo.setFlowLinkNo(flowLinkNo);
  72. exampleInfo.setCacheData(cacheData);
  73. // 走默认流程,直接完成
  74. if (processTenant == null) {
  75. exampleInfo.setComplete(StatusConstant.YES);
  76. exampleInfo.setProcessNodeId(FlowConstant.OVER_PROCESS_FLAG);
  77. exampleInfo.setProcessNodeCode(FlowConstant.END_CODE);
  78. exampleInfo.setFlowType(FlowConstant.FLOW_TYPE_DEFAULT);
  79. exampleInfo.setHandleResult(HandleResultEnum.SUCCESS.getType());
  80. exampleInfoService.save(exampleInfo);
  81. // 开始流程明细
  82. ExampleDetails startDetails = new ExampleDetails();
  83. startDetails.setExampleInfoId(exampleInfo.getId());
  84. startDetails.setProcessInfoId(processInfo.getId());
  85. startDetails.setNameType(ButtonNameEnum.START.getType());
  86. startDetails.setHandleUserId(AuthUtil.getUserId());
  87. startDetails.setRemarks(remarks);
  88. // 结束流程明细
  89. ExampleDetails endDetails = new ExampleDetails();
  90. endDetails.setExampleInfoId(exampleInfo.getId());
  91. endDetails.setProcessInfoId(processInfo.getId());
  92. endDetails.setNameType(ButtonNameEnum.END.getType());
  93. // 封装返回值
  94. exampleResult.setOldExampleInfo(exampleInfo);
  95. exampleResult.setExampleInfo(exampleInfo);
  96. exampleResult.setExampleDetailsIdList(Arrays.asList(startDetails.getId(), endDetails.getId()));
  97. exampleResult.setHandleType(ButtonNameEnum.END.getType());
  98. exampleResult.setCacheDataStr(cacheData);
  99. return R.data(exampleResult);
  100. }
  101. // 查询节点
  102. ProcessNodeButton processNodeButton = processNodeButtonService.getOne(Wrappers.<ProcessNodeButton>lambdaQuery()
  103. .eq(ProcessNodeButton::getProcessTenantId, processTenant.getId())
  104. .eq(ProcessNodeButton::getProcessNodeCode, FlowConstant.START_CODE));
  105. Assert.notEmpty(processNodeButton, FlowExplainConstant.SPECIFY_BRANCH_EMPTY);
  106. exampleInfo.setFlowType(FlowConstant.FLOW_TYPE_TENANT);
  107. exampleInfo.setProcessTenantId(processTenant.getId());
  108. exampleInfo.setProcessNodeId(processNodeButton.getProcessNodeId());
  109. exampleInfo.setProcessNodeCode(processNodeButton.getProcessNodeCode());
  110. exampleInfo.setComplete(StatusConstant.NO);
  111. exampleInfo.setHandleResult(HandleResultEnum.IN_PROGRESS.getType());
  112. exampleInfoService.save(exampleInfo);
  113. JumpVo jumpVo = new JumpVo();
  114. setJumpNode(jumpVo, processNodeButton, nodeCode);
  115. exampleResult.setJumpVo(jumpVo);
  116. return R.data(exampleResult);
  117. }
  118. @GetMapping(GET_JUMP_VO)
  119. @Override
  120. public R<JumpVo> getJumpVo(Long buttonId, String nodeCode) {
  121. JumpVo jumpVo = new JumpVo();
  122. ProcessNodeButton processNodeButton = processNodeButtonService.getById(buttonId);
  123. Assert.notEmpty(processNodeButton, "nodeButton is null");
  124. setJumpNode(jumpVo, processNodeButton, nodeCode);
  125. return R.data(jumpVo);
  126. }
  127. @Transactional(rollbackFor = Exception.class)
  128. @PostMapping(JUMP)
  129. @Override
  130. public R<ExampleResult> jump(JumpVo jumpVo) {
  131. // 返回值
  132. ExampleResult exampleResult = new ExampleResult();
  133. // 查询流程实例
  134. ExampleInfo oleExampleInfo = exampleInfoService.getOne(Wrappers.<ExampleInfo>lambdaQuery()
  135. .eq(ExampleInfo::getFlowLinkNo, jumpVo.getFlowLinkNo())
  136. .eq(ExampleInfo::getComplete, StatusConstant.NO));
  137. Assert.notEmpty(oleExampleInfo, FlowExplainConstant.EXAMPLE_INFO_NULL);
  138. // 复制除一个对象做修改
  139. ExampleInfo exampleInfo = ObjectUtil.clone(oleExampleInfo);
  140. String cacheDataStr = jumpVo.getCacheDataStr();
  141. if (ObjectUtil.isNotEmpty(cacheDataStr)) {
  142. exampleInfo.setCacheData(cacheDataStr);
  143. }
  144. // 明细
  145. ExampleDetails exampleDetails = createExampleDetails(exampleInfo, jumpVo.getNameType());
  146. exampleDetails.setProcessNodeButtonId(jumpVo.getButtonId());
  147. exampleDetails.setHandleUserId(AuthUtil.getUserId());
  148. exampleDetails.setRemarks(jumpVo.getRemarks());
  149. // 赋值按钮处理方法
  150. exampleResult.setHandleType(jumpVo.getNameType());
  151. // 驳回
  152. if (ButtonNameEnum.REJECT.getType().equals(jumpVo.getNameType())) {
  153. exampleInfo.setHandleResult(HandleResultEnum.FAIL.getType());
  154. }
  155. // 通过
  156. else if (FlowConstant.END_CODE.equals(jumpVo.getJumpNodeCode())) {
  157. exampleInfo.setHandleResult(HandleResultEnum.SUCCESS.getType());
  158. // 赋值结束处理方法
  159. exampleResult.setHandleType(ButtonNameEnum.END.getType());
  160. }
  161. exampleInfo.setProcessNodeId(jumpVo.getJumpNodeId());
  162. exampleInfo.setProcessNodeCode(jumpVo.getJumpNodeCode());
  163. exampleInfo.setHandleObjectType(jumpVo.getHandleObjectType());
  164. exampleInfo.setHandleObjectIdSet(jumpVo.getHandleObjectIdSet());
  165. List<Long> exampleDetailsIdList = new ArrayList<>();
  166. // 流程结束
  167. if (FlowConstant.END_CODE.equals(jumpVo.getJumpNodeCode())) {
  168. // 赋值流程完成
  169. exampleInfo.setComplete(StatusConstant.YES);
  170. // 流程明细
  171. ExampleDetails endExampleDetails = createExampleDetails(exampleInfo, ButtonNameEnum.END.getType());
  172. exampleDetailsService.saveBatch(Arrays.asList(exampleDetails, endExampleDetails));
  173. exampleDetailsIdList.add(exampleDetails.getId());
  174. exampleDetailsIdList.add(endExampleDetails.getId());
  175. }
  176. // 正常跳转
  177. else {
  178. // 流程明细
  179. exampleDetailsService.save(exampleDetails);
  180. exampleDetailsIdList.add(exampleDetails.getId());
  181. // 如果跳转节点到发起(一直回退到发起人),处理人只能是发起人
  182. if (FlowConstant.START_CODE.equals(jumpVo.getJumpNodeCode())) {
  183. exampleInfo.setHandleObjectType(ProcessNodeHandleObjectTypeEnum.USER.getType());
  184. exampleInfo.setHandleObjectIdSet(exampleInfo.getCreateUser().toString());
  185. }
  186. }
  187. exampleInfoService.updateById(exampleInfo);
  188. exampleResult.setOldExampleInfo(oleExampleInfo);
  189. exampleResult.setExampleInfo(exampleInfo);
  190. exampleResult.setCacheDataStr(exampleInfo.getCacheData());
  191. exampleResult.setExampleDetailsIdList(exampleDetailsIdList);
  192. return R.data(exampleResult);
  193. }
  194. @PostMapping(ROLL_BACK)
  195. @Override
  196. public void rollBack(ExampleResult result) {
  197. ExampleInfo oldExampleInfo = result.getOldExampleInfo();
  198. if (result.isDeleteExample()) {
  199. exampleInfoService.update(Wrappers.<ExampleInfo>lambdaUpdate()
  200. .eq(BaseEntity::getId, oldExampleInfo.getId())
  201. .set(ExampleInfo::getDelFlag, 1)
  202. .set(ExampleInfo::getRemarks, "业务异常,本次流程实例已删除")
  203. );
  204. } else {
  205. exampleInfoService.updateById(oldExampleInfo);
  206. }
  207. exampleDetailsService.update(Wrappers.<ExampleDetails>lambdaUpdate()
  208. .in(BaseEntity::getId, result.getExampleDetailsIdList())
  209. .set(ExampleDetails::getDelFlag, 1)
  210. .set(ExampleDetails::getRemarks, "业务异常,本次流程明细已删除")
  211. );
  212. }
  213. private void setJumpNode(JumpVo jumpVo, ProcessNodeButton processNodeButton, String nodeCode) {
  214. // 赋值按钮名称类型
  215. jumpVo.setNameType(processNodeButton.getNameType());
  216. jumpVo.setButtonId(processNodeButton.getId());
  217. ProcessNode processNode;
  218. // 非分支跳转
  219. if (ObjectUtil.isNotEmpty(processNodeButton.getJumpNodeId())) {
  220. processNode = processNodeService.getById(processNodeButton.getJumpNodeId());
  221. }
  222. // 分支跳转
  223. else {
  224. // 跳转节点编码不为空
  225. Assert.notEmpty(nodeCode, FlowExplainConstant.SPECIFY_BRANCH_EMPTY);
  226. // 查询跳转到哪个分支节点
  227. processNode = processNodeService.getOne(Wrappers.<ProcessNode>lambdaQuery()
  228. .eq(ProcessNode::getParentId, processNodeButton.getProcessNodeId())
  229. .eq(ProcessNode::getCode, nodeCode));
  230. // 分支节点不为空
  231. Assert.notEmpty(processNode, FlowExplainConstant.SPECIFY_BRANCH_EMPTY);
  232. }
  233. jumpVo.setJumpNodeId(processNode.getId());
  234. jumpVo.setJumpNodeCode(processNode.getCode());
  235. jumpVo.setHandleObjectType(processNode.getHandleObjectType());
  236. jumpVo.setHandleObjectIdSet(processNode.getHandleObjectIdSet());
  237. }
  238. /**
  239. * 创建流程实例明细
  240. */
  241. private ExampleDetails createExampleDetails(ExampleInfo exampleInfo, Integer nameType) {
  242. ExampleDetails exampleDetails = new ExampleDetails();
  243. exampleDetails.setExampleInfoId(exampleInfo.getId());
  244. exampleDetails.setProcessInfoId(exampleInfo.getProcessInfoId());
  245. exampleDetails.setProcessTenantId(exampleInfo.getProcessTenantId());
  246. exampleDetails.setProcessNodeId(exampleInfo.getProcessNodeId());
  247. exampleDetails.setNameType(nameType);
  248. return exampleDetails;
  249. }
  250. }