activiti7实现流程撤回的两种思路

一、使用BpmnModel

/**
* @param processInstanceBusinessKey BUSINESS_KEY_
* @param userName 当前用户
**/
public void rollBackToAssignWoekFlow(String processInstanceBusinessKey, String userName){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(processInstanceBusinessKey).singleResult();
        if(processInstance == null) {
            throw new CustomException("撤回失败,失败原因:未查到流程信息!");
        }

        String processInstanceId = processInstance.getProcessInstanceId();

        //获取流程模型
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
        if (null == bpmnModel) {
            throw new CustomException("撤回失败,失败原因:未查到流程信息!");
        }

        Process process = bpmnModel.getProcesses().get(0);

        // 当前待办的任务(激活的任务)
        org.activiti.engine.task.Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();

        // 获取当前节点
        FlowNode currentFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(task.getTaskDefinitionKey());
        // List<SequenceFlow> outgoingFlows1 = currentFlowNode.getOutgoingFlows();

        // 退回的目标节点(当前用户已办理的最新节点)
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId)
                .taskAssignee(userName).orderByHistoricTaskInstanceEndTime().desc().list();
        HistoricTaskInstance historicTaskInstance = list.get(0);
        FlowNode targetFlowNode = (FlowNode) process.getFlowElement(historicTaskInstance.getTaskDefinitionKey());

        // 动态创建从当前任务节点到目标节点的连线
        SequenceFlow sequenceFlow4 = genarateSequenceFlow("test001","test-name",currentFlowNode,targetFlowNode,null,process);
        // currentFlowNode.getOutgoingFlows().clear();
        List<SequenceFlow> oldOutgoingFlows = currentFlowNode.getOutgoingFlows();
        // 将当前节点的向外连线设置成创建的连线
        currentFlowNode.setOutgoingFlows(List.of(sequenceFlow4));
        task.setAssignee(userName);
        taskService.complete(task.getId());    // 完成任务
        currentFlowNode.setOutgoingFlows(oldOutgoingFlows);    // 完成任务后将原来的连线设置回当前任务节点
    }

二、使用流程图设计撤回

在设计bpmn流程图时添加一个排他网关,使用表达式控制流程撤回(想到于退回功能)。

posted @ 2022-03-01 00:06  我爱这世间美貌女子  阅读(1626)  评论(1编辑  收藏  举报