其他分享
首页 > 其他分享> > 跳转到任意节点实现(回退到某个节点)

跳转到任意节点实现(回退到某个节点)

作者:互联网

跳转到任意节点实现(回退到某个节点)

需求:如下,执行到节点4的时候,我任务不符合要求,需要驳回/回退到节点1重新开始审批

image-20220705112119484

实现思路:

给每个节点设置唯一的id

image-20220705105948711

回退接口实现

 /**
     * 
     * @param taskId 当前任务id
     * @param nodeId 回退的节点id
     */
    @GetMapping("/backToEveryNode")
    @ApiOperation(value = "完成当前任务  并   回退到任意节点")
    public void backToEveryNode(String taskId,String nodeId) {


        //当前任务信息
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();

        //获得当前流程的活动ID
        ExecutionQuery executionQuery = runtimeService.createExecutionQuery();
        Execution execution = executionQuery.executionId(task.getExecutionId()).singleResult();
        String activityId = execution.getActivityId();


        //获取当前节点和目标节点的节点信息
        FlowNode currentFlowNode = getFlowNode(task.getProcessDefinitionId(), activityId);
        FlowNode targetFlowNode = getFlowNode(task.getProcessDefinitionId(), nodeId);

        //获取当前节点的原始流出节点信息
        List<SequenceFlow> oldFlowNodeOutgoingFlows = currentFlowNode.getOutgoingFlows();


        //重新设置流出节点信息  为退回的节点
        SequenceFlow sequenceFlow = new SequenceFlow();
        sequenceFlow.setId("newTempSequenceId");
        sequenceFlow.setSourceFlowElement(currentFlowNode);
        sequenceFlow.setTargetFlowElement(targetFlowNode);//设置流出节点信息  为退回的节点
        List<SequenceFlow> newSequenceFlows = new ArrayList<>();
        newSequenceFlows.add(sequenceFlow);
        currentFlowNode.setOutgoingFlows(newSequenceFlows);

        //完成当前任务
        taskService.complete(taskId);

        //最后恢复原来的流转走向
        currentFlowNode.setOutgoingFlows(oldFlowNodeOutgoingFlows);
    }

    private FlowNode getFlowNode(String processDefinitionId, String activityId) {
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        return (FlowNode) bpmnModel.getMainProcess().getFlowElement(activityId);
    }

标签:currentFlowNode,String,当前任务,taskId,跳转,回退,节点
来源: https://www.cnblogs.com/cutesheep/p/16445793.html