Commit e380bc34f36273a3eaba0b99d769f9eda3da8e24

Authored by YunaiV
1 parent b48052a5

【功能修复】工作流:延迟器 trigger 时,租户 id 丢失的问题

yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/BpmTaskCandidateInvoker.java
... ... @@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskApproveTypeEnum;
12 12 import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskAssignStartUserHandlerTypeEnum;
13 13 import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum;
14 14 import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils;
  15 +import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils;
15 16 import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
16 17 import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
17 18 import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
... ... @@ -91,35 +92,39 @@ public class BpmTaskCandidateInvoker {
91 92 */
92 93 @DataPermission(enable = false) // 忽略数据权限,避免因为过滤,导致找不到候选人
93 94 public Set<Long> calculateUsersByTask(DelegateExecution execution) {
94   - // 审批类型非人工审核时,不进行计算候选人。原因是:后续会自动通过、不通过
95   - FlowElement flowElement = execution.getCurrentFlowElement();
96   - Integer approveType = BpmnModelUtils.parseApproveType(flowElement);
97   - if (ObjectUtils.equalsAny(approveType,
98   - BpmUserTaskApproveTypeEnum.AUTO_APPROVE.getType(),
99   - BpmUserTaskApproveTypeEnum.AUTO_REJECT.getType())) {
100   - return new HashSet<>();
101   - }
102   -
103   - // 1.1 计算任务的候选人
104   - Integer strategy = BpmnModelUtils.parseCandidateStrategy(flowElement);
105   - String param = BpmnModelUtils.parseCandidateParam(flowElement);
106   - Set<Long> userIds = getCandidateStrategy(strategy).calculateUsersByTask(execution, param);
107   - // 1.2 移除被禁用的用户
108   - removeDisableUsers(userIds);
  95 + // 注意:解决极端情况下,Flowable 异步调用,导致租户 id 丢失的情况
  96 + // 例如说,SIMPLE 延迟器在 trigger 的时候!!!
  97 + return FlowableUtils.execute(execution.getTenantId(), () -> {
  98 + // 审批类型非人工审核时,不进行计算候选人。原因是:后续会自动通过、不通过
  99 + FlowElement flowElement = execution.getCurrentFlowElement();
  100 + Integer approveType = BpmnModelUtils.parseApproveType(flowElement);
  101 + if (ObjectUtils.equalsAny(approveType,
  102 + BpmUserTaskApproveTypeEnum.AUTO_APPROVE.getType(),
  103 + BpmUserTaskApproveTypeEnum.AUTO_REJECT.getType())) {
  104 + return new HashSet<>();
  105 + }
109 106  
110   - // 2. 候选人为空时,根据“审批人为空”的配置补充
111   - if (CollUtil.isEmpty(userIds)) {
112   - userIds = getCandidateStrategy(BpmTaskCandidateStrategyEnum.ASSIGN_EMPTY.getStrategy())
113   - .calculateUsersByTask(execution, param);
114   - // ASSIGN_EMPTY 策略,不需要移除被禁用的用户。原因是,再移除,可能会出现更没审批人了!!!
115   - }
  107 + // 1.1 计算任务的候选人
  108 + Integer strategy = BpmnModelUtils.parseCandidateStrategy(flowElement);
  109 + String param = BpmnModelUtils.parseCandidateParam(flowElement);
  110 + Set<Long> userIds = getCandidateStrategy(strategy).calculateUsersByTask(execution, param);
  111 + // 1.2 移除被禁用的用户
  112 + removeDisableUsers(userIds);
  113 +
  114 + // 2. 候选人为空时,根据“审批人为空”的配置补充
  115 + if (CollUtil.isEmpty(userIds)) {
  116 + userIds = getCandidateStrategy(BpmTaskCandidateStrategyEnum.ASSIGN_EMPTY.getStrategy())
  117 + .calculateUsersByTask(execution, param);
  118 + // ASSIGN_EMPTY 策略,不需要移除被禁用的用户。原因是,再移除,可能会出现更没审批人了!!!
  119 + }
116 120  
117   - // 3. 移除发起人的用户
118   - ProcessInstance processInstance = SpringUtil.getBean(BpmProcessInstanceService.class)
119   - .getProcessInstance(execution.getProcessInstanceId());
120   - Assert.notNull(processInstance, "流程实例({}) 不存在", execution.getProcessInstanceId());
121   - removeStartUserIfSkip(userIds, flowElement, Long.valueOf(processInstance.getStartUserId()));
122   - return userIds;
  121 + // 3. 移除发起人的用户
  122 + ProcessInstance processInstance = SpringUtil.getBean(BpmProcessInstanceService.class)
  123 + .getProcessInstance(execution.getProcessInstanceId());
  124 + Assert.notNull(processInstance, "流程实例({}) 不存在", execution.getProcessInstanceId());
  125 + removeStartUserIfSkip(userIds, flowElement, Long.valueOf(processInstance.getStartUserId()));
  126 + return userIds;
  127 + });
123 128 }
124 129  
125 130 public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId,
... ...
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/SimpleModelUtils.java
... ... @@ -628,8 +628,7 @@ public class SimpleModelUtils {
628 628 TimerEventDefinition eventDefinition = new TimerEventDefinition();
629 629 if (node.getDelaySetting().getDelayType().equals(BpmDelayTimerType.FIXED_DATE_TIME.getType())){
630 630 eventDefinition.setTimeDuration(node.getDelaySetting().getDelayTime());
631   - }
632   - if (node.getDelaySetting().getDelayType().equals(BpmDelayTimerType.FIXED_TIME_DURATION.getType())){
  631 + } else if (node.getDelaySetting().getDelayType().equals(BpmDelayTimerType.FIXED_TIME_DURATION.getType())){
633 632 eventDefinition.setTimeDate(node.getDelaySetting().getDelayTime());
634 633 }
635 634 boundaryEvent.addEventDefinition(eventDefinition);
... ...
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java
... ... @@ -1246,15 +1246,14 @@ public class BpmTaskServiceImpl implements BpmTaskService {
1246 1246 .activityId(taskDefineKey)
1247 1247 .singleResult();
1248 1248 if (execution == null) {
1249   - log.error("[processDelayTimerTimeout][processInstanceId({})activityId({}) 没有找到执行活动]",
  1249 + log.error("[processDelayTimerTimeout][processInstanceId({}) activityId({}) 没有找到执行活动]",
1250 1250 processInstanceId, taskDefineKey);
1251 1251 return;
1252 1252 }
  1253 +
1253 1254 // 若存在直接触发接收任务,执行后续节点
1254   - // TODO @芋艿 这里需要帮助看一下,我不懂为啥开启了租户后就一直报错:不存在租户编号
1255   - FlowableUtils.execute(execution.getTenantId(), () -> {
1256   - runtimeService.trigger(execution.getId());
1257   - });
  1255 + FlowableUtils.execute(execution.getTenantId(),
  1256 + () -> runtimeService.trigger(execution.getId()));
1258 1257 }
1259 1258  
1260 1259 /**
... ...