fix:获取下一个节点审批人为空时,返回new HashMap<>(),避免下级使用空指针

review: 优化审批时,校验选择的下一个节点的审批人,是否合法
This commit is contained in:
lizhixian 2025-03-11 17:11:41 +08:00
parent b6a9b5dda9
commit 26dd8b6670
2 changed files with 32 additions and 39 deletions

View File

@ -194,7 +194,7 @@ public class FlowableUtils {
@SuppressWarnings("unchecked")
public static Map<String, List<Long>> getStartUserSelectAssignees(Map<String, Object> processVariables) {
if (processVariables == null) {
return null;
return new HashMap<>();
}
return (Map<String, List<Long>>) processVariables.get(
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES);
@ -214,12 +214,12 @@ public class FlowableUtils {
* 获得流程实例的审批用户选择的下一个节点的审批人 Map
*
* @param processVariables 流程变量
* @return 审批用户选择的下一个节点的审批人Map Map
* @return 审批用户选择的下一个节点的审批人Map
*/
@SuppressWarnings("unchecked")
public static Map<String, List<Long>> getApproveUserSelectAssignees(Map<String, Object> processVariables) {
if (processVariables == null) {
return null;
return new HashMap<>();
}
return (Map<String, List<Long>>) processVariables.get(
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES);

View File

@ -558,18 +558,12 @@ public class BpmTaskServiceImpl implements BpmTaskService {
taskService.addComment(task.getId(), task.getProcessInstanceId(), BpmCommentTypeEnum.APPROVE.getType(),
BpmCommentTypeEnum.APPROVE.formatComment(reqVO.getReason()));
// 2.3 调用 BPM complete 去完成任务
// 其中variables 是存储动态表单到 local 任务级别过滤一下避免 ProcessInstance 系统级的变量被占用
if (CollUtil.isNotEmpty(reqVO.getVariables())) {
// 校验并处理 APPROVE_USER_SELECT 当前审批人选择下一节点审批人的逻辑
Map<String, Object> variables = validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(),
bpmnModel, reqVO.getNextAssignees(), instance);
// 完成任务
runtimeService.setVariables(task.getProcessInstanceId(), variables);
taskService.complete(task.getId(), variables, true);
} else {
// 完成任务
taskService.complete(task.getId());
}
// 校验并处理 APPROVE_USER_SELECT 当前审批人选择下一节点审批人的逻辑
Map<String, Object> variables = validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(),
bpmnModel, reqVO.getNextAssignees(), instance);
// 完成任务
runtimeService.setVariables(task.getProcessInstanceId(), variables);
taskService.complete(task.getId(), variables, true);
// 加签专属处理加签任务
handleParentTaskIfSign(task.getParentTaskId());
@ -590,51 +584,50 @@ public class BpmTaskServiceImpl implements BpmTaskService {
*/
private Map<String, Object> validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel,
Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) {
// 下一个节点参数为空不做处理表示流程正常流转无需选择下一个节点的审判人
// TODO @小北会出现漏选其实需要的情况哇
if (CollUtil.isEmpty(nextAssignees)) {
return variables;
}
// 1. 获取下一个将要执行的节点集合
FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey);
List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables);
// 2. 循环下一个将要执行的节点集合
Map<String, List<Long>> processVariables = new HashMap<>();
Map<String, List<Long>> processVariables;
for (FlowNode nextFlowNode : nextFlowNodes) {
if (!nextAssignees.containsKey(nextFlowNode.getId())) {
throw exception(TASK_TARGET_NODE_NOT_EXISTS, nextFlowNode.getName());
}
List<Long> assignees = nextAssignees.get(nextFlowNode.getId());
// 2.1 情况一如果节点中的审批人策略为 发起人自选
// 获取任务节点中的审批人策略
Integer candidateStrategy = parseCandidateStrategy(nextFlowNode);
// 2.1 情况一如果节点中的审批人策略为 发起人自选
if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())) {
processVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
if (processVariables == null) {
processVariables = new HashMap<>();
}
List<Long> startUserSelectAssignee = processVariables.get(nextFlowNode.getId());
// 特殊如果当前节点已经存在审批人则不允许覆盖
if (CollUtil.isNotEmpty(startUserSelectAssignee)) {
continue;
}
// 如果节点存在但未配置审批人
List<Long> assignees = nextAssignees != null ? nextAssignees.get(nextFlowNode.getId()) : null;
if (CollUtil.isEmpty(assignees)) {
throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
}
processVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
if (CollUtil.isNotEmpty(processVariables)) {
List<Long> startUserSelectAssignee = processVariables.get(nextFlowNode.getId());
// 特殊如果当前节点已经存在审批人则不允许覆盖
if (CollUtil.isNotEmpty(startUserSelectAssignee)) {
continue;
}
}
// 校验通过的全部节点和审批人
processVariables.put(nextFlowNode.getId(), assignees);
processVariables.put(nextFlowNode.getId(), nextAssignees.get(nextFlowNode.getId()));
variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, processVariables);
}
// 2.2 情况二如果节点中的审批人策略为 审批人在审批时选择下一个节点的审批人并且该节点的审批人为空
if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT.getStrategy())) {
// 如果节点存在但未配置审批人
List<Long> assignees = nextAssignees != null ? nextAssignees.get(nextFlowNode.getId()) : null;
if (CollUtil.isEmpty(assignees)) {
throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
}
processVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables());
if (CollUtil.isNotEmpty(processVariables)) {
List<Long> approveUserSelectAssignee = processVariables.get(nextFlowNode.getId());
// 特殊如果当前节点已经存在审批人则不允许覆盖
if (CollUtil.isNotEmpty(approveUserSelectAssignee)) {
continue;
}
}
// 校验通过的全部节点和审批人
processVariables.put(nextFlowNode.getId(), assignees);
// TODO @小北是不是要类似情况一的做法通过 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES 拿一下因为如果 task1 是审批人自选task2 是审批人自选看着会覆盖
processVariables.put(nextFlowNode.getId(), nextAssignees.get(nextFlowNode.getId()));
variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, processVariables);
}
}