package com.example.demo.controller; import com.example.demo.entity.Holiday; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.activiti.api.process.runtime.ProcessRuntime; import org.activiti.api.task.runtime.TaskRuntime; import org.activiti.bpmn.model.BpmnModel; import org.activiti.bpmn.model.EndEvent; import org.activiti.bpmn.model.FlowNode; import org.activiti.bpmn.model.SequenceFlow; import org.activiti.engine.*; import org.activiti.engine.history.HistoricActivityInstance; import org.activiti.engine.history.HistoricTaskInstance; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.Execution; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author yanpeng * @version 1.0 * @desc TODO * @company 北京中经网软件有限公司 * @date 2020/10/27 14:34 */ @RestController @RequestMapping("acback") @Api(tags = "工作流驳回等") public class ACBackController { @Autowired private ProcessEngine processEngine; @Autowired private HistoryService historyService; @Autowired private RuntimeService runtimeService; @Autowired private RepositoryService repositoryService; @Autowired private TaskService taskService; @ApiOperation(value = "发布工作流") @GetMapping("deploy") public void deploy() { RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/acback.bpmn").name("测试流程回退").key("myProcess").deploy(); System.out.println(deploy.getId()); } @ApiOperation(value = "启动工作流") @GetMapping("start") public void start(@RequestParam Integer num) { // Holiday holiday = new Holiday(); // holiday.setNum(num); Map map = new HashMap<>(); map.put("num", num); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", map); System.out.println(processInstance.getProcessDefinitionId()); System.out.println(processInstance.getId()); } @ApiOperation(value = "查询任务") @GetMapping("query") public Boolean query() { List testbpmn = taskService.createTaskQuery().processDefinitionKey("myProcess").list(); testbpmn.stream().forEach(x -> { System.out.println(x.getProcessDefinitionId()); System.out.println(x.getProcessInstanceId()); System.out.println(x.getId()); System.out.println(x.getName()); }); return true; } @ApiOperation(value = "完成任务") @GetMapping("ok") public Boolean ok(String taskid) { TaskService taskService = processEngine.getTaskService(); taskService.complete(taskid); return true; } @ApiOperation(value = "完成任务") @GetMapping("back") public void backProcess(String processInstanceId, String taskId) { // String processInstanceId = task.getProcessInstanceId(); try { // 取得所有历史任务按时间降序排序 List htiList = historyService.createHistoricTaskInstanceQuery() .processInstanceId(processInstanceId) .orderByTaskCreateTime() .desc() .list(); if (htiList == null || htiList.size() < 2) { return; } // list里的第二条代表上一个任务 HistoricTaskInstance lastTask = htiList.get(1); // list里第一条代表当前任务 HistoricTaskInstance curTask = htiList.get(0); // 当前节点的executionId String curExecutionId = curTask.getExecutionId(); // 上个节点的taskId String lastTaskId = lastTask.getId(); // 上个节点的executionId String lastExecutionId = lastTask.getExecutionId(); if (null == lastTaskId) { throw new Exception("LAST TASK IS NULL"); } String processDefinitionId = lastTask.getProcessDefinitionId(); BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId); String lastActivityId = null; List haiFinishedList = historyService.createHistoricActivityInstanceQuery() .executionId(lastExecutionId).finished().list(); for (HistoricActivityInstance hai : haiFinishedList) { if (lastTaskId.equals(hai.getTaskId())) { // 得到ActivityId,只有HistoricActivityInstance对象里才有此方法 lastActivityId = hai.getActivityId(); break; } } // 得到上个节点的信息 FlowNode lastFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(lastActivityId); // 取得当前节点的信息 Execution execution = runtimeService.createExecutionQuery().executionId(curExecutionId).singleResult(); String curActivityId = execution.getActivityId(); FlowNode curFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(curActivityId); //记录当前节点的原活动方向 List oriSequenceFlows = new ArrayList<>(); oriSequenceFlows.addAll(curFlowNode.getOutgoingFlows()); //清理活动方向 curFlowNode.getOutgoingFlows().clear(); //建立新方向 List newSequenceFlowList = new ArrayList<>(); SequenceFlow newSequenceFlow = new SequenceFlow(); newSequenceFlow.setId("newSequenceFlowId"); newSequenceFlow.setSourceFlowElement(curFlowNode); newSequenceFlow.setTargetFlowElement(lastFlowNode); newSequenceFlowList.add(newSequenceFlow); curFlowNode.setOutgoingFlows(newSequenceFlowList); // 完成任务 // taskService.complete(task.getId()); taskService.complete(taskId); //恢复原方向 curFlowNode.setOutgoingFlows(oriSequenceFlows); Task nextTask = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult(); // 设置执行人 if (nextTask != null) { taskService.setAssignee(nextTask.getId(), lastTask.getAssignee()); } System.out.println("退回上一步成功"); } catch (Exception e) { e.printStackTrace(); } } @ApiOperation(value = "驳回任务") @GetMapping("stop") public void stop(String taskId) { try { Task task = taskService.createTaskQuery().taskId(taskId).singleResult(); String processDefinitionId = task.getProcessDefinitionId(); BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId); // 得到结束节点的信息 FlowNode stopFlowNode = bpmnModel.getMainProcess().findFlowElementsOfType(EndEvent.class).get(0); // 取得当前节点的信息 // Execution execution = runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult(); // String curActivityId = execution.getActivityId(); FlowNode curFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(task.getTaskDefinitionKey()); //记录当前节点的原活动方向 List outgoingFlows = new ArrayList<>(); outgoingFlows.addAll(curFlowNode.getOutgoingFlows()); //清理活动方向 curFlowNode.getOutgoingFlows().clear(); //建立新方向 List newSequenceFlowList = new ArrayList<>(); SequenceFlow newSequenceFlow = new SequenceFlow(); newSequenceFlow.setId("newSequenceFlowId"); newSequenceFlow.setSourceFlowElement(curFlowNode); newSequenceFlow.setTargetFlowElement(stopFlowNode); newSequenceFlowList.add(newSequenceFlow); curFlowNode.setOutgoingFlows(newSequenceFlowList); // 完成任务 taskService.complete(taskId); //恢复原方向 curFlowNode.setOutgoingFlows(outgoingFlows); System.out.println("驳回停止成功"); } catch (Exception e) { e.printStackTrace(); } } }