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
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
package org.springblade.mdm.flow.service.execute;
 
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.mdm.commons.service.UserCommonService;
import org.springblade.mdm.flow.constants.FlowConstant;
import org.springblade.mdm.flow.constants.FlowVariableConstant;
import org.springblade.mdm.flow.service.FlowCommonService;
import org.springblade.mdm.flow.vo.BatchDispatchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Map;
 
/**
 * 试切流程完成任务的实现
 */
@Service
public class BatchDispatchService  {
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private FlowCommonService flowCommonService;
    @Autowired
    private UserCommonService userCommonService;
    @Transactional
    public void batchDispatchTask(BatchDispatchVO batchDispatchVO) {
        //String[] taskIds, String[] processInstanceIds, String comment, String assignee
        if(StringUtils.isBlank(batchDispatchVO.getAssignee())){
            throw new ServiceException("缺少处理人参数");
        }
        checkBatchParameter(batchDispatchVO);
 
        Map<String, Object> variables = Kv.create();
        variables.put("assignee", batchDispatchVO.getAssignee());
        variables.put("approve", FlowConstant.Y);//默认就是通过
        String taskId;
        String processInstanceId;
        String comment = batchDispatchVO.getComment();
        String[] taskIds = batchDispatchVO.getTaskIds();
        for(int i=0;i<taskIds.length;i++){
            taskId = taskIds[i];
            processInstanceId = batchDispatchVO.getProcessInstanceIds()[i];
 
            runtimeService.setVariable(processInstanceId, FlowConstant.PROGRAMMER_NAME,userCommonService.getUserNameById(Func.toLong(batchDispatchVO.getAssignee())));
 
            Task task = getTask(taskId);
 
            if(!task.getTaskDefinitionKey().equals("teamLeaderTask")){
                throw new ServiceException("非[任务分派]节点的任务不能批量派工");
            }
            if (StringUtil.isNoneBlank(processInstanceId, comment)) {
                taskService.addComment(taskId, processInstanceId, comment);
            }
 
            taskService.complete(taskId, variables);
        }
    }
 
    /**
     * 自动批量派工
     * @param batchDispatchVO 批量处理参数
     */
    @Transactional
    public void batchAutoDispatchTask(BatchDispatchVO batchDispatchVO) {
        //TODO 待实现
        checkBatchParameter(batchDispatchVO);
        String[] taskIds = batchDispatchVO.getTaskIds();
        String taskId;
        String processInstanceId;
 
        Map<String, Object> variables = Kv.create();
        //variables.put("assignee", batchDispatchVO.getAssignee());
        variables.put("approve", FlowConstant.Y);//自动分派默认就是通过
        variables.put(FlowVariableConstant.COMMENT,batchDispatchVO.getComment());
        ProcessInstance processInstance;
        Object programmer;
        for(int i=0;i<taskIds.length;i++) {
            taskId = taskIds[i];
            processInstanceId = batchDispatchVO.getProcessInstanceIds()[i];
 
            Task task = getTask(taskId);
            if(!task.getTaskDefinitionKey().equals("teamLeaderTask")){
                throw new ServiceException("非[任务分派]节点的任务不能批量派工");
            }
 
            Object programmerId = getAutoProgrammer(processInstanceId);
            variables.put("assignee", programmerId);
 
            runtimeService.setVariable(processInstanceId, FlowConstant.PROGRAMMER_NAME,userCommonService.getUserNameById(Func.toLong(programmerId)));
 
 
            if (StringUtil.isNoneBlank(processInstanceId, batchDispatchVO.getComment())) {
                taskService.addComment(taskId, processInstanceId, batchDispatchVO.getComment());
            }
 
            //最后一步完成
            taskService.complete(taskId, variables);
        }
    }
 
    private Task getTask(String taskId) {
        return taskService.createTaskQuery()
            .taskId(taskId)
            .singleResult();
    }
 
    /**
     * 获取编程的工艺员id
     * @param processInstanceId
     * @return
     */
    Object getAutoProgrammer(String processInstanceId){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables()
            .processInstanceId(processInstanceId)
            .singleResult();
        Object programmer = processInstance.getProcessVariables().get(FlowVariableConstant.PROGRAMMER);
        if(programmer == null){
            throw new ServiceException(processInstance.getProcessVariables().get(FlowVariableConstant.TITLE)+":未找到编程人员");
        }
        return programmer;
    }
 
    /**
     * 高师批量审批
     * @param batchDispatchVO
     */
    @Transactional
    public void batchApprove(BatchDispatchVO batchDispatchVO) {
        checkBatchParameter(batchDispatchVO);
        String[] taskIds = batchDispatchVO.getTaskIds();
        String taskId;
        String processInstanceId;
 
        //审批结果和处理人都一样的
        Map<String, Object> variables = Kv.create();
        variables.put(FlowVariableConstant.APPROVE, batchDispatchVO.getApprove());
        variables.put(FlowVariableConstant.COMMENT, batchDispatchVO.getComment());
 
        for(int i=0;i<taskIds.length;i++) {
            taskId = taskIds[i];
            processInstanceId = batchDispatchVO.getProcessInstanceIds()[i];
 
            Task task = getTask(taskId);
 
            if (StringUtil.isNoneBlank(processInstanceId, batchDispatchVO.getComment())) {
                taskService.addComment(taskId, processInstanceId, batchDispatchVO.getComment());
            }
            if(FlowConstant.N.equals(batchDispatchVO.getApprove())) {
                //驳回,查询实际编程人员 驳回
                variables.put(FlowVariableConstant.ASSIGNEE, getActualProgrammer(processInstanceId));
            }
 
            taskService.complete(taskId, variables);
        }
    }
 
    /**
     * 获取流程执行中实际的编程人员(执行上传文件的人)
     * @param processInstanceId 流程实例id
     * @return 编程人员id
     */
    Object getActualProgrammer(String processInstanceId){
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables()
            .processInstanceId(processInstanceId)
            .singleResult();
        Object actProgrammer = processInstance.getProcessVariables().get(FlowVariableConstant.ACT_PROGRAMMER);
        if(actProgrammer == null){
            throw new ServiceException(processInstance.getProcessVariables().get(FlowVariableConstant.TITLE)+":未找到编程人员");
        }
        return actProgrammer;
    }
    /**
     * 验证批量处理参数
     * @param batchDispatchVO 批量处理参数对象
     */
    void checkBatchParameter(BatchDispatchVO batchDispatchVO){
        String[] taskIds = batchDispatchVO.getTaskIds();
        if(taskIds == null || taskIds.length == 0){
            throw new ServiceException("任务id组为空");
        }
        String[] processInstanceIds = batchDispatchVO.getProcessInstanceIds();
        if(processInstanceIds == null || processInstanceIds.length == 0){
            throw new ServiceException("流程实例id组为空");
        }
        if(taskIds.length != processInstanceIds.length){
            throw new ServiceException("任务id与流程实例id数量不一致");
        }
    }
 
    /**
     * 排练通过现场编制
     * @param batchDispatchVO
     */
    public void batchProgramOnMachine(BatchDispatchVO batchDispatchVO) {
        checkBatchParameter(batchDispatchVO);
        String[] taskIds = batchDispatchVO.getTaskIds();
        String taskId;
        String processInstanceId;
 
        //审批结果和处理人都一样的
        Map<String, Object> variables = Kv.create();
        variables.put(FlowVariableConstant.APPROVE, FlowConstant.Y);
        variables.put(FlowVariableConstant.COMMENT, batchDispatchVO.getComment());
        variables.put(FlowVariableConstant.PROGRAM_ON_MACHINE, FlowConstant.Y);//现场编制:是
        variables.put(FlowVariableConstant.ACT_PROGRAMMER, AuthUtil.getUserId()+"");//实际编制
        variables.put(FlowVariableConstant.ASSIGNEE,batchDispatchVO.getAssignee());
        for(int i=0;i<taskIds.length;i++) {
            taskId = taskIds[i];
            processInstanceId = batchDispatchVO.getProcessInstanceIds()[i];
 
            Task task = getTask(taskId);
            if(!task.getTaskDefinitionKey().equals("programmingTask")){
                throw new ServiceException("非编制任务,不能指定现场编制");
            }
            if (StringUtil.isNoneBlank(processInstanceId, batchDispatchVO.getComment())) {
                taskService.addComment(taskId, processInstanceId, batchDispatchVO.getComment());
            }
 
            taskService.complete(taskId, variables);
        }
    }
}