mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-11 21:29:02 +08:00
feat(flow-engine): 优化审批任务处理逻辑并增强代码可读性
- 重构ApproveServiceTask类,提取拒绝处理逻辑到独立方法 - 增加详细注释说明关键变量和流程逻辑 - 优化变量命名以提高代码可读性 - 实现更清晰的审批条件判断流程 - 支持根据配置将拒绝流程重定向到特定节点或结束节点
This commit is contained in:
1 parent
298d09586d
commit
4779be6cfd
1 file changed
+65
-37
+65
-37
@@ -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,工作流将沿其正常路径继续
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user