diff --git a/src/main/java/com/example/demo/controller/ACBackController.java b/src/main/java/com/example/demo/controller/ACBackController.java new file mode 100644 index 0000000..849601c --- /dev/null +++ b/src/main/java/com/example/demo/controller/ACBackController.java @@ -0,0 +1,172 @@ +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.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(); + } + } +} diff --git a/src/main/java/com/example/demo/controller/WorkFlowController.java b/src/main/java/com/example/demo/controller/WorkFlowController.java index 536e61e..8a93b57 100644 --- a/src/main/java/com/example/demo/controller/WorkFlowController.java +++ b/src/main/java/com/example/demo/controller/WorkFlowController.java @@ -10,13 +10,13 @@ import org.activiti.bpmn.converter.UserTaskXMLConverter; import org.activiti.bpmn.model.*; import org.activiti.bpmn.model.Process; import org.activiti.editor.language.json.converter.BpmnJsonConverter; -import org.activiti.engine.ProcessEngine; -import org.activiti.engine.RepositoryService; -import org.activiti.engine.RuntimeService; -import org.activiti.engine.TaskService; +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.repository.DeploymentBuilder; import org.activiti.engine.repository.Model; +import org.activiti.engine.runtime.Execution; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.apache.commons.lang3.StringUtils; @@ -173,4 +173,77 @@ public class WorkFlowController { taskService.complete(taskid); return true; } + + @Autowired + private HistoryService historyService; + + @Autowired + private RuntimeService runtimeService; + + public void backProcess(Task task) throws Exception { + String processInstanceId = task.getProcessInstanceId(); + // 取得所有历史任务按时间降序排序 + 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()); + //恢复原方向 + curFlowNode.setOutgoingFlows(oriSequenceFlows); + org.activiti.engine.task.Task nextTask = taskService + .createTaskQuery().processInstanceId(processInstanceId).singleResult(); + // 设置执行人 + if(nextTask!=null) { + taskService.setAssignee(nextTask.getId(), lastTask.getAssignee()); + } + } } diff --git a/src/main/resources/bpmn/acback.bpmn b/src/main/resources/bpmn/acback.bpmn new file mode 100644 index 0000000..0fbd54e --- /dev/null +++ b/src/main/resources/bpmn/acback.bpmn @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testt1.bpmn b/src/main/resources/bpmn/testt1.bpmn deleted file mode 100644 index 8097c58..0000000 --- a/src/main/resources/bpmn/testt1.bpmn +++ /dev/null @@ -1,145 +0,0 @@ - - - - - - - - - - - - - - - - - =7}]]> - - - - - =3}]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/bpmn/testt1.xml b/src/main/resources/bpmn/testt1.xml deleted file mode 100644 index d5c85b6..0000000 --- a/src/main/resources/bpmn/testt1.xml +++ /dev/null @@ -1,145 +0,0 @@ - - - - - - - - - - - - - - - - - =7}]]> - - - - - =3 && num<7}]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -