TFCustomFlowController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.CustomFlowCommonDto;
  10. import com.qmth.distributed.print.business.bean.flow.CustomFlowEditDto;
  11. import com.qmth.distributed.print.business.bean.flow.CustomFlowSaveDto;
  12. import com.qmth.distributed.print.business.bean.params.CustomFlowParam;
  13. import com.qmth.distributed.print.business.bean.params.CustomFlowRenameParam;
  14. import com.qmth.distributed.print.business.bean.params.FlowTaskApproveParam;
  15. import com.qmth.distributed.print.business.bean.result.*;
  16. import com.qmth.distributed.print.business.entity.TFCustomFlow;
  17. import com.qmth.distributed.print.business.entity.TFCustomFlowEntity;
  18. import com.qmth.distributed.print.business.entity.TFFlowApprove;
  19. import com.qmth.distributed.print.business.service.ActivitiService;
  20. import com.qmth.distributed.print.business.service.TFCustomFlowEntityService;
  21. import com.qmth.distributed.print.business.service.TFCustomFlowService;
  22. import com.qmth.distributed.print.business.service.TFFlowApproveService;
  23. import com.qmth.teachcloud.common.contant.SystemConstant;
  24. import com.qmth.teachcloud.common.entity.BasicSchool;
  25. import com.qmth.teachcloud.common.entity.SysUser;
  26. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  27. import com.qmth.teachcloud.common.enums.FieldUniqueEnum;
  28. import com.qmth.teachcloud.common.enums.FlowStatusEnum;
  29. import com.qmth.teachcloud.common.enums.TFCustomTypeEnum;
  30. import com.qmth.teachcloud.common.service.CommonCacheService;
  31. import com.qmth.teachcloud.common.util.*;
  32. import io.swagger.annotations.*;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import org.springframework.dao.DuplicateKeyException;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import org.springframework.validation.BindingResult;
  38. import org.springframework.validation.annotation.Validated;
  39. import org.springframework.web.bind.annotation.*;
  40. import javax.annotation.Resource;
  41. import javax.validation.Valid;
  42. import javax.validation.constraints.Max;
  43. import javax.validation.constraints.Min;
  44. import java.security.NoSuchAlgorithmException;
  45. import java.util.*;
  46. import java.util.concurrent.atomic.AtomicInteger;
  47. import java.util.stream.Collectors;
  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, customFlowSaveDto.getModelType());
  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.setInfo(customFlowSaveDto);
  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 = "/testStart", method = RequestMethod.POST)
  140. // @Transactional
  141. // public Result testStart(@ApiParam(value = "命题任务id", required = true) @RequestParam String taskId,
  142. // @ApiParam(value = "命题老师id", required = true) @RequestParam String approveId) {
  143. // Map<String, Object> map = new HashMap<>();
  144. // map.computeIfAbsent(SystemConstant.OBJECT_ID, v -> SystemConstant.convertIdToLong(taskId));
  145. // map.computeIfAbsent(SystemConstant.APPROVE_ID, v -> SystemConstant.convertIdToLong(approveId));
  146. // map.computeIfAbsent(SystemConstant.FLOW_TYPE, v -> TFCustomTypeEnum.ELECTRON_FLOW);
  147. // activitiService.customFlowStart(map);
  148. // return ResultUtil.ok(true);
  149. // }
  150. @ApiOperation(value = "审批流程")
  151. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  152. @RequestMapping(value = "/task/approve", method = RequestMethod.POST)
  153. public Result taskApprove(@Valid @RequestBody FlowTaskApproveParam flowTaskApproveParam, BindingResult bindingResult) {
  154. if (bindingResult.hasErrors()) {
  155. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  156. }
  157. Map<String, Object> map = new HashMap<>();
  158. map.computeIfAbsent(SystemConstant.FLOW_TASK_ID, v -> flowTaskApproveParam.getTaskId());
  159. map.computeIfAbsent(SystemConstant.APPROVE_OPERATION, v -> flowTaskApproveParam.getApprovePass());
  160. map.computeIfAbsent(SystemConstant.APPROVE_REMARK, v -> flowTaskApproveParam.getRemark());
  161. map.computeIfAbsent(SystemConstant.APPROVE_SETUP, v -> flowTaskApproveParam.getSetup());
  162. map.computeIfAbsent(SystemConstant.APPROVE_USER_IDS, v -> flowTaskApproveParam.getApproveUserIds());
  163. activitiService.taskApprove(map);
  164. return ResultUtil.ok(true);
  165. }
  166. @ApiOperation(value = "流程审批记录列表")
  167. @ApiResponses({@ApiResponse(code = 200, message = "流程审批记录信息", response = FlowApproveListResult.class)})
  168. @RequestMapping(value = "/approve/list", method = RequestMethod.POST)
  169. public Result taskApproveList(@ApiParam(value = "状态", required = false) @RequestParam(required = false) FlowStatusEnum status,
  170. @ApiParam(value = "提交人名称", required = false) @RequestParam(required = false) String teacherUserName,
  171. @ApiParam(value = "教研室", required = false) @RequestParam(required = false) String teachingRoomId,
  172. @ApiParam(value = "提交开始时间", required = false) @RequestParam(required = false) Long startTime,
  173. @ApiParam(value = "提交结束时间", required = false) @RequestParam(required = false) Long endTime,
  174. @ApiParam(value = "提交人名称", required = false) @RequestParam(required = false) String pendApproveUserName,
  175. @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
  176. @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
  177. return ResultUtil.ok(tfFlowApproveService.findApproveList(new Page<>(pageNumber, pageSize), status, teacherUserName, SystemConstant.convertIdToLong(teachingRoomId), startTime, endTime, pendApproveUserName, SystemConstant.getHeadOrUserSchoolId(), null));
  178. }
  179. @ApiOperation(value = "流程列表")
  180. @ApiResponses({@ApiResponse(code = 200, message = "流程信息", response = TFCustomFlow.class)})
  181. @RequestMapping(value = "/list", method = RequestMethod.POST)
  182. public Result list(@ApiParam(value = "流程名称", required = false) @RequestParam(required = false) String name,
  183. @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
  184. @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
  185. return ResultUtil.ok(tfCustomFlowService.list(new Page<>(pageNumber, pageSize), name, SystemConstant.getHeadOrUserSchoolId(), null));
  186. }
  187. @ApiOperation(value = "流程编辑")
  188. @ApiResponses({@ApiResponse(code = 200, message = "流程信息", response = ResultUtil.class)})
  189. @RequestMapping(value = "/edit", method = RequestMethod.POST)
  190. public Result edit(@ApiParam(value = "自定义流程id", required = true) @RequestParam String id) {
  191. TFCustomFlow tfCustomFlow = tfCustomFlowService.getById(SystemConstant.convertIdToLong(id));
  192. Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
  193. Optional.ofNullable(tfCustomFlow.getObjectData()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程绘图数据为空"));
  194. Gson gson = new Gson();
  195. List<CustomFlowCommonDto> customFlowLists = gson.fromJson(tfCustomFlow.getObjectData(), new TypeToken<List<CustomFlowCommonDto>>() {
  196. }.getType());
  197. return ResultUtil.ok(new CustomFlowEditDto(tfCustomFlow, customFlowLists), null);
  198. }
  199. @ApiOperation(value = "流程逻辑删除")
  200. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  201. @RequestMapping(value = "/enable", method = RequestMethod.POST)
  202. public Result enable(@Valid @RequestBody CustomFlowParam customFlowParam, BindingResult bindingResult) {
  203. if (bindingResult.hasErrors()) {
  204. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  205. }
  206. TFCustomFlow tfCustomFlow = tfCustomFlowService.getById(customFlowParam.getId());
  207. Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
  208. if (!customFlowParam.getEnable()) {
  209. List<TFCustomFlowEntity> tfCustomFlowEntityList = tfCustomFlowEntityService.findListByCustomFlowId(customFlowParam.getId());
  210. if (Objects.nonNull(tfCustomFlowEntityList) && tfCustomFlowEntityList.size() > 0) {
  211. List<Long> flowIds = tfCustomFlowEntityList.stream().map(e -> e.getFlowId()).collect(Collectors.toList());
  212. int min = 0;
  213. int max = SystemConstant.IN_SIZE_MAX, size = flowIds.size();
  214. if (max >= size) {
  215. max = size;
  216. }
  217. while (max <= size) {
  218. QueryWrapper<TFFlowApprove> tfFlowApproveQueryWrapper = new QueryWrapper<>();
  219. tfFlowApproveQueryWrapper.lambda().ne(TFFlowApprove::getStatus, FlowStatusEnum.FINISH)
  220. .ne(TFFlowApprove::getStatus, FlowStatusEnum.END)
  221. .in(TFFlowApprove::getFlowId, flowIds.subList(min, max));
  222. int count = tfFlowApproveService.count(tfFlowApproveQueryWrapper);
  223. if (count > 0) {
  224. throw ExceptionResultEnum.ERROR.exception("已存在流程数据,不能删除");
  225. }
  226. if (max == size) {
  227. break;
  228. }
  229. min = max;
  230. max += SystemConstant.IN_SIZE_MAX;
  231. if (max >= size) {
  232. max = size;
  233. }
  234. }
  235. }
  236. }
  237. tfCustomFlow.setEnable(!customFlowParam.getEnable() ? null : customFlowParam.getEnable());
  238. return ResultUtil.ok(tfCustomFlowService.updateById(tfCustomFlow));
  239. }
  240. @ApiOperation(value = "查看流程信息")
  241. @ApiResponses({@ApiResponse(code = 200, message = "审批流程信息", response = FlowViewResult.class)})
  242. @RequestMapping(value = "/view", method = RequestMethod.POST)
  243. public Result view(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
  244. return ResultUtil.ok(activitiService.getFlowView(SystemConstant.convertIdToLong(flowId)));
  245. }
  246. @ApiOperation(value = "流程审批记录逻辑删除")
  247. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  248. @RequestMapping(value = "/approve/enable", method = RequestMethod.POST)
  249. public Result approveEnable(@Valid @RequestBody CustomFlowParam customFlowParam, BindingResult bindingResult) {
  250. if (bindingResult.hasErrors()) {
  251. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  252. }
  253. TFFlowApprove tfFlowApprove = tfFlowApproveService.getById(customFlowParam.getId());
  254. Optional.ofNullable(tfFlowApprove).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("流程审批记录数据为空"));
  255. tfFlowApprove.setEnable(customFlowParam.getEnable());
  256. return ResultUtil.ok(tfFlowApproveService.updateById(tfFlowApprove));
  257. }
  258. @ApiOperation(value = "流程终止")
  259. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  260. @RequestMapping(value = "/end", method = RequestMethod.POST)
  261. public Result end(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
  262. activitiService.flowEnd(flowId);
  263. return ResultUtil.ok();
  264. }
  265. @ApiOperation(value = "获取所有流程节点")
  266. @ApiResponses({@ApiResponse(code = 200, message = "流程节点", response = FlowTaskResult.class)})
  267. @RequestMapping(value = "/task/all", method = RequestMethod.POST)
  268. public Result taskAll(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
  269. return ResultUtil.ok(activitiService.getTaskAll(flowId));
  270. }
  271. @ApiOperation(value = "流程节点转他人审批")
  272. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  273. @RequestMapping(value = "/task/approver/exchange", method = RequestMethod.POST)
  274. public Result taskApproverExchange(@ApiParam(value = "审批人id", required = true) @RequestParam String userId,
  275. @ApiParam(value = "流程节点id", required = true) @RequestParam String taskId) {
  276. return ResultUtil.ok(activitiService.taskApproverExchange(userId, taskId));
  277. }
  278. @ApiOperation(value = "获取当前流程节点信息")
  279. @ApiResponses({@ApiResponse(code = 200, message = "当前流程节点信息", response = TaskInfoResult.class)})
  280. @RequestMapping(value = "/task/info", method = RequestMethod.POST)
  281. public Result taskInfo(@ApiParam(value = "流程节点id", required = true) @RequestParam String taskId) {
  282. return ResultUtil.ok(activitiService.getTaskInfo(SystemConstant.convertIdToLong(taskId)));
  283. }
  284. @ApiOperation(value = "重命名自定义流程名称")
  285. @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
  286. @RequestMapping(value = "/rename", method = RequestMethod.POST)
  287. public Result rename(@Valid @RequestBody CustomFlowRenameParam customFlowRenameParam, BindingResult bindingResult) {
  288. if (bindingResult.hasErrors()) {
  289. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  290. }
  291. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  292. UpdateWrapper<TFCustomFlow> tfCustomFlowUpdateWrapper = new UpdateWrapper<>();
  293. tfCustomFlowUpdateWrapper.lambda().eq(TFCustomFlow::getId, customFlowRenameParam.getId())
  294. .set(TFCustomFlow::getName, customFlowRenameParam.getName())
  295. .set(TFCustomFlow::getUpdateId, sysUser.getId())
  296. .set(TFCustomFlow::getUpdateTime, System.currentTimeMillis());
  297. return ResultUtil.ok(tfCustomFlowService.update(tfCustomFlowUpdateWrapper));
  298. }
  299. @ApiOperation(value = "根据流程类型获取流程节点")
  300. @ApiResponses({@ApiResponse(code = 200, message = "当前流程节点信息", response = FlowInfoResult.class)})
  301. @RequestMapping(value = "/get_flow_info_by_type", method = RequestMethod.POST)
  302. public Result getFlowInfoByType(@ApiParam(value = "流程类型", required = false) @RequestParam(required = false) TFCustomTypeEnum type,
  303. @ApiParam(value = "流程id", required = false) @RequestParam(required = false) String flowId) {
  304. if (Objects.isNull(type) && Objects.isNull(flowId)) {
  305. throw ExceptionResultEnum.ERROR.exception("流程类型或流程id必须输入一项");
  306. }
  307. return ResultUtil.ok(activitiService.getFlowInfoByType(type, Objects.nonNull(flowId) ? SystemConstant.convertIdToLong(flowId) : null));
  308. }
  309. }