From 4779be6cfd3a7c02c025725b684dd9de59f86958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Sat, 3 May 2025 17:01:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(flow-engine):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=AE=A1=E6=89=B9=E4=BB=BB=E5=8A=A1=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E5=A2=9E=E5=BC=BA=E4=BB=A3=E7=A0=81=E5=8F=AF?= =?UTF-8?q?=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构ApproveServiceTask类,提取拒绝处理逻辑到独立方法 - 增加详细注释说明关键变量和流程逻辑 - 优化变量命名以提高代码可读性 - 实现更清晰的审批条件判断流程 - 支持根据配置将拒绝流程重定向到特定节点或结束节点 --- .../engine/service/ApproveServiceTask.java | 102 +++++++++++------- 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/pigx-flow/pigx-flow-engine/pigx-flow-engine-biz/src/main/java/com/pig4cloud/pigx/flow/engine/service/ApproveServiceTask.java b/pigx-flow/pigx-flow-engine/pigx-flow-engine-biz/src/main/java/com/pig4cloud/pigx/flow/engine/service/ApproveServiceTask.java index eb62a2436..6359160ca 100644 --- a/pigx-flow/pigx-flow-engine/pigx-flow-engine-biz/src/main/java/com/pig4cloud/pigx/flow/engine/service/ApproveServiceTask.java +++ b/pigx-flow/pigx-flow-engine/pigx-flow-engine-biz/src/main/java/com/pig4cloud/pigx/flow/engine/service/ApproveServiceTask.java @@ -5,59 +5,87 @@ import cn.hutool.extra.spring.SpringUtil; import com.pig4cloud.pigx.flow.task.api.feign.RemoteFlowTaskService; import com.pig4cloud.pigx.flow.task.dto.Node; import com.pig4cloud.pigx.flow.task.dto.Refuse; -import lombok.extern.slf4j.Slf4j; import org.flowable.engine.RuntimeService; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.JavaDelegate; import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl; /** - * 审批任务处理器--java服务任务 + * 审批服务任务类,负责处理工作流中的审批逻辑 + * 该类根据审批条件确定工作流的下一步流程 */ -@Slf4j public class ApproveServiceTask implements JavaDelegate { - @Override - public void execute(DelegateExecution execution) { + @Override + public void execute(DelegateExecution execution) { + // 转换为实现类以访问额外的方法 + ExecutionEntityImpl entity = (ExecutionEntityImpl) execution; - ExecutionEntityImpl entity = (ExecutionEntityImpl) execution; - String nodeIdO = entity.getActivityId(); - String flowId = entity.getProcessDefinitionKey(); - String processInstanceId = entity.getProcessInstanceId(); + // 从执行上下文中提取关键标识符 + String currentActivityId = entity.getActivityId(); // 当前活动ID + String flowDefinitionKey = entity.getProcessDefinitionKey(); // 流程定义键 + String processInstanceId = entity.getProcessInstanceId(); // 流程实例ID - String nodeId = StrUtil.subAfter(nodeIdO, "approve_service_task_", true); + // 从活动ID中提取节点标识符 + // 如果currentActivityId是"approve_service_task_node1",则nodeId将是"node1" + String nodeId = StrUtil.subAfter(currentActivityId, "approve_service_task_", true); - Boolean approve = execution.getVariable(StrUtil.format("{}_approve_condition", nodeId), Boolean.class); + // 获取此节点的审批条件变量 + // 变量名格式:"{nodeId}_approve_condition"(例如:"node1_approve_condition") + String conditionVarName = StrUtil.format("{}_approve_condition", nodeId); + Boolean isApproved = execution.getVariable(conditionVarName, Boolean.class); - if (approve != null) { + // 只有当审批条件明确设置时才继续处理 + if (isApproved != null) { + // 获取Flowable RuntimeService bean,用于工作流状态变更 + RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class); - RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class); + // 如果未批准,确定工作流重定向的位置 + if (!isApproved) { + handleRejection(flowDefinitionKey, nodeId, currentActivityId, processInstanceId, runtimeService); + } + // 如果已批准,工作流将沿其正常路径继续(无需操作) + } + } - if (!approve) { - // 跳转 - RemoteFlowTaskService remoteFlowTaskService = SpringUtil.getBean(RemoteFlowTaskService.class); - Node node = remoteFlowTaskService.queryNodeOriData(flowId, nodeId).getData(); - Refuse refuse = node.getRefuse(); - if (refuse != null) { - String handler = refuse.getHandler(); - if (StrUtil.equals(handler, "TO_NODE")) { - runtimeService.createChangeActivityStateBuilder() - .processInstanceId(processInstanceId) - .moveActivityIdTo(nodeIdO, refuse.getNodeId()) - .changeState(); - } - else { - runtimeService.createChangeActivityStateBuilder() - .processInstanceId(processInstanceId) - .moveActivityIdTo(nodeIdO, "end") - .changeState(); - } - } + /** + * 处理任务被拒绝时的工作流重定向逻辑 + * + * @param flowDefinitionKey 流程定义键 + * @param nodeId 当前节点标识符 + * @param currentActivityId 当前活动标识符 + * @param processInstanceId 流程实例标识符 + * @param runtimeService 用于工作流状态操作的运行时服务 + */ + private void handleRejection(String flowDefinitionKey, String nodeId, + String currentActivityId, String processInstanceId, + RuntimeService runtimeService) { + // 获取远程流程任务服务以获取节点配置数据 + RemoteFlowTaskService remoteFlowTaskService = SpringUtil.getBean(RemoteFlowTaskService.class); - } + // 查询节点的原始配置数据 + Node node = remoteFlowTaskService.queryNodeOriData(flowDefinitionKey, nodeId).getData(); - } + // 获取拒绝处理配置 + Refuse refuseConfig = node.getRefuse(); - } + if (refuseConfig != null) { + String rejectionHandler = refuseConfig.getHandler(); -} + // 为当前流程实例创建状态变更构建器 + var stateChangeBuilder = runtimeService.createChangeActivityStateBuilder() + .processInstanceId(processInstanceId); + + if (StrUtil.equals(rejectionHandler, "TO_NODE")) { + // 根据拒绝设置重定向到特定节点 + stateChangeBuilder.moveActivityIdTo(currentActivityId, refuseConfig.getNodeId()) + .changeState(); + } else { + // 默认行为:重定向到结束节点 + stateChangeBuilder.moveActivityIdTo(currentActivityId, "end") + .changeState(); + } + } + // 如果未设置refuseConfig,工作流将沿其正常路径继续 + } +} \ No newline at end of file