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

时间:2025-03-19 10:57:05
/** * @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 = (); // 退回的目标节点(当前用户已办理的最新节点) 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); // ().clear(); List<SequenceFlow> oldOutgoingFlows = currentFlowNode.getOutgoingFlows(); // 将当前节点的向外连线设置成创建的连线 currentFlowNode.setOutgoingFlows(List.of(sequenceFlow4)); task.setAssignee(userName); taskService.complete(task.getId()); // 完成任务 currentFlowNode.setOutgoingFlows(oldOutgoingFlows); // 完成任务后将原来的连线设置回当前任务节点 }