TFCustomFlowController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.qmth.distributed.print.api;
  2. import com.qmth.boot.api.annotation.Aac;
  3. import com.qmth.boot.api.annotation.BOOL;
  4. import com.qmth.boot.api.constant.ApiConstant;
  5. import com.qmth.boot.api.exception.ApiException;
  6. import com.qmth.distributed.print.business.bean.flow.CustomFlowSaveDto;
  7. import com.qmth.distributed.print.business.entity.TFCustomFlow;
  8. import com.qmth.distributed.print.business.service.ActivitiService;
  9. import com.qmth.distributed.print.business.service.TFCustomFlowService;
  10. import com.qmth.teachcloud.common.contant.SystemConstant;
  11. import com.qmth.teachcloud.common.entity.BasicSchool;
  12. import com.qmth.teachcloud.common.entity.SysUser;
  13. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  14. import com.qmth.teachcloud.common.service.CommonCacheService;
  15. import com.qmth.teachcloud.common.util.*;
  16. import io.swagger.annotations.*;
  17. import org.activiti.engine.TaskService;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import org.springframework.validation.BindingResult;
  22. import org.springframework.validation.annotation.Validated;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.annotation.Resource;
  25. import javax.validation.Valid;
  26. import java.security.NoSuchAlgorithmException;
  27. import java.util.Map;
  28. import java.util.Objects;
  29. import java.util.concurrent.atomic.AtomicInteger;
  30. /**
  31. * <p>
  32. * 自定义流程表 前端控制器
  33. * </p>
  34. *
  35. * @author wangliang
  36. * @since 2022-01-21
  37. */
  38. @Api(tags = "自定义流程Controller")
  39. @RestController
  40. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.customFlow}")
  41. @Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
  42. @Validated
  43. public class TFCustomFlowController {
  44. private final static Logger log = LoggerFactory.getLogger(TFCustomFlowController.class);
  45. @Resource
  46. TFCustomFlowService tfCustomFlowService;
  47. @Resource
  48. RedisUtil redisUtil;
  49. @Resource
  50. ActivitiService activitiService;
  51. @Resource
  52. CommonCacheService commonCacheService;
  53. @ApiOperation(value = "保存和发布流程")
  54. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  55. @RequestMapping(value = "/save", method = RequestMethod.POST)
  56. @Transactional
  57. public Result save(@Valid @RequestBody CustomFlowSaveDto customFlowSaveDto, BindingResult bindingResult) throws NoSuchAlgorithmException {
  58. if (bindingResult.hasErrors()) {
  59. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  60. }
  61. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  62. customFlowSaveDto.setSchoolAndOrgInfo(sysUser.getSchoolId(), sysUser.getOrgId());
  63. String flowBpmnId = MD5Util.encoder(customFlowSaveDto.toString());
  64. BasicSchool basicSchool = commonCacheService.schoolCache(customFlowSaveDto.getSchoolId());
  65. flowBpmnId = basicSchool.getCode() + "_" + flowBpmnId;
  66. boolean lock = redisUtil.lock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId, SystemConstant.REDIS_LOCK_CUSTOM_FLOW_TIME_OUT);
  67. if (!lock) {
  68. throw ExceptionResultEnum.ERROR.exception("正在发布中,请稍候再试!");
  69. }
  70. try {
  71. Integer dbVersion = tfCustomFlowService.findMaxVersion(customFlowSaveDto.getSchoolId(), customFlowSaveDto.getOrgId(), customFlowSaveDto.getType());
  72. TFCustomFlow tfCustomFlow = new TFCustomFlow(customFlowSaveDto.getSchoolId(), customFlowSaveDto.getOrgId(), customFlowSaveDto.getName(), customFlowSaveDto.getType(), customFlowSaveDto.getPublish(), JacksonUtil.parseJson(customFlowSaveDto.getCustomFlowLists()), sysUser.getId(), flowBpmnId);
  73. AtomicInteger atomicInteger = null;
  74. if (Objects.isNull(dbVersion)) {//新增
  75. atomicInteger = new AtomicInteger(1);
  76. tfCustomFlow.setVersion(atomicInteger.get());
  77. } else {//版本号递增
  78. atomicInteger = new AtomicInteger(dbVersion);
  79. tfCustomFlow.setVersion(atomicInteger.incrementAndGet());
  80. }
  81. //自定义流程处理开始
  82. Map<String, Object> map = activitiService.dynamicBuildBpmn(customFlowSaveDto, flowBpmnId);
  83. tfCustomFlow.setFlowProcessVar(JacksonUtil.parseJson(map));
  84. tfCustomFlow.setActFlowId((String) map.get(SystemConstant.PROCESS_DEFINITION_ID));
  85. tfCustomFlowService.save(tfCustomFlow);
  86. } catch (Exception e) {
  87. log.error(SystemConstant.LOG_ERROR, e);
  88. if (e instanceof ApiException) {
  89. ResultUtil.error((ApiException) e, e.getMessage());
  90. } else {
  91. ResultUtil.error(e.getMessage());
  92. }
  93. } finally {
  94. redisUtil.releaseLock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId);
  95. }
  96. return ResultUtil.ok(true);
  97. }
  98. @Resource
  99. TaskService taskService;
  100. @ApiOperation(value = "测试启动流程")
  101. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  102. @RequestMapping(value = "/testStart", method = RequestMethod.POST)
  103. @Transactional
  104. public Result testStart(@ApiParam(value = "流程id", required = true) @RequestParam String id) {
  105. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  106. activitiService.customFlowStartUpdateApproveId(SystemConstant.convertIdToLong(id), sysUser.getId());
  107. return ResultUtil.ok(true);
  108. }
  109. }