TFCustomFlowController.java 19 KB

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