Commit 70386263e4775ef21d6b11a0f9e661202f630502
1 parent
764a242d
【功能修复】工作流:自动审批通过时,不能使用 TransactionSynchronizationManager 的 afterCommit 的情况
Showing
3 changed files
with
52 additions
and
47 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmUserTaskActivityBehavior.java
| 1 | package cn.iocoder.yudao.module.bpm.framework.flowable.core.behavior; | 1 | package cn.iocoder.yudao.module.bpm.framework.flowable.core.behavior; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | -import cn.hutool.core.util.ObjectUtil; | ||
| 5 | import cn.hutool.core.util.RandomUtil; | 4 | import cn.hutool.core.util.RandomUtil; |
| 6 | -import cn.hutool.extra.spring.SpringUtil; | ||
| 7 | -import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskApproveReqVO; | ||
| 8 | -import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskRejectReqVO; | ||
| 9 | -import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskApproveTypeEnum; | ||
| 10 | -import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskAssignEmptyHandlerTypeEnum; | ||
| 11 | -import cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum; | ||
| 12 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker; | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker; |
| 13 | -import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; | ||
| 14 | -import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService; | ||
| 15 | import lombok.Setter; | 6 | import lombok.Setter; |
| 16 | import lombok.extern.slf4j.Slf4j; | 7 | import lombok.extern.slf4j.Slf4j; |
| 17 | import org.flowable.bpmn.model.UserTask; | 8 | import org.flowable.bpmn.model.UserTask; |
| @@ -22,8 +13,7 @@ import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; | @@ -22,8 +13,7 @@ import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; | ||
| 22 | import org.flowable.engine.impl.util.TaskHelper; | 13 | import org.flowable.engine.impl.util.TaskHelper; |
| 23 | import org.flowable.task.service.TaskService; | 14 | import org.flowable.task.service.TaskService; |
| 24 | import org.flowable.task.service.impl.persistence.entity.TaskEntity; | 15 | import org.flowable.task.service.impl.persistence.entity.TaskEntity; |
| 25 | -import org.springframework.transaction.support.TransactionSynchronization; | ||
| 26 | -import org.springframework.transaction.support.TransactionSynchronizationManager; | 16 | +import org.springframework.transaction.annotation.Transactional; |
| 27 | 17 | ||
| 28 | import java.util.List; | 18 | import java.util.List; |
| 29 | import java.util.Set; | 19 | import java.util.Set; |
| @@ -46,6 +36,7 @@ public class BpmUserTaskActivityBehavior extends UserTaskActivityBehavior { | @@ -46,6 +36,7 @@ public class BpmUserTaskActivityBehavior extends UserTaskActivityBehavior { | ||
| 46 | } | 36 | } |
| 47 | 37 | ||
| 48 | @Override | 38 | @Override |
| 39 | + @Transactional(rollbackFor = Exception.class) | ||
| 49 | protected void handleAssignments(TaskService taskService, String assignee, String owner, | 40 | protected void handleAssignments(TaskService taskService, String assignee, String owner, |
| 50 | List<String> candidateUsers, List<String> candidateGroups, TaskEntity task, ExpressionManager expressionManager, | 41 | List<String> candidateUsers, List<String> candidateGroups, TaskEntity task, ExpressionManager expressionManager, |
| 51 | DelegateExecution execution, ProcessEngineConfigurationImpl processEngineConfiguration) { | 42 | DelegateExecution execution, ProcessEngineConfigurationImpl processEngineConfiguration) { |
| @@ -54,39 +45,7 @@ public class BpmUserTaskActivityBehavior extends UserTaskActivityBehavior { | @@ -54,39 +45,7 @@ public class BpmUserTaskActivityBehavior extends UserTaskActivityBehavior { | ||
| 54 | // 第二步,设置作为负责人 | 45 | // 第二步,设置作为负责人 |
| 55 | if (assigneeUserId != null) { | 46 | if (assigneeUserId != null) { |
| 56 | TaskHelper.changeTaskAssignee(task, String.valueOf(assigneeUserId)); | 47 | TaskHelper.changeTaskAssignee(task, String.valueOf(assigneeUserId)); |
| 57 | - return; | ||
| 58 | } | 48 | } |
| 59 | - | ||
| 60 | - // 特殊:处理需要自动通过、不通过的情况 | ||
| 61 | - Integer approveType = BpmnModelUtils.parseApproveType(userTask); | ||
| 62 | - Integer assignEmptyHandlerType = BpmnModelUtils.parseAssignEmptyHandlerType(userTask); | ||
| 63 | - TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { | ||
| 64 | - | ||
| 65 | - @Override | ||
| 66 | - public void afterCommit() { | ||
| 67 | - // 特殊情况一:【人工审核】审批人为空,根据配置是否要自动通过、自动拒绝 | ||
| 68 | - if (ObjectUtil.equal(approveType, BpmUserTaskApproveTypeEnum.USER.getType())) { | ||
| 69 | - if (ObjectUtil.equal(assignEmptyHandlerType, BpmUserTaskAssignEmptyHandlerTypeEnum.APPROVE.getType())) { | ||
| 70 | - SpringUtil.getBean(BpmTaskService.class).approveTask(null, new BpmTaskApproveReqVO() | ||
| 71 | - .setId(task.getId()).setReason(BpmReasonEnum.ASSIGN_EMPTY_APPROVE.getReason())); | ||
| 72 | - } else if (ObjectUtil.equal(assignEmptyHandlerType, BpmUserTaskAssignEmptyHandlerTypeEnum.REJECT.getType())) { | ||
| 73 | - SpringUtil.getBean(BpmTaskService.class).rejectTask(null, new BpmTaskRejectReqVO() | ||
| 74 | - .setId(task.getId()).setReason(BpmReasonEnum.ASSIGN_EMPTY_REJECT.getReason())); | ||
| 75 | - } | ||
| 76 | - // 特殊情况二:【自动审核】审批类型为自动通过、不通过 | ||
| 77 | - } else { | ||
| 78 | - if (ObjectUtil.equal(approveType, BpmUserTaskApproveTypeEnum.AUTO_APPROVE.getType())) { | ||
| 79 | - SpringUtil.getBean(BpmTaskService.class).approveTask(null, new BpmTaskApproveReqVO() | ||
| 80 | - .setId(task.getId()).setReason(BpmReasonEnum.APPROVE_TYPE_AUTO_APPROVE.getReason())); | ||
| 81 | - } else if (ObjectUtil.equal(approveType, BpmUserTaskApproveTypeEnum.AUTO_REJECT.getType())) { | ||
| 82 | - SpringUtil.getBean(BpmTaskService.class).rejectTask(null, new BpmTaskRejectReqVO() | ||
| 83 | - .setId(task.getId()).setReason(BpmReasonEnum.APPROVE_TYPE_AUTO_REJECT.getReason())); | ||
| 84 | - } | ||
| 85 | - } | ||
| 86 | - | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - }); | ||
| 90 | } | 49 | } |
| 91 | 50 | ||
| 92 | private Long calculateTaskCandidateUsers(DelegateExecution execution) { | 51 | private Long calculateTaskCandidateUsers(DelegateExecution execution) { |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskService.java
| @@ -188,7 +188,12 @@ public interface BpmTaskService { | @@ -188,7 +188,12 @@ public interface BpmTaskService { | ||
| 188 | // ========== Event 事件相关方法 ========== | 188 | // ========== Event 事件相关方法 ========== |
| 189 | 189 | ||
| 190 | /** | 190 | /** |
| 191 | - * 处理 Task 创建事件,目前是更新它的状态为审批中 | 191 | + * 处理 Task 创建事件,目前是 |
| 192 | + * | ||
| 193 | + * 1. 更新它的状态为审批中 | ||
| 194 | + * 2. 处理自动通过的情况,例如说:1)无审批人时,是否自动通过、不通过;2)非【人工审核】时,是否自动通过、不通过 | ||
| 195 | + * | ||
| 196 | + * 注意:它的触发时机,晚于 {@link #processTaskAssigned(Task)} 之后 | ||
| 192 | * | 197 | * |
| 193 | * @param task 任务实体 | 198 | * @param task 任务实体 |
| 194 | */ | 199 | */ |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java
| @@ -12,9 +12,7 @@ import cn.iocoder.yudao.framework.common.util.object.PageUtils; | @@ -12,9 +12,7 @@ import cn.iocoder.yudao.framework.common.util.object.PageUtils; | ||
| 12 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 12 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; |
| 13 | import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.*; | 13 | import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.*; |
| 14 | import cn.iocoder.yudao.module.bpm.convert.task.BpmTaskConvert; | 14 | import cn.iocoder.yudao.module.bpm.convert.task.BpmTaskConvert; |
| 15 | -import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskAssignStartUserHandlerTypeEnum; | ||
| 16 | -import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskRejectHandlerType; | ||
| 17 | -import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskTimeoutHandlerTypeEnum; | 15 | +import cn.iocoder.yudao.module.bpm.enums.definition.*; |
| 18 | import cn.iocoder.yudao.module.bpm.enums.task.BpmCommentTypeEnum; | 16 | import cn.iocoder.yudao.module.bpm.enums.task.BpmCommentTypeEnum; |
| 19 | import cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum; | 17 | import cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum; |
| 20 | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskSignTypeEnum; | 18 | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskSignTypeEnum; |
| @@ -888,12 +886,55 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -888,12 +886,55 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 888 | 886 | ||
| 889 | @Override | 887 | @Override |
| 890 | public void processTaskCreated(Task task) { | 888 | public void processTaskCreated(Task task) { |
| 889 | + // 1. 设置为待办中 | ||
| 891 | Integer status = (Integer) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_STATUS); | 890 | Integer status = (Integer) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_STATUS); |
| 892 | if (status != null) { | 891 | if (status != null) { |
| 893 | log.error("[updateTaskStatusWhenCreated][taskId({}) 已经有状态({})]", task.getId(), status); | 892 | log.error("[updateTaskStatusWhenCreated][taskId({}) 已经有状态({})]", task.getId(), status); |
| 894 | return; | 893 | return; |
| 895 | } | 894 | } |
| 896 | updateTaskStatus(task.getId(), BpmTaskStatusEnum.RUNNING.getStatus()); | 895 | updateTaskStatus(task.getId(), BpmTaskStatusEnum.RUNNING.getStatus()); |
| 896 | + | ||
| 897 | + // 2. 处理自动通过的情况,例如说:1)无审批人时,是否自动通过、不通过;2)非【人工审核】时,是否自动通过、不通过 | ||
| 898 | + ProcessInstance processInstance = processInstanceService.getProcessInstance(task.getProcessInstanceId()); | ||
| 899 | + if (processInstance == null) { | ||
| 900 | + log.error("[processTaskCreated][taskId({}) 没有找到流程实例]", task.getId()); | ||
| 901 | + return; | ||
| 902 | + } | ||
| 903 | + BpmnModel bpmnModel = modelService.getBpmnModelByDefinitionId(processInstance.getProcessDefinitionId()); | ||
| 904 | + FlowElement userTaskElement = BpmnModelUtils.getFlowElementById(bpmnModel, task.getTaskDefinitionKey()); | ||
| 905 | + Integer approveType = BpmnModelUtils.parseApproveType(userTaskElement); | ||
| 906 | + Integer assignEmptyHandlerType = BpmnModelUtils.parseAssignEmptyHandlerType(userTaskElement); | ||
| 907 | + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { | ||
| 908 | + | ||
| 909 | + @Override | ||
| 910 | + public void afterCompletion(int transactionStatus) { | ||
| 911 | + // 特殊情况:部分情况下,TransactionSynchronizationManager 注册 afterCommit 监听时,不会被调用,但是 afterCompletion 可以 | ||
| 912 | + // 例如说:第一个 task 就是配置【自动通过】或者【自动拒绝】时 | ||
| 913 | + if (ObjectUtil.notEqual(transactionStatus, TransactionSynchronization.STATUS_COMMITTED)) { | ||
| 914 | + return; | ||
| 915 | + } | ||
| 916 | + // 特殊情况一:【人工审核】审批人为空,根据配置是否要自动通过、自动拒绝 | ||
| 917 | + if (ObjectUtil.equal(approveType, BpmUserTaskApproveTypeEnum.USER.getType())) { | ||
| 918 | + if (ObjectUtil.equal(assignEmptyHandlerType, BpmUserTaskAssignEmptyHandlerTypeEnum.APPROVE.getType())) { | ||
| 919 | + SpringUtil.getBean(BpmTaskService.class).approveTask(null, new BpmTaskApproveReqVO() | ||
| 920 | + .setId(task.getId()).setReason(BpmReasonEnum.ASSIGN_EMPTY_APPROVE.getReason())); | ||
| 921 | + } else if (ObjectUtil.equal(assignEmptyHandlerType, BpmUserTaskAssignEmptyHandlerTypeEnum.REJECT.getType())) { | ||
| 922 | + SpringUtil.getBean(BpmTaskService.class).rejectTask(null, new BpmTaskRejectReqVO() | ||
| 923 | + .setId(task.getId()).setReason(BpmReasonEnum.ASSIGN_EMPTY_REJECT.getReason())); | ||
| 924 | + } | ||
| 925 | + // 特殊情况二:【自动审核】审批类型为自动通过、不通过 | ||
| 926 | + } else { | ||
| 927 | + if (ObjectUtil.equal(approveType, BpmUserTaskApproveTypeEnum.AUTO_APPROVE.getType())) { | ||
| 928 | + SpringUtil.getBean(BpmTaskService.class).approveTask(null, new BpmTaskApproveReqVO() | ||
| 929 | + .setId(task.getId()).setReason(BpmReasonEnum.APPROVE_TYPE_AUTO_APPROVE.getReason())); | ||
| 930 | + } else if (ObjectUtil.equal(approveType, BpmUserTaskApproveTypeEnum.AUTO_REJECT.getType())) { | ||
| 931 | + SpringUtil.getBean(BpmTaskService.class).rejectTask(null, new BpmTaskRejectReqVO() | ||
| 932 | + .setId(task.getId()).setReason(BpmReasonEnum.APPROVE_TYPE_AUTO_REJECT.getReason())); | ||
| 933 | + } | ||
| 934 | + } | ||
| 935 | + } | ||
| 936 | + | ||
| 937 | + }); | ||
| 897 | } | 938 | } |
| 898 | 939 | ||
| 899 | /** | 940 | /** |