新增:审批人自选标识,
新增:审批人自选策略,
新增:获得流程实例的审批用户选择的下一个节点的审批人
新增:错误码1_009_004_007,下一个节点审批人未配置
This commit is contained in:
smallNorthLee 2025-03-02 21:06:28 +08:00
parent 4d35a272c3
commit 9dfed5365b
5 changed files with 128 additions and 22 deletions

View File

@ -40,7 +40,7 @@ public interface ErrorCodeConstants {
ErrorCode PROCESS_INSTANCE_START_USER_CAN_START = new ErrorCode(1_009_004_005, "发起流程失败,你没有权限发起该流程");
ErrorCode PROCESS_INSTANCE_CANCEL_FAIL_NOT_ALLOW = new ErrorCode(1_009_004_005, "流程取消失败,该流程不允许取消");
ErrorCode PROCESS_INSTANCE_HTTP_TRIGGER_CALL_ERROR = new ErrorCode(1_009_004_006, "流程 Http 触发器请求调用失败");
ErrorCode PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG = new ErrorCode(1_009_004_007, "任务({})的下一个节点审批人未配置");
ErrorCode PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG = new ErrorCode(1_009_004_007, "任务({})的下一个执行节点审批人未配置");
// ========== 流程任务 1-009-005-000 ==========
ErrorCode TASK_OPERATE_FAIL_ASSIGN_NOT_SELF = new ErrorCode(1_009_005_001, "操作失败,原因:该任务的审批人不是你");

View File

@ -0,0 +1,73 @@
package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user.BpmTaskCandidateUserStrategy;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils;
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
import com.google.common.collect.Sets;
import jakarta.annotation.Resource;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
/**
* 审批人自选 {@link BpmTaskCandidateUserStrategy} 实现类
* 审批人在审批时选择下一个节点的审批人
* @author smallNorthLee
*/
@Component
public class BpmTaskCandidateApproveUserSelectStrategy extends AbstractBpmTaskCandidateDeptLeaderStrategy {
@Resource
@Lazy // 延迟加载避免循环依赖
private BpmProcessInstanceService processInstanceService;
@Override
public BpmTaskCandidateStrategyEnum getStrategy() {
return BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT;
}
@Override
public void validateParam(String param) {}
@Override
public boolean isParamRequired() {
return false;
}
@Override
public LinkedHashSet<Long> calculateUsersByTask(DelegateExecution execution, String param) {
ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId());
Assert.notNull(processInstance, "流程实例({})不能为空", execution.getProcessInstanceId());
Map<String, List<Long>> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processInstance);
Assert.notNull(approveUserSelectAssignees, "流程实例({}) 的下一个执行节点审批人不能为空",
execution.getProcessInstanceId());
// 获得审批人
List<Long> assignees = approveUserSelectAssignees.get(execution.getCurrentActivityId());
return CollUtil.isNotEmpty(assignees) ? new LinkedHashSet<>(assignees) : Sets.newLinkedHashSet();
}
@Override
public LinkedHashSet<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param,
Long startUserId, String processDefinitionId, Map<String, Object> processVariables) {
if (processVariables == null) {
return Sets.newLinkedHashSet();
}
Map<String, List<Long>> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processVariables);
if (approveUserSelectAssignees == null) {
return Sets.newLinkedHashSet();
}
// 获得审批人
List<Long> assignees = approveUserSelectAssignees.get(activityId);
return CollUtil.isNotEmpty(assignees) ? new LinkedHashSet<>(assignees) : Sets.newLinkedHashSet();
}
}

View File

@ -24,6 +24,7 @@ public enum BpmTaskCandidateStrategyEnum implements ArrayValuable<Integer> {
MULTI_DEPT_LEADER_MULTI(23, "连续多级部门的负责人"),
POST(22, "岗位"),
USER(30, "用户"),
APPROVE_USER_SELECT(34, "审批人自选"), // 审批人在审批时选择下一个节点的审批人
START_USER_SELECT(35, "发起人自选"), // 申请人自己可在提交申请时选择此节点的审批人
START_USER(36, "发起人自己"), // 申请人自己, 一般紧挨开始节点常用于发起人信息审核场景
START_USER_DEPT_LEADER(37, "发起人部门负责人"),

View File

@ -200,6 +200,31 @@ public class FlowableUtils {
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES);
}
/**
* 获得流程实例的审批用户选择的下一个节点的审批人 Map
*
* @param processInstance 流程实例
* @return 审批用户选择的下一个节点的审批人Map
*/
public static Map<String, List<Long>> getApproveUserSelectAssignees(ProcessInstance processInstance) {
return processInstance != null ? getApproveUserSelectAssignees(processInstance.getProcessVariables()) : null;
}
/**
* 获得流程实例的审批用户选择的下一个节点的审批人 Map
*
* @param processVariables 流程变量
* @return 审批用户选择的下一个节点的审批人Map Map
*/
@SuppressWarnings("unchecked")
public static Map<String, List<Long>> getApproveUserSelectAssignees(Map<String, Object> processVariables) {
if (processVariables == null) {
return null;
}
return (Map<String, List<Long>>) processVariables.get(
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES);
}
/**
* 获得流程实例的摘要
*

View File

@ -528,22 +528,9 @@ public class BpmTaskServiceImpl implements BpmTaskService {
// 2.3 调用 BPM complete 去完成任务
// 其中variables 是存储动态表单到 local 任务级别过滤一下避免 ProcessInstance 系统级的变量被占用
if (CollUtil.isNotEmpty(reqVO.getVariables())) {
Map<String, Object> variables = FlowableUtils.filterTaskFormVariable(reqVO.getVariables());
// 校验传递的参数中是否为下一个将要执行的任务节点
validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(), bpmnModel, reqVO.getNextAssignees(), instance);
// 如果有下一个审批人则设置到流程变量中
if (CollUtil.isNotEmpty(reqVO.getNextAssignees())) {
// 获取实例中的全部节点数据避免后续节点的审批人被覆盖
// TODO @小北这里有个需要讨论的点微信哈
// TODO 因为 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES 定位是发起人那么审批人选择的放在 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES目前想到两个方案
// TODO 方案一增加一个 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES然后设置到这里面然后BpmTaskCandidateStartUserSelectStrategy 也从这里读
// TODO 方案二也是增加一个 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES根据节点审批人类型放到 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEESPROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES
// TODO 方案三融合成 PROCESS_INSTANCE_VARIABLE_USER_SELECT_ASSIGNEES不再区分是发起人选择还是审批人选择
Map<String, List<Long>> hisProcessVariables = FlowableUtils.getStartUserSelectAssignees(instance.getProcessVariables());
hisProcessVariables.putAll(reqVO.getNextAssignees());
variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, hisProcessVariables);
}
runtimeService.setVariables(task.getProcessInstanceId(), variables);
Map<String, Object> variables = validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(),
bpmnModel, reqVO.getNextAssignees(), instance);
taskService.complete(task.getId(), variables, true);
} else {
taskService.complete(task.getId());
@ -566,21 +553,29 @@ public class BpmTaskServiceImpl implements BpmTaskService {
* @param nextAssignees 下一个节点审批人集合参数
* @param processInstance 流程实例
*/
private void validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel,
private Map<String, Object> validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel,
Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) {
// 1. 获取当前任务节点的信息
FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey);
// 2. 获取下一个将要执行的节点集合
List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables);
// 获取流程实例中的审批人自选审批人
Map<String, List<Long>> hisProcessVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables());
if (CollUtil.isEmpty(hisProcessVariables)){
hisProcessVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
}
// 校验通过的全部节点和审批人
Map<String, List<Long>> allNextAssignees = new HashMap<>();
// 3. 循环下一个将要执行的节点集合
for (FlowNode nextFlowNode : nextFlowNodes) {
// 3.1 获取下一个将要执行节点中的审批人策略
Integer candidateStrategy = parseCandidateStrategy(nextFlowNode);
// 3.2 获取流程实例中的发起人自选审批人
Map<String, List<Long>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
List<Long> startUserSelectAssignee = startUserSelectAssignees.get(nextFlowNode.getId());
// 3.3 如果节点中的审批人策略为 发起人自选并且该节点的审批人为空
if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy()) && CollUtil.isEmpty(startUserSelectAssignee)) {
// 3.2 获取流程实例中的审批人自选审批人
List<Long> approveUserSelectAssignee = hisProcessVariables.get(nextFlowNode.getId());
// 3.3 如果节点中的审批人策略为 发起人自选或者审批人自选并且该节点的审批人为空
if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())
|| ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT.getStrategy())
&& CollUtil.isEmpty(approveUserSelectAssignee)) {
// 判断节点是否为执行节点仅校验节点
if (!nextAssignees.containsKey(nextFlowNode.getId())) {
throw exception(TASK_TARGET_NODE_NOT_EXISTS, nextFlowNode.getName());
@ -589,9 +584,21 @@ public class BpmTaskServiceImpl implements BpmTaskService {
if (CollUtil.isEmpty(nextAssignees.get(nextFlowNode.getId()))) {
throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
}
// 4.3.3 校验通过的全部节点和审批人
allNextAssignees.put(nextFlowNode.getId(), nextAssignees.get(nextFlowNode.getId()));
}
// TODO @小北加一个审批人选择的校验
}
// variables 是存储动态表单到 local 任务级别过滤一下避免 ProcessInstance 系统级的变量被占用
Map<String, Object> newVariables = FlowableUtils.filterTaskFormVariable(variables);
// 4. 如果下个节点审批参数不为空则设置流程变量
if (CollUtil.isNotEmpty(allNextAssignees)) {
// 获取实例中的全部节点数据避免后续节点的审批人被覆盖
hisProcessVariables.putAll(allNextAssignees);
newVariables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, hisProcessVariables);
runtimeService.setVariables(processInstance.getProcessInstanceId(), variables);
}
return newVariables;
}
/**