需求
在springboot整合activiti6实现审批的同时,如果申请申请人提交申请之后,中间审批人误批,需要实现撤销审批!!!
代码
@ResponseBody
@PostMapping("/chexiao")
@Transactional
public String chexiao(String sid) throws Exception {
//上面的sid只是我的业务需要,可不用管
List<suppliesinoutTodoItem> suppliesinouttodoitem = outdoorTodoItemService.selectBizTodoItemBySId1(sid);
//此处的taskid我是通过业务查出来的,指的是提交人提交或者审批人审批之后产生的任务id
String taskid = null;
if(suppliesinouttodoitem.size() > 0){
taskid = suppliesinouttodoitem.get(0).getTaskId();
}
// System.out.println(taskid);
//通过taskid查询出当前的任务
Task task = taskService.createTaskQuery().taskId(taskid).singleResult();
if (task == null) {
//throw new ServiceException("流程未启动或已执行完成,无法撤回");
return "500";
}else{
// List<HistoricTaskInstance> htlist = historyService.createHistoricTaskInstanceQuery()
// .processDefinitionId(task.getProcessDefinitionId()).list();
//通过instancedid查询历史
List<HistoricTaskInstance> htlist = historyService.createHistoricTaskInstanceQuery().processInstanceId(task.getProcessInstanceId()).list();
String myTaskId = null;
HistoricTaskInstance myTask = null;
String uname = ShiroUtils.getLoginName();
for (HistoricTaskInstance hti : htlist) {
//此处的uname表示的是节点对应的处理人名
if ((hti.getAssignee() != null) && hti.getAssignee().equals(uname)) {
myTaskId = hti.getId();
myTask = hti;
break;
}
}
if (myTask == null) {
throw new ServiceException("该任务非当前用户提交,无法撤回");
}
String processDefinitionId = myTask.getProcessDefinitionId();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
String myActivityId = null;
List<HistoricActivityInstance> haiList =
historyService
.createHistoricActivityInstanceQuery()
.executionId(myTask.getExecutionId())
.finished()
.list();
for (HistoricActivityInstance hai : haiList) {
if (myTaskId.equals(hai.getTaskId())) {
myActivityId = hai.getActivityId();
break;
}
}
FlowNode myFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(myActivityId);
Execution execution = runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();
String activityId = execution.getActivityId();
System.out.println(activityId);
FlowNode flowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(activityId);
//记录原活动方向
List<SequenceFlow> oriSequenceFlows = new ArrayList<SequenceFlow>();
oriSequenceFlows.addAll(flowNode.getOutgoingFlows());
//清理活动方向
flowNode.getOutgoingFlows().clear();
//建立新方向
List<SequenceFlow> newSequenceFlowList = new ArrayList<SequenceFlow>();
SequenceFlow newSequenceFlow = new SequenceFlow();
newSequenceFlow.setId("newSequenceFlowId");
newSequenceFlow.setSourceFlowElement(flowNode);
newSequenceFlow.setTargetFlowElement(myFlowNode);
newSequenceFlowList.add(newSequenceFlow);
flowNode.setOutgoingFlows(newSequenceFlowList);
Authentication.setAuthenticatedUserId(ShiroUtils.getLoginName());
taskService.addComment(task.getId(), task.getProcessInstanceId(), "撤回");
//完成任务
List<Task> taskList = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).active().list();
taskService.complete(task.getId());
//撤销的节点任务
List<Task> taskList1 = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).active().list();
//需要处理biz里面的数据
//删除撤销之前提交之后产生的数据
String aaa = taskList.get(0).getId();
outdoorTodoItemService.deleteBizTodoItemBytaskid(aaa);
List<suppliesinoutTodoItem> suppliesinouttodoitem1 = outdoorTodoItemService.selectBizTodoItemBySId1(sid);
suppliesinoutTodoItem sm = suppliesinouttodoitem1.get(0);
sm.setTaskId(taskList1.get(0).getId());
sm.setTaskName(taskList1.get(0).getTaskDefinitionKey());
sm.setNodeName(taskList1.get(0).getName());
sm.setIsView("0");
sm.setIsHandle("0");
sm.setHandleUserId(null);
sm.setHandleUserName(null);
sm.setHandleTime(null);
outdoorTodoItemService.updateBizTodoItem(sm);
//恢复原方向
flowNode.setOutgoingFlows(oriSequenceFlows);
return "200";
}
}
}
大佬勿喷,欢迎提意见建议评论!!!!