【代码评审】BPM:计算下一个节点的审批人(审批人自选)

This commit is contained in:
YunaiV 2025-03-13 06:44:41 +08:00
parent 9ae18fe53d
commit 24261cf767
4 changed files with 24 additions and 22 deletions

View File

@ -908,12 +908,13 @@ public class BpmnModelUtils {
*/ */
private static SequenceFlow findMatchSequenceFlowByExclusiveGateway(Gateway gateway, Map<String, Object> variables) { private static SequenceFlow findMatchSequenceFlowByExclusiveGateway(Gateway gateway, Map<String, Object> variables) {
// TODO 表单无可编辑字段时variables为空流程走向会出现问题比如流程审批过程中无需要修改的字段值 // TODO 表单无可编辑字段时variables为空流程走向会出现问题比如流程审批过程中无需要修改的字段值
// TODO @小北是不是还是保证编辑的时候如果计算下一个节点还是 variables 是完整体而不是空的可以微信讨论下
SequenceFlow matchSequenceFlow; SequenceFlow matchSequenceFlow;
if (CollUtil.isNotEmpty(variables)){ if (CollUtil.isNotEmpty(variables)) {
matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(), matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(),
flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId()) flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId())
&& (evalConditionExpress(variables, flow.getConditionExpression()))); && (evalConditionExpress(variables, flow.getConditionExpression())));
}else { } else {
matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(), matchSequenceFlow = CollUtil.findOne(gateway.getOutgoingFlows(),
flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId())); flow -> ObjUtil.notEqual(gateway.getDefaultFlow(), flow.getId()));
} }

View File

@ -194,7 +194,7 @@ public class FlowableUtils {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Map<String, List<Long>> getStartUserSelectAssignees(Map<String, Object> processVariables) { public static Map<String, List<Long>> getStartUserSelectAssignees(Map<String, Object> processVariables) {
if (processVariables == null) { if (processVariables == null) {
return null; return new HashMap<>();
} }
return (Map<String, List<Long>>) processVariables.get( return (Map<String, List<Long>>) processVariables.get(
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES); BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES);
@ -219,7 +219,7 @@ public class FlowableUtils {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Map<String, List<Long>> getApproveUserSelectAssignees(Map<String, Object> processVariables) { public static Map<String, List<Long>> getApproveUserSelectAssignees(Map<String, Object> processVariables) {
if (processVariables == null) { if (processVariables == null) {
return null; return new HashMap<>();
} }
return (Map<String, List<Long>>) processVariables.get( return (Map<String, List<Long>>) processVariables.get(
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES); BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES);

View File

@ -231,7 +231,7 @@ public class BpmModelServiceImpl implements BpmModelService {
repositoryService.saveModel(model); repositoryService.saveModel(model);
} }
private void validateBpmnXml(byte[] bpmnBytes, int type) { private void validateBpmnXml(byte[] bpmnBytes, Integer type) {
BpmnModel bpmnModel = BpmnModelUtils.getBpmnModel(bpmnBytes); BpmnModel bpmnModel = BpmnModelUtils.getBpmnModel(bpmnBytes);
if (bpmnModel == null) { if (bpmnModel == null) {
throw exception(MODEL_NOT_EXISTS); throw exception(MODEL_NOT_EXISTS);
@ -248,6 +248,7 @@ public class BpmModelServiceImpl implements BpmModelService {
throw exception(MODEL_DEPLOY_FAIL_BPMN_USER_TASK_NAME_NOT_EXISTS, userTask.getId()); throw exception(MODEL_DEPLOY_FAIL_BPMN_USER_TASK_NAME_NOT_EXISTS, userTask.getId());
} }
}); });
// TODO @小北是不是可以 UserTask firUserTask = CollUtil.get(userTasks, BpmModelTypeEnum.BPMN.getType().equals(type) ? 0 : 1)然后最好判空极端情况下 usertask 哈哈哈哈
// 3. 校验第一个用户任务节点的规则类型是否为审批人自选 // 3. 校验第一个用户任务节点的规则类型是否为审批人自选
Map<Integer, UserTask> userTaskMap = new HashMap<>(); Map<Integer, UserTask> userTaskMap = new HashMap<>();
// BPMN 设计器校验第一个用户任务节点 // BPMN 设计器校验第一个用户任务节点

View File

@ -557,12 +557,11 @@ public class BpmTaskServiceImpl implements BpmTaskService {
// 2.2 添加评论 // 2.2 添加评论
taskService.addComment(task.getId(), task.getProcessInstanceId(), BpmCommentTypeEnum.APPROVE.getType(), taskService.addComment(task.getId(), task.getProcessInstanceId(), BpmCommentTypeEnum.APPROVE.getType(),
BpmCommentTypeEnum.APPROVE.formatComment(reqVO.getReason())); BpmCommentTypeEnum.APPROVE.formatComment(reqVO.getReason()));
// 2.3 调用 BPM complete 去完成任务 // 2.3 校验并处理 APPROVE_USER_SELECT 当前审批人选择下一节点审批人的逻辑
// 校验并处理 APPROVE_USER_SELECT 当前审批人选择下一节点审批人的逻辑
Map<String, Object> variables = validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(), Map<String, Object> variables = validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(),
bpmnModel, reqVO.getNextAssignees(), instance); bpmnModel, reqVO.getNextAssignees(), instance);
// 完成任务
runtimeService.setVariables(task.getProcessInstanceId(), variables); runtimeService.setVariables(task.getProcessInstanceId(), variables);
// 2.4 调用 BPM complete 去完成任务
taskService.complete(task.getId(), variables, true); taskService.complete(task.getId(), variables, true);
// 加签专属处理加签任务 // 加签专属处理加签任务
@ -587,10 +586,10 @@ public class BpmTaskServiceImpl implements BpmTaskService {
// 1. 获取下一个将要执行的节点集合 // 1. 获取下一个将要执行的节点集合
FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey); FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey);
List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables); List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables);
// 2. 循环下一个将要执行的节点集合
// 2. 校验选择的下一个节点的审批人是否合法
Map<String, List<Long>> processVariables; Map<String, List<Long>> processVariables;
for (FlowNode nextFlowNode : nextFlowNodes) { for (FlowNode nextFlowNode : nextFlowNodes) {
// 获取任务节点中的审批人策略
Integer candidateStrategy = parseCandidateStrategy(nextFlowNode); Integer candidateStrategy = parseCandidateStrategy(nextFlowNode);
// 2.1 情况一如果节点中的审批人策略为 发起人自选 // 2.1 情况一如果节点中的审批人策略为 发起人自选
if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())) { if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())) {
@ -600,16 +599,16 @@ public class BpmTaskServiceImpl implements BpmTaskService {
throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
} }
processVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables()); processVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
if (processVariables == null){ // 特殊如果当前节点已经存在审批人则不允许覆盖
processVariables = new HashMap<>(); // TODO @小北不用改通过 if return让逻辑更简洁一点虽然会多判断一次 processVariables但是 if else 层级更少
}else { if (processVariables != null
List<Long> startUserSelectAssignee = processVariables.get(nextFlowNode.getId()); && CollUtil.isNotEmpty(processVariables.get(nextFlowNode.getId()))) {
// 特殊如果当前节点已经存在审批人则不允许覆盖 continue;
if (CollUtil.isNotEmpty(startUserSelectAssignee)) { }
continue; // 设置 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES
} if (processVariables == null) {
processVariables = new HashMap<>();
} }
// 校验通过的全部节点和审批人
processVariables.put(nextFlowNode.getId(), assignees); processVariables.put(nextFlowNode.getId(), assignees);
variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, processVariables); variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, processVariables);
} }
@ -621,16 +620,17 @@ public class BpmTaskServiceImpl implements BpmTaskService {
throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
} }
processVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables()); processVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables());
if (processVariables == null){ if (processVariables == null) {
processVariables = new HashMap<>(); processVariables = new HashMap<>();
}else { } else {
List<Long> approveUserSelectAssignee = processVariables.get(nextFlowNode.getId()); List<Long> approveUserSelectAssignee = processVariables.get(nextFlowNode.getId());
// 特殊如果当前节点已经存在审批人则不允许覆盖 // 特殊如果当前节点已经存在审批人则不允许覆盖
// TODO @小北这种应该可以覆盖呢
if (CollUtil.isNotEmpty(approveUserSelectAssignee)) { if (CollUtil.isNotEmpty(approveUserSelectAssignee)) {
continue; continue;
} }
} }
// 校验通过的全部节点和审批人 // 设置 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES
processVariables.put(nextFlowNode.getId(), assignees); processVariables.put(nextFlowNode.getId(), assignees);
variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, processVariables); variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, processVariables);
} }