flowable 根据流程变量预判下一个审批节点
public List<String> queryNextTaskCandidateGroup(String instanceId){
List<String> candidateGroupIds = new ArrayList<>();
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(instanceId)
.singleResult();
Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
FlowElement flowElement = bpmnModel.getFlowElement(task.getTaskDefinitionKey());
List<SequenceFlow> outgoingFlows = ((UserTask)flowElement).getOutgoingFlows();
for (SequenceFlow outgoingFlow : outgoingFlows) {
FlowElement targetElement = outgoingFlow.getTargetFlowElement();
if (targetElement instanceof UserTask) {
UserTask nextUserTask = (UserTask) targetElement;
// 获取下一个用户任务的详细信息
candidateGroupIds = nextUserTask.getCandidateGroups();
break;
}else if(targetElement instanceof ExclusiveGateway){
ExclusiveGateway gateway = (ExclusiveGateway) targetElement;
List<SequenceFlow> outgoingFlows1 = gateway.getOutgoingFlows();
Map<String, Object> variables = runtimeService.getVariables(instanceId);
variables.put("state","agree");//设置审批通过时的流程变量
for (SequenceFlow item : outgoingFlows1) {
String conditionExpression = item.getConditionExpression();
// 评估条件表达式
ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
Expression expression = expressionManager.createExpression(conditionExpression);
MapVariableContainer container = new MapVariableContainer(variables);
boolean result = (Boolean) expression.getValue(container);
System.out.println("Expression result: " + result);
if(conditionExpression !=null && result){
FlowElement targetFlowElement = item.getTargetFlowElement();
if(targetFlowElement instanceof UserTask){
UserTask nextUserTask = (UserTask) targetFlowElement;
// 获取下一个用户任务的详细信息
candidateGroupIds= nextUserTask.getCandidateGroups();
}
}
}
}
}
List<String> roleIds = candidateGroupIds.stream().map(item -> item.replace("ROLE", "")).collect(Collectors.toList());
return roleIds;
}