|
@@ -384,6 +384,96 @@ public class FlowTaskServiceImpl extends FlowServiceFactory implements IFlowTask
|
384
|
384
|
}
|
385
|
385
|
|
386
|
386
|
|
|
387
|
+ /**
|
|
388
|
+ * 重新提交任务
|
|
389
|
+ *
|
|
390
|
+ * @param flowTaskVo 请求实体参数
|
|
391
|
+ */
|
|
392
|
+ @Transactional(rollbackFor = Exception.class)
|
|
393
|
+ @Override
|
|
394
|
+ public void taskRecommit(FlowTaskVo flowTaskVo) {
|
|
395
|
+ if (taskService.createTaskQuery().taskId(flowTaskVo.getTaskId()).singleResult().isSuspended()) {
|
|
396
|
+ throw new CustomException("任务处于挂起状态");
|
|
397
|
+ }
|
|
398
|
+ // 当前任务 task
|
|
399
|
+ Task task = taskService.createTaskQuery().taskId(flowTaskVo.getTaskId()).singleResult();
|
|
400
|
+ // 获取流程定义信息
|
|
401
|
+ ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
|
|
402
|
+ // 获取所有节点信息
|
|
403
|
+ Process process = repositoryService.getBpmnModel(processDefinition.getId()).getProcesses().get(0);
|
|
404
|
+ // 获取全部节点列表,包含子节点
|
|
405
|
+ Collection<FlowElement> allElements = FlowableUtils.getAllElements(process.getFlowElements(), null);
|
|
406
|
+ // 获取当前任务节点元素
|
|
407
|
+ FlowElement source = null;
|
|
408
|
+ // 获取跳转的节点元素
|
|
409
|
+ FlowElement target = null;
|
|
410
|
+ if (allElements != null) {
|
|
411
|
+ for (FlowElement flowElement : allElements) {
|
|
412
|
+ // 当前任务节点元素
|
|
413
|
+ if (flowElement.getId().equals(task.getTaskDefinitionKey())) {
|
|
414
|
+ source = flowElement;
|
|
415
|
+ }
|
|
416
|
+ // 跳转的节点元素
|
|
417
|
+ if (flowElement.getId().equals(flowTaskVo.getTargetKey())) {
|
|
418
|
+ target = flowElement;
|
|
419
|
+ }
|
|
420
|
+ }
|
|
421
|
+ }
|
|
422
|
+
|
|
423
|
+ // 从当前节点向前扫描
|
|
424
|
+ // 如果存在路线上不存在目标节点,说明目标节点是在网关上或非同一路线上,不可跳转
|
|
425
|
+ // 否则目标节点相对于当前节点,属于串行
|
|
426
|
+// Boolean isSequential = FlowableUtils.iteratorCheckSequentialReferTarget(source, flowTaskVo.getTargetKey(), null, null);
|
|
427
|
+// if (!isSequential) {
|
|
428
|
+// throw new CustomException("当前节点相对于目标节点,不属于串行关系,无法回退");
|
|
429
|
+// }
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+ // 获取所有正常进行的任务节点 Key,这些任务不能直接使用,需要找出其中需要撤回的任务
|
|
433
|
+ List<Task> runTaskList = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).list();
|
|
434
|
+ List<String> runTaskKeyList = new ArrayList<>();
|
|
435
|
+ runTaskList.forEach(item -> runTaskKeyList.add(item.getTaskDefinitionKey()));
|
|
436
|
+ // 需退回任务列表
|
|
437
|
+ List<String> currentIds = new ArrayList<>();
|
|
438
|
+ currentIds.add(source.getId());
|
|
439
|
+ // 通过父级网关的出口连线,结合 runTaskList 比对,获取需要撤回的任务
|
|
440
|
+// List<UserTask> currentUserTaskList = FlowableUtils.iteratorFindChildUserTasks(target, runTaskKeyList, null, null);
|
|
441
|
+// currentUserTaskList.forEach(item -> currentIds.add(item.getId()));
|
|
442
|
+
|
|
443
|
+ // 循环获取那些需要被撤回的节点的ID,用来设置驳回原因
|
|
444
|
+ List<String> currentTaskIds = new ArrayList<>();
|
|
445
|
+ currentIds.forEach(currentId -> runTaskList.forEach(runTask -> {
|
|
446
|
+ if (currentId.equals(runTask.getTaskDefinitionKey())) {
|
|
447
|
+ currentTaskIds.add(runTask.getId());
|
|
448
|
+ }
|
|
449
|
+ }));
|
|
450
|
+ // 设置回退意见
|
|
451
|
+ currentTaskIds.forEach(currentTaskId -> taskService.addComment(currentTaskId, task.getProcessInstanceId(), FlowComment.NORMAL.getRemark(), flowTaskVo.getComment()));
|
|
452
|
+
|
|
453
|
+ try {
|
|
454
|
+ // 1 对 1 或 多 对 1 情况,currentIds 当前要跳转的节点列表(1或多),targetKey 跳转到的节点(1)
|
|
455
|
+ runtimeService.createChangeActivityStateBuilder()
|
|
456
|
+ .processInstanceId(task.getProcessInstanceId())
|
|
457
|
+ .moveActivityIdsToSingleActivityId(currentIds, flowTaskVo.getTargetKey()).changeState();
|
|
458
|
+ String currentTaskId = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).singleResult().getId();
|
|
459
|
+ List<HistoricTaskInstance> htiList = historyService.createHistoricTaskInstanceQuery()
|
|
460
|
+ .processInstanceId(task.getProcessInstanceId()).taskDefinitionKey(flowTaskVo.getTargetKey())
|
|
461
|
+ .list();
|
|
462
|
+ String taskAssignee = "";
|
|
463
|
+ for (HistoricTaskInstance hti: htiList) {
|
|
464
|
+ if (!hti.getId().equals(currentTaskId)) {
|
|
465
|
+ taskAssignee = hti.getAssignee();
|
|
466
|
+ break;
|
|
467
|
+ }
|
|
468
|
+ }
|
|
469
|
+ taskService.setAssignee(currentTaskId, taskAssignee);
|
|
470
|
+ } catch (FlowableObjectNotFoundException e) {
|
|
471
|
+ throw new CustomException("未找到流程实例,流程可能已发生变化");
|
|
472
|
+ } catch (FlowableException e) {
|
|
473
|
+ throw new CustomException("无法取消或开始活动");
|
|
474
|
+ }
|
|
475
|
+ }
|
|
476
|
+
|
387
|
477
|
/**
|
388
|
478
|
* 获取所有可回退的节点
|
389
|
479
|
*
|