【代码评审】BPM:子流程

This commit is contained in:
YunaiV 2025-02-22 17:17:27 +08:00
parent 10bbe741b0
commit 254b55778f
8 changed files with 81 additions and 55 deletions

View File

@ -30,7 +30,7 @@ public enum BpmSimpleModelNodeTypeEnum implements ArrayValuable<Integer> {
DELAY_TIMER_NODE(14, "延迟器", "receiveTask"),
TRIGGER_NODE(15, "触发器", "serviceTask"),
CHILD_PROCESS(20, "子流程", "callActivity"),
CHILD_PROCESS(20, "子流程", "callActivity"), // TODO @lesanCHILD_PROCESSASYNC_CHILD_PROCESS 可以合并为一个么
ASYNC_CHILD_PROCESS(21, "异步子流程", "callActivity"),
// 50 ~ 条件分支

View File

@ -26,7 +26,7 @@ public enum BpmReasonEnum {
TIMEOUT_REJECT("审批超时,系统自动不通过"),
ASSIGN_START_USER_APPROVE("审批人与提交人为同一人时,自动通过"),
ASSIGN_START_USER_APPROVE_WHEN_SKIP("审批人与提交人为同一人时,自动通过"),
ASSIGN_START_USER_APPROVE_WHEN_SKIP_START_USER_NODE("发起人节点首次自动通过"),
ASSIGN_START_USER_APPROVE_WHEN_SKIP_START_USER_NODE("发起人节点首次自动通过"), // 目前仅子流程使用
ASSIGN_START_USER_APPROVE_WHEN_DEPT_LEADER_NOT_FOUND("审批人与提交人为同一人时,找不到部门负责人,自动通过"),
ASSIGN_START_USER_TRANSFER_DEPT_LEADER("审批人与提交人为同一人时,转交给部门负责人审批"),
ASSIGN_EMPTY_APPROVE("审批人为空,自动通过"),

View File

@ -124,6 +124,9 @@ public class BpmSimpleModelNodeVO {
*/
private TriggerSetting triggerSetting;
/**
* 子流程设置
*/
private ChildProcessSetting childProcessSetting;
@Schema(description = "任务监听器")
@ -410,21 +413,24 @@ public class BpmSimpleModelNodeVO {
@Valid
public static class ChildProcessSetting {
@Schema(description = "被调用流程", example = "xxx")
// TODO @lesancalledElement => calledProcessDefinitionKey ? 这样更容易理解不过如果一个流程多次发起key 变了好像会有问题
@Schema(description = "被调用流程", requiredMode = Schema.RequiredMode.REQUIRED, example = "xxx")
@NotEmpty(message = "被调用流程不能为空")
private String calledElement;
@Schema(description = "被调用流程名称", example = "xxx")
@Schema(description = "被调用流程名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "xxx")
@NotEmpty(message = "被调用流程名称不能为空")
private String calledElementName;
@Schema(description = "是否异步", example = "false")
@Schema(description = "是否异步", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
@NotNull(message = "是否异步不能为空")
private Boolean async;
@Schema(description = "输入参数(主->子)", example = "[]")
// TODO @lesaninVariables
@Schema(description = "输入参数(主->子)", requiredMode = Schema.RequiredMode.REQUIRED, example = "[]")
private List<IOParameter> inVariable;
// TODO @lesanoutVariables
@Schema(description = "输出参数(子->主)", example = "[]")
private List<IOParameter> outVariable;
@ -432,7 +438,8 @@ public class BpmSimpleModelNodeVO {
@NotNull(message = "是否自动跳过子流程发起节点不能为空")
private Boolean skipStartUserNode;
@Schema(description = "子流程发起人配置", example = "{}")
@Schema(description = "子流程发起人配置", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
// TODO @lesan这个应该也必须填写
private StartUserSetting startUserSetting;
@Schema(description = "子流程发起人配置")
@ -448,7 +455,9 @@ public class BpmSimpleModelNodeVO {
@Schema(description = "表单", example = "xxx")
private String formField;
// TODO @lesanemptyHandleType => emptyType type 对上
@Schema(description = "当子流程发起人为空时类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "当子流程发起人为空时类型不能为空")
@InEnum(BpmChildProcessStartUserEmptyTypeEnum.class)
private Integer emptyHandleType;

View File

@ -25,7 +25,10 @@ import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.TaskInfo;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
@ -241,6 +244,7 @@ public class FlowableUtils {
return formFieldsMap.entrySet().stream()
.limit(3)
.map(entry -> new KeyValue<>(entry.getValue().getTitle(),
// TODO @lesan MapUtil.getStr 可以更简单
StrUtil.toStringOrEmpty(processVariables.getOrDefault(entry.getValue().getField(), ""))))
.collect(Collectors.toList());
}

View File

@ -11,8 +11,11 @@ import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.B
import cn.iocoder.yudao.module.bpm.enums.definition.*;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.listener.BpmCopyTaskDelegate;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.listener.BpmTriggerTaskDelegate;
import cn.iocoder.yudao.module.bpm.service.task.listener.BpmCallActivityListener;
import cn.iocoder.yudao.module.bpm.service.task.listener.BpmUserTaskListener;
import org.flowable.bpmn.BpmnAutoLayout;
import org.flowable.bpmn.constants.BpmnXMLConstants;
import org.flowable.bpmn.model.Process;
@ -24,10 +27,7 @@ import org.springframework.util.MultiValueMap;
import java.util.*;
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.*;
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_SKIP_START_USER_NODE;
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS;
import static cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils.*;
import static cn.iocoder.yudao.module.bpm.service.task.listener.BpmUserTaskListener.DELEGATE_EXPRESSION;
import static java.util.Arrays.asList;
/**
@ -462,7 +462,7 @@ public class SimpleModelUtils {
FlowableListener flowableListener = new FlowableListener();
flowableListener.setEvent(TaskListener.EVENTNAME_CREATE);
flowableListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
flowableListener.setImplementation(DELEGATE_EXPRESSION);
flowableListener.setImplementation(BpmUserTaskListener.DELEGATE_EXPRESSION);
addListenerConfig(flowableListener, node.getTaskCreateListener());
flowableListeners.add(flowableListener);
}
@ -471,7 +471,7 @@ public class SimpleModelUtils {
FlowableListener flowableListener = new FlowableListener();
flowableListener.setEvent(TaskListener.EVENTNAME_ASSIGNMENT);
flowableListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
flowableListener.setImplementation(DELEGATE_EXPRESSION);
flowableListener.setImplementation(BpmUserTaskListener.DELEGATE_EXPRESSION);
addListenerConfig(flowableListener, node.getTaskAssignListener());
flowableListeners.add(flowableListener);
}
@ -480,7 +480,7 @@ public class SimpleModelUtils {
FlowableListener flowableListener = new FlowableListener();
flowableListener.setEvent(TaskListener.EVENTNAME_COMPLETE);
flowableListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
flowableListener.setImplementation(DELEGATE_EXPRESSION);
flowableListener.setImplementation(BpmUserTaskListener.DELEGATE_EXPRESSION);
addListenerConfig(flowableListener, node.getTaskCompleteListener());
flowableListeners.add(flowableListener);
}
@ -788,48 +788,56 @@ public class SimpleModelUtils {
CallActivity callActivity = new CallActivity();
callActivity.setId(node.getId());
callActivity.setName(node.getName());
callActivity.setCalledElementType("key");
callActivity.setCalledElementType("key"); // TODO @lesan这里为啥是 key
// 1. 是否异步
callActivity.setAsynchronous(node.getChildProcessSetting().getAsync());
// 2. 调用的子流程
callActivity.setCalledElement(childProcessSetting.getCalledElement());
callActivity.setProcessInstanceName(childProcessSetting.getCalledElementName());
// 3. 是否自动跳过子流程发起节点
// TODO @lesan貌似只有 SourceExpression 的区别直接通过 valueOf childProcessSetting.getSkipStartUserNode()
if (Boolean.TRUE.equals(childProcessSetting.getSkipStartUserNode())) {
IOParameter ioParameter = new IOParameter();
ioParameter.setSourceExpression("true");
ioParameter.setTarget(PROCESS_INSTANCE_VARIABLE_SKIP_START_USER_NODE);
ioParameter.setTarget(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_SKIP_START_USER_NODE);
inVariable.add(ioParameter);
} else {
IOParameter ioParameter = new IOParameter();
ioParameter.setSourceExpression("false");
ioParameter.setTarget(PROCESS_INSTANCE_VARIABLE_SKIP_START_USER_NODE);
ioParameter.setTarget(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_SKIP_START_USER_NODE);
inVariable.add(ioParameter);
}
// 4. 子变量传递
// 默认需要传递的一些变量
// 4.1 流程状态
// 4.1 默认需要传递的一些变量流程状态
// TODO @lesan4.1 这个要不单独一个序号类似 3. 这个然后下面就是把 子变量传递主变量传递这样逻辑连贯点哈
IOParameter ioParameter = new IOParameter();
ioParameter.setSource(PROCESS_INSTANCE_VARIABLE_STATUS);
ioParameter.setTarget(PROCESS_INSTANCE_VARIABLE_STATUS);
ioParameter.setSource(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS);
ioParameter.setTarget(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS);
inVariable.add(ioParameter);
callActivity.setInParameters(inVariable);
// 5. 主变量传递
// TODO @lesan通过 isNotEmpty 这种哈
if (childProcessSetting.getOutVariable() != null && !childProcessSetting.getOutVariable().isEmpty()) {
callActivity.setOutParameters(childProcessSetting.getOutVariable());
}
// 6. 子流程发起人配置
List<FlowableListener> executionListeners = new ArrayList<>();
FlowableListener flowableListener = new FlowableListener();
flowableListener.setEvent(ExecutionListener.EVENTNAME_START);
flowableListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
flowableListener.setImplementation("${bpmCallActivityListener}");
flowableListener.setImplementation(BpmCallActivityListener.DELEGATE_EXPRESSION);
FieldExtension fieldExtension = new FieldExtension();
fieldExtension.setFieldName("listenerConfig");
fieldExtension.setStringValue(JsonUtils.toJsonString(childProcessSetting.getStartUserSetting()));
flowableListener.getFieldExtensions().add(fieldExtension);
executionListeners.add(flowableListener);
callActivity.setExecutionListeners(executionListeners);
// 添加节点类型
addNodeType(node.getType(), callActivity);
return callActivity;

View File

@ -320,7 +320,7 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
// 遍历 tasks 列表只处理已结束的 UserTask
// 为什么不通过 activities 因为加签场景下它只存在于 tasks没有 activities导致如果遍历 activities
// 的话它无法成为一个节点
// TODO @芋艿子流程只有activity这里获取不到已结束的子流程
// TODO @芋艿子流程只有activity这里获取不到已结束的子流程TODO @lesan这个会有啥影响微信聊
List<HistoricTaskInstance> endTasks = filterList(tasks, task -> task.getEndTime() != null);
List<ActivityNode> approvalNodes = convertList(endTasks, task -> {
FlowElement flowNode = BpmnModelUtils.getFlowElementById(bpmnModel, task.getTaskDefinitionKey());
@ -389,7 +389,7 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
Map<String, Object> processVariables,
List<HistoricActivityInstance> activities,
List<HistoricTaskInstance> tasks) {
// 构建运行中的任务基于 activityId 分组
// 构建运行中的任务子流程基于 activityId 分组
List<HistoricActivityInstance> runActivities = filterList(activities, activity -> activity.getEndTime() == null
&& (StrUtil.equalsAny(activity.getActivityType(), ELEMENT_TASK_USER, ELEMENT_CALL_ACTIVITY)));
Map<String, List<HistoricActivityInstance>> runningTaskMap = convertMultiMap(runActivities,
@ -414,6 +414,7 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
// 处理每个任务的 tasks 属性
for (HistoricActivityInstance activity : taskActivities) {
HistoricTaskInstance task = taskMap.get(activity.getTaskId());
// TODO @lesan这里为啥 continue
if (task == null) {
continue;
}

View File

@ -1214,7 +1214,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
}
}
// 发起人节点
// 获取发起人节点
BpmnModel bpmnModel = modelService.getBpmnModelByDefinitionId(processInstance.getProcessDefinitionId());
if (bpmnModel == null) {
log.error("[processTaskAssigned][taskId({}) 没有找到流程模型]", task.getId());
@ -1227,16 +1227,17 @@ public class BpmTaskServiceImpl implements BpmTaskService {
String.format(PROCESS_INSTANCE_VARIABLE_RETURN_FLAG, task.getTaskDefinitionKey()), Boolean.class);
Boolean skipStartUserNodeFlag = Convert.toBool(runtimeService.getVariable(processInstance.getProcessInstanceId(),
PROCESS_INSTANCE_VARIABLE_SKIP_START_USER_NODE, String.class));
if (userTaskElement.getId().equals(START_USER_NODE_ID) &&
(skipStartUserNodeFlag == null || Boolean.TRUE.equals(skipStartUserNodeFlag)) &&
!Boolean.TRUE.equals(returnTaskFlag)) {
if (userTaskElement.getId().equals(START_USER_NODE_ID)
&& (skipStartUserNodeFlag == null // 目的一般是主流程发起人节点自动通过审核
|| Boolean.TRUE.equals(skipStartUserNodeFlag)) // 目的一般是子流程发起人节点按配置自动通过审核
&& !Boolean.TRUE.equals(returnTaskFlag)) { // TODO @lesanObjUtil.notEqual(returnTaskFlag, Boolean.TRUE) 改成这个有问题么尽量不用 ! 取反
getSelf().approveTask(Long.valueOf(task.getAssignee()), new BpmTaskApproveReqVO().setId(task.getId())
.setReason(BpmReasonEnum.ASSIGN_START_USER_APPROVE_WHEN_SKIP_START_USER_NODE.getReason()));
return;
}
// 当不为发起人节点时审批人与提交人为同一人时根据 BpmUserTaskAssignStartUserHandlerTypeEnum 策略进行处理
if (!userTaskElement.getId().equals(START_USER_NODE_ID) &&
StrUtil.equals(task.getAssignee(), processInstance.getStartUserId())) {
if (ObjectUtil.notEqual(userTaskElement.getId(), START_USER_NODE_ID)
&& StrUtil.equals(task.getAssignee(), processInstance.getStartUserId())) {
if (ObjUtil.notEqual(returnTaskFlag, Boolean.TRUE)) {
Integer assignStartUserHandlerType = BpmnModelUtils.parseAssignStartUserHandlerType(userTaskElement);

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.bpm.service.task.listener;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
@ -22,7 +21,7 @@ import org.springframework.stereotype.Component;
import java.util.List;
/**
* BPM 子流程监听器
* BPM 子流程监听器设置流程的发起人
*
* @author Lesan
*/
@ -42,47 +41,51 @@ public class BpmCallActivityListener implements ExecutionListener {
public void notify(DelegateExecution execution) {
String expressionText = listenerConfig.getExpressionText();
Assert.notNull(expressionText, "监听器扩展字段({})不能为空", expressionText);
BpmSimpleModelNodeVO.ChildProcessSetting.StartUserSetting startUserSetting = JsonUtils.parseObject(expressionText, BpmSimpleModelNodeVO.ChildProcessSetting.StartUserSetting.class);
// 1. 当发起人来源为表单时
if (startUserSetting != null &&
startUserSetting.getType().equals(BpmChildProcessStartUserTypeEnum.FROM_FORM.getType())) {
BpmSimpleModelNodeVO.ChildProcessSetting.StartUserSetting startUserSetting = JsonUtils.parseObject(
expressionText, BpmSimpleModelNodeVO.ChildProcessSetting.StartUserSetting.class);
// 1. 当发起人来源为主流程发起人时并兜底 startUserSetting 为空时
if (startUserSetting == null
|| startUserSetting.getType().equals(BpmChildProcessStartUserTypeEnum.MAIN_PROCESS_START_USER.getType())) {
ExecutionEntity parent = (ExecutionEntity) execution.getParent();
String formField = parent.getVariable(startUserSetting.getFormField(), String.class);
// 1.1 当表单值为空时
if (StrUtil.isEmpty(formField)) {
// 1.1.1 来自主流程发起人
if (startUserSetting.getEmptyHandleType().equals(BpmChildProcessStartUserEmptyTypeEnum.MAIN_PROCESS_START_USER.getType())){
FlowableUtils.setAuthenticatedUserId(Long.parseLong(parent.getStartUserId()));
return;
}
// 2. 当发起人来源为表单时
if (startUserSetting.getType().equals(BpmChildProcessStartUserTypeEnum.FROM_FORM.getType())) {
ExecutionEntity parent = (ExecutionEntity) execution.getParent();
String formFieldValue = parent.getVariable(startUserSetting.getFormField(), String.class);
// 2.1 当表单值为空时
if (StrUtil.isEmpty(formFieldValue)) {
// 2.1.1 来自主流程发起人
if (startUserSetting.getEmptyHandleType().equals(BpmChildProcessStartUserEmptyTypeEnum.MAIN_PROCESS_START_USER.getType())) {
FlowableUtils.setAuthenticatedUserId(Long.parseLong(parent.getStartUserId()));
return;
}
// 1.1.2 来自子流程管理员
if (startUserSetting.getEmptyHandleType().equals(BpmChildProcessStartUserEmptyTypeEnum.CHILD_PROCESS_ADMIN.getType())){
// 2.1.2 来自子流程管理员
if (startUserSetting.getEmptyHandleType().equals(BpmChildProcessStartUserEmptyTypeEnum.CHILD_PROCESS_ADMIN.getType())) {
BpmProcessDefinitionInfoDO processDefinition = processDefinitionService.getProcessDefinitionInfo(execution.getProcessDefinitionId());
List<Long> managerUserIds = processDefinition.getManagerUserIds();
FlowableUtils.setAuthenticatedUserId(Convert.toLong(managerUserIds.get(0)));
FlowableUtils.setAuthenticatedUserId(managerUserIds.get(0));
return;
}
// 1.1.3 来自主流程管理员
if (startUserSetting.getEmptyHandleType().equals(BpmChildProcessStartUserEmptyTypeEnum.MAIN_PROCESS_ADMIN.getType())){
// 2.1.3 来自主流程管理员
if (startUserSetting.getEmptyHandleType().equals(BpmChildProcessStartUserEmptyTypeEnum.MAIN_PROCESS_ADMIN.getType())) {
BpmProcessDefinitionInfoDO processDefinition = processDefinitionService.getProcessDefinitionInfo(parent.getProcessDefinitionId());
List<Long> managerUserIds = processDefinition.getManagerUserIds();
FlowableUtils.setAuthenticatedUserId(Convert.toLong(managerUserIds.get(0)));
FlowableUtils.setAuthenticatedUserId(managerUserIds.get(0));
return;
}
}
// 1.2 使用表单值并兜底字符串转Long失败时使用主流程发起人
// 2.2 使用表单值并兜底字符串转 Long 失败时使用主流程发起人
try {
FlowableUtils.setAuthenticatedUserId(Long.parseLong(formField));
FlowableUtils.setAuthenticatedUserId(Long.parseLong(formFieldValue));
} catch (Exception e) {
// todo @lesan打个日志方便排查
FlowableUtils.setAuthenticatedUserId(Long.parseLong(parent.getStartUserId()));
}
}
// 2. 当发起人来源为主流程发起人时并兜底startUserSetting为空时
if (startUserSetting == null ||
startUserSetting.getType().equals(BpmChildProcessStartUserTypeEnum.MAIN_PROCESS_START_USER.getType())) {
ExecutionEntity parent = (ExecutionEntity) execution.getParent();
FlowableUtils.setAuthenticatedUserId(Long.parseLong(parent.getStartUserId()));
}
}
}