@Override
@Transactional(rollbackFor = Exception.class)
public int callBack(String processId) {
//通过流程id查找到当前的任务
List<Task> tasks= taskService.createTaskQuery()
.processInstanceId(processId)
.list();
//如果为空或者小于等于0则证明没有可查看到的任务
if (tasks == null||tasks.size()<=0) {
System.out.println("当前没有任务或者任务已执行完了");
return -1;
}
//查看所有走过的历史任务
List<HistoricTaskInstance> htlist = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(processId)
.finished()
.list();
if (htlist == null||htlist.size()<=0) {
System.out.println("请先提交任务");
}
String myTaskId = null;
HistoricTaskInstance myTask = null;
//前一个任务节点,也就是提交人 拿到前一个任务
for (HistoricTaskInstance hti : htlist) {
//回退到zhangsan也就是任务A,业务中这里就是当前登录的用户名 TODO:从登录名拿
if (hti.getAssignee().equals("1")) {
myTaskId = hti.getId();
myTask = hti;
break;
}
}
if (myTask == null) {
System.out.println("这个任务不是你的任务");
}
//流程定义id
String processDefinitionId = myTask.getProcessDefinitionId();
//整个流程节点对象
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
//我的节点
String myActivityId = null;
//历史节点完成节点
List<HistoricActivityInstance> haiList =
historyService
.createHistoricActivityInstanceQuery()
.executionId(myTask.getExecutionId())
.finished()
.list();
//拿到我的节点 _3
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(tasks.get(0).getExecutionId()).singleResult();
//当任务执行节点_4
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("zhangsan");
taskService.addComment(tasks.get(0).getId(), tasks.get(0).getProcessInstanceId(), "撤回");
String taskId= tasks.get(0).getId();
//完成任务
if (tasks.size() > 1) {
//会签撤回
HashMap<String, Object> variables = new HashMap<>();
variables.put("pass",true);
// variables.put("param","y");
taskService.complete(taskId,variables);
//恢复原方向
flowNode.setOutgoingFlows(oriSequenceFlows);
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(tasks.get(0).getProcessInstanceId()).singleResult();
//更新数据库 从新设置state为未提交状态
Apply apply = new Apply();
apply.setCode(processInstance.getBusinessKey());
apply.setState(0);
return applyService.updateByCode(apply);
}
//普通撤回
taskService.complete(taskId);
//恢复原方向
flowNode.setOutgoingFlows(oriSequenceFlows);
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(tasks.get(0).getProcessInstanceId()).singleResult();
//更新数据库 从新设置state为未提交状态
Apply apply = new Apply();
apply.setCode(processInstance.getBusinessKey());
apply.setState(0);
return applyService.updateByCode(apply);
}
actviti7撤回操作