刘光辉
14 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package jnpf.workflow.flowable.service;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.stereotype.Service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import jnpf.workflow.common.exception.BizException;
import jnpf.workflow.common.exception.ResultCode;
import jnpf.workflow.common.model.fo.CompensateFo;
import jnpf.workflow.common.model.fo.FlowTargetTaskFo;
import jnpf.workflow.common.model.fo.InfoModel;
import jnpf.workflow.common.model.fo.InstanceStartFo;
import jnpf.workflow.common.model.fo.JumpFo;
import jnpf.workflow.common.model.fo.MoveMultiToSingleFo;
import jnpf.workflow.common.model.fo.MoveSingleToMultiFo;
import jnpf.workflow.common.model.fo.TaskAfterFo;
import jnpf.workflow.common.model.fo.TaskBackFo;
import jnpf.workflow.common.model.fo.TaskCompleteFo;
import jnpf.workflow.common.model.fo.TaskNextFo;
import jnpf.workflow.common.model.fo.TaskOutgoingFo;
import jnpf.workflow.common.model.fo.TaskPrevFo;
import jnpf.workflow.common.model.vo.FlowVo;
import jnpf.workflow.common.model.vo.HistoricNodeVo;
import jnpf.workflow.common.model.vo.InstanceVo;
import jnpf.workflow.common.model.vo.NodeElementVo;
import jnpf.workflow.common.model.vo.TaskVo;
import jnpf.workflow.common.service.IInstanceService;
import jnpf.workflow.common.service.ITaskService;
import jnpf.workflow.flowable.cmd.JumpCmd;
import jnpf.workflow.flowable.util.FlowableUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class TaskServiceImpl implements ITaskService {
 
    private final TaskService taskService;
    private final HistoryService historyService;
    private final RuntimeService runtimeService;
    private final RepositoryService repositoryService;
    private final ManagementService managementService;
    private final IInstanceService instanceService;
 
    @Override
    public List<TaskVo> getTask(String instanceId) {
        List<Task> list = taskService.createTaskQuery().processInstanceId(instanceId).list();
        List<TaskVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Task task : list) {
                TaskVo vo = new TaskVo();
                vo.setTaskId(task.getId());
                vo.setTaskName(task.getName());
                vo.setTaskKey(task.getTaskDefinitionKey());
                vo.setInstanceId(task.getProcessInstanceId());
                vos.add(vo);
            }
        }
        return vos;
    }
 
    @Override
    public boolean complete(TaskCompleteFo fo) {
        Task task = taskService.createTaskQuery().taskId(fo.getTaskId()).singleResult();
        if (null == task) {
            throw new BizException(ResultCode.TASK_NOT_EXIST);
        }
        try {
            if (CollectionUtil.isNotEmpty(fo.getVariables())) {
                taskService.complete(fo.getTaskId(), fo.getVariables());
            } else {
                taskService.complete(fo.getTaskId());
            }
            return true;
        } catch (Exception e) {
            log.error(ResultCode.TASK_COMPLETE_ERROR.getMsg(), e);
            return false;
        }
    }
 
    @Override
    public boolean moveSingleToMulti(MoveSingleToMultiFo fo) {
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(fo.getInstanceId()).singleResult();
        if (null == instance) {
            throw new BizException(ResultCode.INSTANCE_NOT_EXIST);
        }
        try {
            moveSingleActivityIdToActivityIds(fo.getInstanceId(), fo.getSourceKey(), fo.getTargetKeys());
            return true;
        } catch (Exception e) {
            log.error(ResultCode.TASK_JUMP_ERROR.getMsg(), e);
            return false;
        }
    }
 
    public void moveSingleActivityIdToActivityIds(String processInstanceId, String activityId, List<String> activityIds) {
        runtimeService.createChangeActivityStateBuilder()
                .processInstanceId(processInstanceId)
                .moveSingleActivityIdToActivityIds(activityId, activityIds)
                .changeState();
    }
 
    @Override
    public boolean moveMultiToSingle(MoveMultiToSingleFo fo) {
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(fo.getInstanceId()).singleResult();
        if (null == instance) {
            throw new BizException(ResultCode.INSTANCE_NOT_EXIST);
        }
        try {
            moveActivityIdsToSingleActivityId(fo.getInstanceId(), fo.getSourceKeys(), fo.getTargetKey());
            return true;
        } catch (Exception e) {
            log.error(ResultCode.TASK_JUMP_ERROR.getMsg(), e);
            return false;
        }
    }
 
    public void moveActivityIdsToSingleActivityId(String processInstanceId, List<String> activityIds, String activityId) {
        runtimeService.createChangeActivityStateBuilder()
                .processInstanceId(processInstanceId)
                .moveActivityIdsToSingleActivityId(activityIds, activityId)
                .changeState();
    }
 
    @Override
    public boolean jump(JumpFo fo) {
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(fo.getInstanceId()).singleResult();
        if (null == instance) {
            throw new BizException(ResultCode.INSTANCE_NOT_EXIST);
        }
        try {
            BpmnModel bpmnModel = repositoryService.getBpmnModel(instance.getProcessDefinitionId());
            JumpCmd jumpCmd = new JumpCmd(fo.getInstanceId(), fo.getSource(), fo.getTarget(), "custom jump", bpmnModel, runtimeService);
            managementService.executeCommand(jumpCmd);
            return true;
        } catch (Exception e) {
            log.error(ResultCode.TASK_JUMP_ERROR.getMsg(), e);
            return false;
        }
    }
 
    @Override
    public List<String> getFallbacks(String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (null == task) {
            throw new BizException(ResultCode.TASK_NOT_EXIST);
        }
        FlowElement source = getFlowElement(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
        List<String> list = FlowableUtil.getPassActs(source, null, null);
        return list.stream().distinct().collect(Collectors.toList());
    }
 
    public FlowElement getFlowElement(String processDefinitionId, String taskDefinitionKey) {
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
        if (null == definition) {
            throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
        }
        Process process = repositoryService.getBpmnModel(definition.getId()).getProcesses().get(0);
        Collection<FlowElement> elements = FlowableUtil.getAllElements(process.getFlowElements(), null);
        FlowElement source = null;
        if (null != elements && !elements.isEmpty()) {
            for (FlowElement element : elements) {
                if (!element.getId().equals(taskDefinitionKey)) {
                    continue;
                }
                source = element;
            }
        }
        return source;
    }
 
    @Override
    public List<String> back(TaskBackFo fo) {
        String instanceId;
        String definitionId;
        Task task = taskService.createTaskQuery().taskId(fo.getTaskId()).singleResult();
        if (null == task) {
            HistoricTaskInstance historicTask = historyService.createHistoricTaskInstanceQuery().taskId(fo.getTaskId()).singleResult();
            definitionId = historicTask.getProcessDefinitionId();
            instanceId = historicTask.getProcessInstanceId();
        } else {
            definitionId = task.getProcessDefinitionId();
            instanceId = task.getProcessInstanceId();
        }
        if (StrUtil.isNotBlank(fo.getTargetKey())) {
            String[] split = fo.getTargetKey().split(",");
            if (split.length == 0) {
                throw new BizException("目标节点编码不能为空");
            }
            List<String> list = Arrays.asList(split);
            return back(definitionId, instanceId, list);
        }
        return null;
    }
 
    public List<String> back(String definitionId, String instanceId, List<String> targetList) {
        List<String> currentIds = new ArrayList<>();
        for (String targetKey : targetList) {
            FlowElement target = getFlowElement(definitionId, targetKey);
            List<Task> runTaskList = taskService.createTaskQuery().processInstanceId(instanceId).list();
            List<String> runTaskKeyList = runTaskList.stream().map(TaskInfo::getTaskDefinitionKey).collect(Collectors.toList());
            List<UserTask> userTaskList = FlowableUtil.getChildUserTasks(target, runTaskKeyList, null, null);
            List<String> collect = userTaskList.stream().map(BaseElement::getId).collect(Collectors.toList());
            currentIds.addAll(collect);
        }
        currentIds = currentIds.stream().distinct().collect(Collectors.toList());
        JumpFo jumpFo = new JumpFo();
        jumpFo.setInstanceId(instanceId);
        jumpFo.setSource(currentIds);
        jumpFo.setTarget(targetList);
        jump(jumpFo);
        return currentIds;
    }
 
    @Override
    public List<String> getPrevUserTask(TaskPrevFo fo) {
        List<String> list;
        if (StrUtil.isNotBlank(fo.getDeploymentId())) {
            ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(fo.getDeploymentId()).singleResult();
            if (null == definition) {
                throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
            }
            FlowElement source = getFlowElement(definition.getId(), fo.getTaskKey());
            list = FlowableUtil.getParentActs(source, null, null);
        } else {
            Task task = taskService.createTaskQuery().taskId(fo.getTaskId()).singleResult();
            if (null == task) {
                throw new BizException(ResultCode.TASK_NOT_EXIST);
            }
            FlowElement source = getFlowElement(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
            list = FlowableUtil.getParentActs(source, null, null);
        }
        return list;
    }
 
    @Override
    public List<NodeElementVo> getNextUserTask(TaskNextFo fo) {
        List<UserTask> nextUserTasks;
        FlowElement source;
        if (StrUtil.isNotBlank(fo.getDeploymentId())) {
            ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(fo.getDeploymentId()).singleResult();
            if (null == definition) {
                throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
            }
            source = getFlowElement(definition.getId(), fo.getTaskKey());
            nextUserTasks = FlowableUtil.getNextUserTasks(source, null, null);
        } else {
            HistoricTaskInstance taskInst = historyService.createHistoricTaskInstanceQuery().taskId(fo.getTaskId()).singleResult();
            if (null == taskInst) {
                throw new BizException(ResultCode.TASK_NOT_EXIST);
            }
            source = getFlowElement(taskInst.getProcessDefinitionId(), taskInst.getTaskDefinitionKey());
            nextUserTasks = FlowableUtil.getNextUserTasks(source, null, null);
        }
        List<NodeElementVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(nextUserTasks)) {
            for (UserTask userTask : nextUserTasks) {
                NodeElementVo vo = new NodeElementVo();
                vo.setId(userTask.getId());
                vo.setName(userTask.getName());
                vo.setIncomingList(userTask.getIncomingFlows().stream().map(BaseElement::getId).collect(Collectors.toList()));
                vo.setOutgoingList(userTask.getOutgoingFlows().stream().map(BaseElement::getId).collect(Collectors.toList()));
                vos.add(vo);
            }
        }
        return vos;
    }
 
    @Override
    public List<String> getTaskKeyAfterFlow(FlowTargetTaskFo fo) {
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(fo.getDeploymentId()).singleResult();
        if (null == definition) {
            throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
        }
        List<String> list = new ArrayList<>();
        FlowElement source = getFlowElement(definition.getId(), fo.getFlowKey());
        String taskKey = FlowableUtil.getTaskKeyAfterFlow(source);
        list.add(taskKey);
        return list;
    }
 
    @Override
    public boolean retract(String taskId) {
        HistoricTaskInstance taskInst = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
        if (null != taskInst) {
            ProcessInstance procInst = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(taskInst.getProcessInstanceId()).active().singleResult();
            if (null != procInst) {
                FlowElement source = getFlowElement(taskInst.getProcessDefinitionId(), taskInst.getTaskDefinitionKey());
                List<UserTask> nextUserTasks = FlowableUtil.getNextUserTasks(source, null, null);
                List<String> nextUserTaskKeys = nextUserTasks.stream().map(BaseElement::getId).collect(Collectors.toList());
                List<Task> activateTasks = taskService.createTaskQuery().processInstanceId(taskInst.getProcessInstanceId()).list();
                List<String> currentIds = new ArrayList<>();
                for (Task task : activateTasks) {
                    if (!CollUtil.contains(nextUserTaskKeys, task.getTaskDefinitionKey())) {
                        continue;
                    }
                    currentIds.add(task.getTaskDefinitionKey());
                }
                moveActivityIdsToSingleActivityId(taskInst.getProcessInstanceId(), currentIds, taskInst.getTaskDefinitionKey());
                return true;
            }
        }
        return false;
    }
 
    @Override
    public List<String> getOutgoingFlows(TaskOutgoingFo fo) {
        if (StrUtil.isNotBlank(fo.getDeploymentId())) {
            ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(fo.getDeploymentId()).singleResult();
            if (null == definition) {
                throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
            }
            return getOutgoingFlows(definition.getId(), fo.getTaskKey());
        }
        Task task = taskService.createTaskQuery().taskId(fo.getTaskId()).singleResult();
        if (null == task) {
            throw new BizException(ResultCode.TASK_NOT_EXIST);
        }
        return getOutgoingFlows(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
    }
 
    public List<String> getOutgoingFlows(String processDefinitionId, String taskDefinitionKey) {
        FlowElement source = getFlowElement(processDefinitionId, taskDefinitionKey);
        List<SequenceFlow> flows = new ArrayList<>();
        flows = FlowableUtil.getOutFlowsWithGateway(source, flows);
        List<String> list = new ArrayList<>();
        if (!flows.isEmpty()) {
            for (SequenceFlow flow : flows) {
                list.add(flow.getId());
            }
        }
        return list.stream().distinct().collect(Collectors.toList());
    }
 
    @Override
    public List<FlowVo> getOutgoing(TaskOutgoingFo fo) {
        if (StrUtil.isNotBlank(fo.getDeploymentId())) {
            ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(fo.getDeploymentId()).singleResult();
            if (null == definition) {
                throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
            }
            return getOutgoing(definition.getId(), fo.getTaskKey());
        }
        Task task = taskService.createTaskQuery().taskId(fo.getTaskId()).singleResult();
        if (null == task) {
            throw new BizException(ResultCode.TASK_NOT_EXIST);
        }
        return getOutgoing(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
    }
 
    public List<FlowVo> getOutgoing(String processDefinitionId, String taskDefinitionKey) {
        FlowElement source = getFlowElement(processDefinitionId, taskDefinitionKey);
        return FlowableUtil.getOutFlows(source, null);
    }
 
    @Override
    public List<String> getKeysOfFinished(String instanceId) {
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
        if (null == instance) {
            throw new BizException(ResultCode.INSTANCE_NOT_EXIST);
        }
        List<String> keysOfBefore = getKeysOfBefore(instance);
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(instanceId).finished().list();
        if (CollectionUtil.isNotEmpty(list)) {
            List<String> keysOfFinished = list.stream()
                    .filter(e -> !"sequenceFlow".equals(e.getActivityType()))
                    .sorted(Comparator.comparing(HistoricActivityInstance::getStartTime))
                    .map(HistoricActivityInstance::getActivityId)
                    .distinct()
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(keysOfBefore)) {
                keysOfFinished.retainAll(keysOfBefore);
            }
            return keysOfFinished;
        }
        return null;
    }
 
    public List<String> getKeysOfBefore(ProcessInstance instance) {
        List<String> res = new ArrayList<>();
        List<Task> taskList = taskService.createTaskQuery().processInstanceId(instance.getId()).list();
        if (CollectionUtil.isNotEmpty(taskList)) {
            List<String> keys = taskList.stream().map(TaskInfo::getTaskDefinitionKey).collect(Collectors.toList());
            for (String key : keys) {
                FlowElement source = getFlowElement(instance.getProcessDefinitionId(), key);
                List<String> list = FlowableUtil.getBefore(source, null, null);
                res.addAll(list);
            }
        }
        return res.stream().distinct().collect(Collectors.toList());
    }
 
    @Override
    public List<String> getIncomingFlows(String taskId) {
        HistoricTaskInstance taskInst = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
        if (null != taskInst) {
            FlowElement source = getFlowElement(taskInst.getProcessDefinitionId(), taskInst.getTaskDefinitionKey());
            List<SequenceFlow> flows = FlowableUtil.getElementIncomingFlows(source);
            return flows.stream().map(BaseElement::getId).collect(Collectors.toList());
        }
        return null;
    }
 
    @Override
    public List<String> getToBePass(String instanceId) {
        List<String> list = new ArrayList<>();
        List<Task> currentList = taskService.createTaskQuery().processInstanceId(instanceId).list();
        if (CollectionUtil.isNotEmpty(currentList)) {
            List<String> collect = currentList.stream().map(TaskInfo::getTaskDefinitionKey).collect(Collectors.toList());
            for (Task task : currentList) {
                FlowElement source = getFlowElement(task.getProcessDefinitionId(), task.getTaskDefinitionKey());
                List<String> after = FlowableUtil.getAfter(source, null, null);
                list.addAll(after);
            }
            list = list.stream().filter(e -> !collect.contains(e)).collect(Collectors.toList());
        }
        return list.stream().distinct().collect(Collectors.toList());
    }
 
    @Override
    public List<String> getAfter(TaskAfterFo fo) {
        String deploymentId = fo.getDeploymentId();
        List<String> taskKeys = fo.getTaskKeys();
        List<String> list = new ArrayList<>();
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        if (null == definition) {
            throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
        }
        for (String taskKey : taskKeys) {
            FlowElement source = getFlowElement(definition.getId(), taskKey);
            List<String> after = FlowableUtil.getAfter(source, null, null);
            list.addAll(after);
        }
        return list.stream().distinct().collect(Collectors.toList());
    }
 
    @Override
    public List<TaskVo> compensate(CompensateFo fo) {
        String instanceId = fo.getInstanceId();
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
        if (null == instance) {
            HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery()
                    .processInstanceId(instanceId).singleResult();
            Map<String, Object> variables = new HashMap<>();
            List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
                    .processInstanceId(instanceId).list();
            for (HistoricVariableInstance var : list) {
                variables.put(var.getVariableName(), var.getValue());
            }
            String deploymentId = historicInstance.getDeploymentId();
            InstanceStartFo startFo = new InstanceStartFo();
            startFo.setDeploymentId(deploymentId);
            startFo.setVariables(variables);
            InstanceVo instanceVo = instanceService.startById(startFo);
            instanceId = instanceVo.getInstanceId();
        }
        List<String> sourceList = fo.getSource().stream().sorted().collect(Collectors.toList());
        List<TaskVo> taskVoList = getTask(instanceId);
        List<String> currentList = taskVoList.stream().map(TaskVo::getTaskKey).sorted().collect(Collectors.toList());
        List<String> createList = sourceList.stream().filter(e -> !currentList.contains(e)).collect(Collectors.toList());
        List<String> deleteList = currentList.stream().filter(e -> !sourceList.contains(e)).collect(Collectors.toList());
        JumpFo jumpFo = new JumpFo();
        jumpFo.setInstanceId(instanceId);
        jumpFo.setSource(deleteList);
        jumpFo.setTarget(createList);
        jump(jumpFo);
        List<TaskVo> vos = getTask(instanceId);
        if (null != instance) {
            vos.forEach(e -> e.setInstanceId(null));
        }
        return vos;
    }
 
    @Override
    public List<HistoricNodeVo> getHistoric(String instanceId) {
        Set<String> set = new HashSet<>();
        set.add("userTask");
        set.add("startEvent");
        List<HistoricNodeVo> vos = getHistoricVos(instanceId, set);
        return vos.stream().sorted(Comparator.comparing(HistoricNodeVo::getStartTime)).collect(Collectors.toList());
    }
 
    @Override
    public List<String> getHistoricEnd(String instanceId) {
        List<String> list = new ArrayList<>();
        List<HistoricNodeVo> vos = getHistoricVos(instanceId, null);
        if (CollectionUtil.isNotEmpty(vos)) {
            list = vos.stream().map(HistoricNodeVo::getCode).distinct().collect(Collectors.toList());
        }
        return list;
    }
 
    public List<HistoricNodeVo> getHistoricVos(String instanceId, Set<String> set) {
        List<HistoricNodeVo> vos = new ArrayList<>();
        if (CollectionUtil.isEmpty(set)) {
            set = new HashSet<>();
            set.add("endEvent");
        }
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .activityTypes(set).processInstanceId(instanceId).list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (HistoricActivityInstance act : list) {
                HistoricNodeVo vo = new HistoricNodeVo();
                vo.setCode(act.getActivityId());
                vo.setTaskId(act.getTaskId());
                vo.setStartTime(act.getStartTime().getTime());
                vos.add(vo);
            }
        }
        return vos;
    }
 
    @Override
    public NodeElementVo getElementInfo(InfoModel model) {
        String deploymentId = model.getDeploymentId();
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        if (null == definition) {
            throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
        }
        String definitionId = definition.getId();
        String key = model.getKey();
        NodeElementVo vo = new NodeElementVo();
        FlowElement source = getFlowElement(definitionId, key);
        if (null != source) {
            vo.setId(source.getId());
            List<SequenceFlow> outgoingFlows = FlowableUtil.getElementOutgoingFlows(source);
            vo.setOutgoingList(outgoingFlows.stream().map(BaseElement::getId).collect(Collectors.toList()));
            List<SequenceFlow> incomingFlows = FlowableUtil.getElementIncomingFlows(source);
            vo.setIncomingList(incomingFlows.stream().map(BaseElement::getId).collect(Collectors.toList()));
        }
        return vo;
    }
}