TFCustomFlowController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package com.qmth.distributed.print.api;
  2. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.google.gson.Gson;
  5. import com.google.gson.reflect.TypeToken;
  6. import com.qmth.boot.api.constant.ApiConstant;
  7. import com.qmth.boot.api.exception.ApiException;
  8. import com.qmth.distributed.print.business.bean.flow.CustomFlowDto;
  9. import com.qmth.distributed.print.business.bean.flow.CustomFlowSaveDto;
  10. import com.qmth.distributed.print.business.bean.params.CustomFlowParam;
  11. import com.qmth.distributed.print.business.bean.params.CustomFlowRenameParam;
  12. import com.qmth.distributed.print.business.bean.params.FlowTaskApproveParam;
  13. import com.qmth.distributed.print.business.bean.result.FlowApproveListResult;
  14. import com.qmth.distributed.print.business.bean.result.FlowTaskResult;
  15. import com.qmth.distributed.print.business.bean.result.FlowViewResult;
  16. import com.qmth.distributed.print.business.bean.result.TaskInfoResult;
  17. import com.qmth.distributed.print.business.entity.TFCustomFlow;
  18. import com.qmth.distributed.print.business.entity.TFCustomFlowEntity;
  19. import com.qmth.distributed.print.business.entity.TFFlowApprove;
  20. import com.qmth.distributed.print.business.service.ActivitiService;
  21. import com.qmth.distributed.print.business.service.TFCustomFlowEntityService;
  22. import com.qmth.distributed.print.business.service.TFCustomFlowService;
  23. import com.qmth.distributed.print.business.service.TFFlowApproveService;
  24. import com.qmth.teachcloud.common.contant.SystemConstant;
  25. import com.qmth.teachcloud.common.entity.BasicSchool;
  26. import com.qmth.teachcloud.common.entity.SysUser;
  27. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  28. import com.qmth.teachcloud.common.enums.FieldUniqueEnum;
  29. import com.qmth.teachcloud.common.enums.FlowStatusEnum;
  30. import com.qmth.teachcloud.common.enums.TFCustomTypeEnum;
  31. import com.qmth.teachcloud.common.service.CommonCacheService;
  32. import com.qmth.teachcloud.common.util.*;
  33. import io.swagger.annotations.*;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import org.springframework.dao.DuplicateKeyException;
  37. import org.springframework.transaction.annotation.Transactional;
  38. import org.springframework.validation.BindingResult;
  39. import org.springframework.validation.annotation.Validated;
  40. import org.springframework.web.bind.annotation.*;
  41. import javax.annotation.Resource;
  42. import javax.validation.Valid;
  43. import javax.validation.constraints.Max;
  44. import javax.validation.constraints.Min;
  45. import java.security.NoSuchAlgorithmException;
  46. import java.util.*;
  47. import java.util.concurrent.atomic.AtomicInteger;
  48. /**
  49. * <p>
  50. * 自定义流程表 前端控制器
  51. * </p>
  52. *
  53. * @author wangliang
  54. * @since 2022-01-21
  55. */
  56. @Api(tags = "自定义流程Controller")
  57. @RestController
  58. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.customFlow}")
  59. //@Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
  60. @Validated
  61. public class TFCustomFlowController {
  62. private final static Logger log = LoggerFactory.getLogger(TFCustomFlowController.class);
  63. @Resource
  64. TFCustomFlowService tfCustomFlowService;
  65. @Resource
  66. RedisUtil redisUtil;
  67. @Resource
  68. ActivitiService activitiService;
  69. @Resource
  70. CommonCacheService commonCacheService;
  71. @Resource
  72. TFFlowApproveService tfFlowApproveService;
  73. @Resource
  74. TFCustomFlowEntityService tfCustomFlowEntityService;
  75. @ApiOperation(value = "保存和发布流程")
  76. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  77. @RequestMapping(value = "/save", method = RequestMethod.POST)
  78. @Transactional
  79. public Result save(@Valid @RequestBody CustomFlowSaveDto customFlowSaveDto, BindingResult bindingResult) throws NoSuchAlgorithmException {
  80. if (bindingResult.hasErrors()) {
  81. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  82. }
  83. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  84. Long schoolId = Objects.isNull(sysUser.getSchoolId()) ? SystemConstant.getHeadOrUserSchoolId() : sysUser.getSchoolId();
  85. customFlowSaveDto.setSchoolAndOrgInfo(schoolId, sysUser.getOrgId());
  86. String flowBpmnId = MD5Util.encoder(customFlowSaveDto.toString());
  87. BasicSchool basicSchool = commonCacheService.schoolCache(customFlowSaveDto.getSchoolId());
  88. flowBpmnId = basicSchool.getCode() + "_" + flowBpmnId;
  89. boolean lock = redisUtil.lock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId, SystemConstant.REDIS_LOCK_CUSTOM_FLOW_TIME_OUT);
  90. if (!lock) {
  91. throw ExceptionResultEnum.ERROR.exception("正在发布中,请稍候再试!");
  92. }
  93. try {
  94. TFCustomFlow dbTfCustomFlow = tfCustomFlowService.findMaxVersion(customFlowSaveDto.getSchoolId(), null, customFlowSaveDto.getType());
  95. TFCustomFlow tfCustomFlow = null;
  96. if (Objects.isNull(customFlowSaveDto.getCustomFlowId())) {
  97. tfCustomFlow = new TFCustomFlow(customFlowSaveDto.getSchoolId(), customFlowSaveDto.getOrgId(), customFlowSaveDto.getName(), customFlowSaveDto.getType(), customFlowSaveDto.getPublish(), JacksonUtil.parseJson(customFlowSaveDto.getCustomFlowLists()), sysUser.getId(), flowBpmnId);
  98. } else {
  99. tfCustomFlow = tfCustomFlowService.getById(customFlowSaveDto.getCustomFlowId());
  100. Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
  101. if (!tfCustomFlow.getEnable()) {
  102. throw ExceptionResultEnum.ERROR.exception("自定义流程数据已删除");
  103. }
  104. tfCustomFlow.setObjectData(JacksonUtil.parseJson(customFlowSaveDto.getCustomFlowLists()));
  105. tfCustomFlow.updateInfo(sysUser.getId());
  106. }
  107. AtomicInteger atomicInteger = null;
  108. if (Objects.isNull(dbTfCustomFlow)) {//新增
  109. atomicInteger = new AtomicInteger(1);
  110. tfCustomFlow.setVersion(atomicInteger.get());
  111. } else {//版本号递增
  112. atomicInteger = new AtomicInteger(dbTfCustomFlow.getVersion());
  113. tfCustomFlow.setVersion(atomicInteger.incrementAndGet());
  114. }
  115. //自定义流程处理开始
  116. Map<String, Object> map = activitiService.dynamicBuildBpmn(customFlowSaveDto, flowBpmnId, tfCustomFlow.getVersion());
  117. String actFlowId = tfCustomFlowService.findActIdByFlowKey(flowBpmnId);
  118. tfCustomFlow.setActFlowId(actFlowId);
  119. tfCustomFlow.setFlowProcessVar(JacksonUtil.parseJson(map));
  120. tfCustomFlowService.saveOrUpdate(tfCustomFlow);
  121. } catch (Exception e) {
  122. log.error(SystemConstant.LOG_ERROR, e);
  123. if (e instanceof DuplicateKeyException) {
  124. String errorColumn = e.getCause().toString();
  125. String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
  126. throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
  127. } else if (e instanceof ApiException) {
  128. ResultUtil.error((ApiException) e, e.getMessage());
  129. } else {
  130. ResultUtil.error(e.getMessage());
  131. }
  132. } finally {
  133. redisUtil.releaseLock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId);
  134. }
  135. return ResultUtil.ok(true);
  136. }
  137. @ApiOperation(value = "审批流程")
  138. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  139. @RequestMapping(value = "/task/approve", method = RequestMethod.POST)
  140. public Result taskApprove(@Valid @RequestBody FlowTaskApproveParam flowTaskApproveParam, BindingResult bindingResult) {
  141. if (bindingResult.hasErrors()) {
  142. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  143. }
  144. Map<String, Object> map = new HashMap<>();
  145. map.computeIfAbsent(SystemConstant.FLOW_TASK_ID, v -> flowTaskApproveParam.getTaskId());
  146. map.computeIfAbsent(SystemConstant.APPROVE_OPERATION, v -> flowTaskApproveParam.getApprovePass());
  147. map.computeIfAbsent(SystemConstant.APPROVE_REMARK, v -> flowTaskApproveParam.getRemark());
  148. map.computeIfAbsent(SystemConstant.APPROVE_SETUP, v -> flowTaskApproveParam.getSetup());
  149. activitiService.taskApprove(map);
  150. return ResultUtil.ok(true);
  151. }
  152. @ApiOperation(value = "流程审批记录列表")
  153. @ApiResponses({@ApiResponse(code = 200, message = "流程审批记录信息", response = FlowApproveListResult.class)})
  154. @RequestMapping(value = "/approve/list", method = RequestMethod.POST)
  155. public Result taskApproveList(@ApiParam(value = "状态", required = false) @RequestParam(required = false) FlowStatusEnum status,
  156. @ApiParam(value = "提交人名称", required = false) @RequestParam(required = false) String teacherUserName,
  157. @ApiParam(value = "教研室", required = false) @RequestParam(required = false) String teachingRoomId,
  158. @ApiParam(value = "提交开始时间", required = false) @RequestParam(required = false) Long startTime,
  159. @ApiParam(value = "提交结束时间", required = false) @RequestParam(required = false) Long endTime,
  160. @ApiParam(value = "提交人名称", required = false) @RequestParam(required = false) String pendApproveUserName,
  161. @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
  162. @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
  163. return ResultUtil.ok(tfFlowApproveService.findApproveList(new Page<>(pageNumber, pageSize), status, teacherUserName, SystemConstant.convertIdToLong(teachingRoomId), startTime, endTime, pendApproveUserName, SystemConstant.getHeadOrUserSchoolId(), null));
  164. }
  165. @ApiOperation(value = "流程列表")
  166. @ApiResponses({@ApiResponse(code = 200, message = "流程信息", response = TFCustomFlow.class)})
  167. @RequestMapping(value = "/list", method = RequestMethod.POST)
  168. public Result list(@ApiParam(value = "流程名称", required = false) @RequestParam(required = false) String name,
  169. @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
  170. @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
  171. return ResultUtil.ok(tfCustomFlowService.list(new Page<>(pageNumber, pageSize), name, SystemConstant.getHeadOrUserSchoolId(), null));
  172. }
  173. @ApiOperation(value = "流程编辑")
  174. @ApiResponses({@ApiResponse(code = 200, message = "流程信息", response = ResultUtil.class)})
  175. @RequestMapping(value = "/edit", method = RequestMethod.POST)
  176. public Result edit(@ApiParam(value = "自定义流程id", required = true) @RequestParam String id) {
  177. TFCustomFlow tfCustomFlow = tfCustomFlowService.getById(SystemConstant.convertIdToLong(id));
  178. Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
  179. Optional.ofNullable(tfCustomFlow.getObjectData()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程绘图数据为空"));
  180. Gson gson = new Gson();
  181. List<CustomFlowDto> customFlowLists = gson.fromJson(tfCustomFlow.getObjectData(), new TypeToken<List<CustomFlowDto>>() {
  182. }.getType());
  183. return ResultUtil.ok(new CustomFlowSaveDto(tfCustomFlow, customFlowLists), null);
  184. }
  185. @ApiOperation(value = "流程逻辑删除")
  186. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  187. @RequestMapping(value = "/enable", method = RequestMethod.POST)
  188. public Result enable(@Valid @RequestBody CustomFlowParam customFlowParam, BindingResult bindingResult) {
  189. if (bindingResult.hasErrors()) {
  190. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  191. }
  192. TFCustomFlow tfCustomFlow = tfCustomFlowService.getById(customFlowParam.getId());
  193. Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
  194. if (!customFlowParam.getEnable()) {
  195. List<TFCustomFlowEntity> tfCustomFlowEntityList = tfCustomFlowEntityService.findListByCustomFlowId(customFlowParam.getId());
  196. if (Objects.nonNull(tfCustomFlowEntityList) && tfCustomFlowEntityList.size() > 0) {
  197. throw ExceptionResultEnum.ERROR.exception("已存在流程数据,不能删除");
  198. }
  199. }
  200. tfCustomFlow.setEnable(customFlowParam.getEnable());
  201. return ResultUtil.ok(tfCustomFlowService.updateById(tfCustomFlow));
  202. }
  203. @ApiOperation(value = "查看流程信息")
  204. @ApiResponses({@ApiResponse(code = 200, message = "审批流程信息", response = FlowViewResult.class)})
  205. @RequestMapping(value = "/view", method = RequestMethod.POST)
  206. public Result view(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
  207. return ResultUtil.ok(activitiService.getFlowView(SystemConstant.convertIdToLong(flowId)));
  208. }
  209. @ApiOperation(value = "流程审批记录逻辑删除")
  210. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  211. @RequestMapping(value = "/approve/enable", method = RequestMethod.POST)
  212. public Result approveEnable(@Valid @RequestBody CustomFlowParam customFlowParam, BindingResult bindingResult) {
  213. if (bindingResult.hasErrors()) {
  214. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  215. }
  216. TFFlowApprove tfFlowApprove = tfFlowApproveService.getById(customFlowParam.getId());
  217. Optional.ofNullable(tfFlowApprove).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("流程审批记录数据为空"));
  218. tfFlowApprove.setEnable(customFlowParam.getEnable());
  219. return ResultUtil.ok(tfFlowApproveService.updateById(tfFlowApprove));
  220. }
  221. @ApiOperation(value = "流程终止")
  222. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  223. @RequestMapping(value = "/end", method = RequestMethod.POST)
  224. public Result end(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
  225. activitiService.flowEnd(flowId);
  226. return ResultUtil.ok();
  227. }
  228. @ApiOperation(value = "获取所有流程节点")
  229. @ApiResponses({@ApiResponse(code = 200, message = "流程节点", response = FlowTaskResult.class)})
  230. @RequestMapping(value = "/task/all", method = RequestMethod.POST)
  231. public Result taskAll(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
  232. return ResultUtil.ok(activitiService.getTaskAll(flowId));
  233. }
  234. @ApiOperation(value = "流程节点转他人审批")
  235. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  236. @RequestMapping(value = "/task/approver/exchange", method = RequestMethod.POST)
  237. public Result taskApproverExchange(@ApiParam(value = "审批人id", required = true) @RequestParam String userId,
  238. @ApiParam(value = "流程节点id", required = true) @RequestParam String taskId) {
  239. return ResultUtil.ok(activitiService.taskApproverExchange(userId, taskId));
  240. }
  241. @ApiOperation(value = "获取当前流程节点信息")
  242. @ApiResponses({@ApiResponse(code = 200, message = "当前流程节点信息", response = TaskInfoResult.class)})
  243. @RequestMapping(value = "/task/info", method = RequestMethod.POST)
  244. public Result taskInfo(@ApiParam(value = "流程节点id", required = true) @RequestParam String taskId) {
  245. return ResultUtil.ok(activitiService.getTaskInfo(SystemConstant.convertIdToLong(taskId)));
  246. }
  247. @ApiOperation(value = "重命名自定义流程名称")
  248. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  249. @RequestMapping(value = "/rename", method = RequestMethod.POST)
  250. public Result rename(@Valid @RequestBody CustomFlowRenameParam customFlowRenameParam, BindingResult bindingResult) {
  251. if (bindingResult.hasErrors()) {
  252. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  253. }
  254. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  255. UpdateWrapper<TFCustomFlow> tfCustomFlowUpdateWrapper = new UpdateWrapper<>();
  256. tfCustomFlowUpdateWrapper.lambda().eq(TFCustomFlow::getId, customFlowRenameParam.getId())
  257. .set(TFCustomFlow::getName, customFlowRenameParam.getName())
  258. .set(TFCustomFlow::getUpdateId, sysUser.getId())
  259. .set(TFCustomFlow::getUpdateTime, System.currentTimeMillis());
  260. return ResultUtil.ok(tfCustomFlowService.update(tfCustomFlowUpdateWrapper));
  261. }
  262. @ApiOperation(value = "根据流程类型获取流程节点")
  263. @ApiResponses({@ApiResponse(code = 200, message = "当前流程节点信息", response = FlowTaskResult.class)})
  264. @RequestMapping(value = "/get_flow_info_by_type", method = RequestMethod.POST)
  265. public Result getFlowInfoByType(@ApiParam(value = "流程类型", required = true) @RequestParam TFCustomTypeEnum type) {
  266. return ResultUtil.ok(activitiService.getFlowInfoByType(type));
  267. }
  268. }