yangys
2025-09-18 0d61b9bfca526e9c3da2209de8f9f367e76fd013
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
package org.springblade.mdm.flow.service;
 
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.flowable.engine.RepositoryService;
import java.util.HashMap;
import java.util.Optional;
 
/**
 * 流程撤回服务
 */
 
 
@Service
public class WIthdrawService {
    @Autowired
    private WithdrawProperties withdrawProps;
    @Autowired
    private RuntimeService runtimeService;
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private FlowableBackWithAssigneeService backWithAssigneeService;
    @Autowired
    private RepositoryService repositoryService;
    /*
     * 撤回到编程员
     */
    @Transactional
    public void withdraw(String processInstanceId) {
        //System.out.println(withdrawProps);
 
        String targetAssignee = ""+AuthUtil.getUserId();
 
        String processDefKey = getProcessDefinitionKeyByProcessInstanceId(processInstanceId);
        String targetActivityId = this.withdrawProps.getMapping().get(processDefKey);
        String taskId = getCurrentTaskIdByProcessInstanceId(processInstanceId);
        backWithAssigneeService.backToTaskWithNewAssignee(taskId,targetActivityId,targetAssignee,"流程撤回",new HashMap<>());
    }
 
 
    public String getCurrentTaskIdByProcessInstanceId(String processInstanceId) {
        Optional<String> optTaskId = taskService.createTaskQuery()
            .processInstanceId(processInstanceId)
            .active()
            .list()
            .stream()
            .map(Task::getId).findFirst();
 
 
        return optTaskId.orElse(null);
    }
 
    public String getProcessDefinitionKeyByProcessInstanceId(String processInstanceId) {
        // 获取流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
            .processInstanceId(processInstanceId)
            .singleResult();
 
        if (processInstance == null) {
            throw new RuntimeException("流程实例不存在: " + processInstanceId);
        }
 
        // 获取流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
            .processDefinitionId(processInstance.getProcessDefinitionId())
            .singleResult();
 
        if (processDefinition == null) {
            throw new RuntimeException("流程定义不存在: " + processInstance.getProcessDefinitionId());
        }
 
        return processDefinition.getKey();
    }
 
    /**
     * 取回到组长派工
     * @param processInstanceId
     */
    public void takeBackToDispatch(String processInstanceId) {
        String targetAssignee = ""+AuthUtil.getUserId();
 
        final String targetActivityId = "teamLeaderTask";//组长派工
        String taskId = getCurrentTaskIdByProcessInstanceId(processInstanceId);
        backWithAssigneeService.backToTaskWithNewAssignee(taskId,targetActivityId,targetAssignee,"组长取回",new HashMap<>());
    }
}