天天看點

actviti7撤回操作

@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);
    }      

繼續閱讀