yangys
16 小时以前 833e909fbb25dd1245ec6aabb0c2cbe4c72d31c7
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
 
package org.springblade.mdm.flow.service;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.*;
 
import org.springblade.mdm.basesetting.machine.MachineService;
import org.springblade.mdm.flow.entity.MdmFlowProcess;
import org.springblade.mdm.flow.util.MdmFlowCache;
import org.springblade.mdm.flow.vo.FlowVO;
import org.springblade.mdm.program.entity.ProcessProgRef;
import org.springblade.mdm.program.service.ProcessProgRefService;
import org.springblade.system.feign.IUserClient;
import org.springblade.system.pojo.entity.User;
import org.springframework.stereotype.Service;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 流程业务实现类
 *
 * @author Chill
 */
@Service
@AllArgsConstructor
public class FlowBusinessService {
    private final RuntimeService runtimeService;
    private final TaskService taskService;
    private final HistoryService historyService;
    private final IUserClient userClient;
    private final ProcessProgRefService processProgRefService;
    private final ApproveRecordService approveRecordService;
 
    /**
     * 查询我的流程(个人待办列表)
     * @param page
     * @param createTimeBegin
     * @param createTimeEnd
     * @param keyword
     * @return
     */
    public IPage<FlowVO> selectTodoPage(IPage<FlowVO> page, LocalDateTime createTimeBegin, LocalDateTime createTimeEnd, String keyword) {
        //String taskUser = TaskUtil.getTaskUser();
        String userId = "" + AuthUtil.getUserId();
        List<FlowVO> flowList = new LinkedList<>();
 
        TaskQuery todoQuery = taskService.createTaskQuery().taskAssignee(userId).active().includeProcessVariables();
        if (Func.isNotEmpty(createTimeBegin)) {
            todoQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin));
 
            ////如果查询实例的开始时间:只能用以下这个,先查出实例id来
            //todoQuery.processInstanceIdIn()
        }
        if (Func.isNotEmpty(createTimeEnd)) {
            todoQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd));
            //todoQuery.taskInProgressStartTimeBefore(DateUtil.toDate(createTimeEnd));
        }
        if (Func.isNotEmpty(keyword)) {
            addKeywordCondition(todoQuery, keyword);
        }
 
        todoQuery.orderByTaskCreateTime().desc();
 
        // 构建列表数据
        FlowVO bladeFlow = new FlowVO();
        buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO , "todo"
        flowList.forEach(flowVO -> {
            if(flowVO.getVariables().containsKey("machineCode")){
                //Machine machinemachineService.getByCode((String)flowVO.getVariables().get("machineCode"))
            }
        });
        // 计算总数
        long count = todoQuery.count();
        // 设置页数
        page.setSize(count);
        // 设置总数
        page.setTotal(count);
        // 设置数据
        page.setRecords(flowList);
        return page;
    }
 
    void addKeywordCondition(TaskQuery todoQuery,String keyword) {
        if(Func.isNotEmpty(keyword)) {
            todoQuery.or();
 
            todoQuery.processVariableValueLike("processNo", "%" + keyword + "%");
            todoQuery.processVariableValueLike("machineCode", "%" + keyword + "%");
            todoQuery.processVariableValueLike("machineMode", "%" + keyword + "%");
            todoQuery.processVariableValueLike("processName", "%" + keyword + "%");
            todoQuery.processVariableValueLike("processEdition", "%" + keyword + "%");
            todoQuery.processVariableValueLike("craftEdition", "%" + keyword + "%");
 
            todoQuery.endOr();
        }
    }
    /*
    @Override
    public IPage<BladeFlow> selectSendPage(IPage<BladeFlow> page, BladeFlow bladeFlow) {
        String taskUser = TaskUtil.getTaskUser();
        List<BladeFlow> flowList = new LinkedList<>();
 
        HistoricProcessInstanceQuery historyQuery = historyService.createHistoricProcessInstanceQuery().startedBy(taskUser).orderByProcessInstanceStartTime().desc();
 
        if (bladeFlow.getCategory() != null) {
            historyQuery.processDefinitionCategory(bladeFlow.getCategory());
        }
        if (bladeFlow.getProcessDefinitionName() != null) {
            historyQuery.processDefinitionName(bladeFlow.getProcessDefinitionName());
        }
        if (bladeFlow.getBeginDate() != null) {
            historyQuery.startedAfter(bladeFlow.getBeginDate());
        }
        if (bladeFlow.getEndDate() != null) {
            historyQuery.startedBefore(bladeFlow.getEndDate());
        }
 
        // 查询列表
        List<HistoricProcessInstance> historyList = historyQuery.listPage(Func.toInt((page.getCurrent() - 1) * page.getSize()), Func.toInt(page.getSize()));
 
        historyList.forEach(historicProcessInstance -> {
            BladeFlow flow = new BladeFlow();
            // historicProcessInstance
            flow.setCreateTime(historicProcessInstance.getStartTime());
            flow.setEndTime(historicProcessInstance.getEndTime());
            flow.setVariables(historicProcessInstance.getProcessVariables());
            String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
            if (businessKey.length > 1) {
                flow.setBusinessTable(businessKey[0]);
                flow.setBusinessId(businessKey[1]);
            }
            flow.setHistoryActivityName(historicProcessInstance.getName());
            flow.setProcessInstanceId(historicProcessInstance.getId());
            flow.setHistoryProcessInstanceId(historicProcessInstance.getId());
            // ProcessDefinition
            FlowProcess processDefinition = FlowCache.getProcessDefinition(historicProcessInstance.getProcessDefinitionId());
            flow.setProcessDefinitionId(processDefinition.getId());
            flow.setProcessDefinitionName(processDefinition.getName());
            flow.setProcessDefinitionVersion(processDefinition.getVersion());
            flow.setProcessDefinitionKey(processDefinition.getKey());
            flow.setCategory(processDefinition.getCategory());
            flow.setCategoryName(FlowCache.getCategoryName(processDefinition.getCategory()));
            flow.setProcessInstanceId(historicProcessInstance.getId());
            // HistoricTaskInstance
            List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().processInstanceId(historicProcessInstance.getId()).orderByHistoricTaskInstanceEndTime().desc().list();
            if (Func.isNotEmpty(historyTasks)) {
                HistoricTaskInstance historyTask = historyTasks.iterator().next();
                flow.setTaskId(historyTask.getId());
                flow.setTaskName(historyTask.getName());
                flow.setTaskDefinitionKey(historyTask.getTaskDefinitionKey());
            }
            // Status
            if (historicProcessInstance.getEndActivityId() != null) {
                flow.setProcessIsFinished(FlowEngineConstant.STATUS_FINISHED);
            } else {
                flow.setProcessIsFinished(FlowEngineConstant.STATUS_UNFINISHED);
            }
            flow.setStatus(FlowEngineConstant.STATUS_FINISH);
            flowList.add(flow);
        });
 
        // 计算总数
        long count = historyQuery.count();
        // 设置总数
        page.setTotal(count);
        page.setRecords(flowList);
        return page;
    }
 
    @Override
    public IPage<BladeFlow> selectDonePage(IPage<BladeFlow> page, BladeFlow bladeFlow) {
        String taskUser = TaskUtil.getTaskUser();
        List<BladeFlow> flowList = new LinkedList<>();
 
        HistoricTaskInstanceQuery doneQuery = historyService.createHistoricTaskInstanceQuery().taskAssignee(taskUser).finished()
            .includeProcessVariables().orderByHistoricTaskInstanceEndTime().desc();
 
        if (bladeFlow.getCategory() != null) {
            doneQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory()));
        }
        if (bladeFlow.getProcessDefinitionName() != null) {
            doneQuery.processDefinitionName(bladeFlow.getProcessDefinitionName());
        }
        if (bladeFlow.getBeginDate() != null) {
            doneQuery.taskCompletedAfter(bladeFlow.getBeginDate());
        }
        if (bladeFlow.getEndDate() != null) {
            doneQuery.taskCompletedBefore(bladeFlow.getEndDate());
        }
 
        // 查询列表
        List<HistoricTaskInstance> doneList = doneQuery.listPage(Func.toInt((page.getCurrent() - 1) * page.getSize()), Func.toInt(page.getSize()));
        doneList.forEach(historicTaskInstance -> {
            BladeFlow flow = new BladeFlow();
            flow.setTaskId(historicTaskInstance.getId());
            flow.setTaskDefinitionKey(historicTaskInstance.getTaskDefinitionKey());
            flow.setTaskName(historicTaskInstance.getName());
            flow.setAssignee(historicTaskInstance.getAssignee());
            flow.setCreateTime(historicTaskInstance.getCreateTime());
            flow.setExecutionId(historicTaskInstance.getExecutionId());
            flow.setHistoryTaskEndTime(historicTaskInstance.getEndTime());
            flow.setVariables(historicTaskInstance.getProcessVariables());
 
            FlowProcess processDefinition = FlowCache.getProcessDefinition(historicTaskInstance.getProcessDefinitionId());
            flow.setProcessDefinitionId(processDefinition.getId());
            flow.setProcessDefinitionName(processDefinition.getName());
            flow.setProcessDefinitionKey(processDefinition.getKey());
            flow.setProcessDefinitionVersion(processDefinition.getVersion());
            flow.setCategory(processDefinition.getCategory());
            flow.setCategoryName(FlowCache.getCategoryName(processDefinition.getCategory()));
 
            flow.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
            flow.setHistoryProcessInstanceId(historicTaskInstance.getProcessInstanceId());
            HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance((historicTaskInstance.getProcessInstanceId()));
            if (Func.isNotEmpty(historicProcessInstance)) {
                String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
                flow.setBusinessTable(businessKey[0]);
                flow.setBusinessId(businessKey[1]);
                if (historicProcessInstance.getEndActivityId() != null) {
                    flow.setProcessIsFinished(FlowEngineConstant.STATUS_FINISHED);
                } else {
                    flow.setProcessIsFinished(FlowEngineConstant.STATUS_UNFINISHED);
                }
            }
            flow.setStatus(FlowEngineConstant.STATUS_FINISH);
            flowList.add(flow);
        });
        // 计算总数
        long count = doneQuery.count();
        // 设置总数
        page.setTotal(count);
        page.setRecords(flowList);
        return page;
    }
 
    */
    /**
     * 构建流程
     *
     * @param bladeFlow 流程通用类
     * @param flowList  流程列表
     * @param taskQuery 任务查询类
     */
    private void buildFlowTaskList(FlowVO bladeFlow, List<FlowVO> flowList, TaskQuery taskQuery) {
        if (bladeFlow.getCategory() != null) {
            taskQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory()));
        }
        if (bladeFlow.getProcessDefinitionName() != null) {
            taskQuery.processDefinitionName(bladeFlow.getProcessDefinitionName());
        }
        if (bladeFlow.getBeginDate() != null) {
            taskQuery.taskCreatedAfter(bladeFlow.getBeginDate());
        }
        if (bladeFlow.getEndDate() != null) {
            taskQuery.taskCreatedBefore(bladeFlow.getEndDate());
        }
 
        List<Task> tasks = taskQuery.list();
        tasks.forEach(task -> {
            FlowVO flow = new FlowVO();
            flow.setTaskId(task.getId());
            flow.setTaskDefinitionKey(task.getTaskDefinitionKey());
            flow.setTaskName(task.getName());
            flow.setAssignee(task.getAssignee());
            flow.setCreateTime(task.getCreateTime());
            flow.setClaimTime(task.getClaimTime());
            flow.setExecutionId(task.getExecutionId());
            flow.setVariables(task.getProcessVariables());
            flow.setDueDate(task.getDueDate());
 
            flow.setProcessDefinitionId(task.getProcessDefinitionId());
 
            //flow.setProcessDefinitionKey(processDefinition.getKey());
            //flow.setProcessDefinitionVersion(processDefinition.getVersion());
            flow.setProcessInstanceId(task.getProcessInstanceId());
 
            // 查询流程实例创建时间
            HistoricProcessInstance historicProcess = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(task.getProcessInstanceId())
                .singleResult();
            flow.setProcessCreateTime(historicProcess.getStartTime());
 
            R<User> ru = userClient.userInfoById(Long.valueOf(historicProcess.getStartUserId()));
            if(ru.isSuccess()) {
                flow.setStartUserName(ru.getData().getName());
            }
            ;
            List<Comment> comments = lastStepComments(task);//taskService.getTaskComments(task.getId());
            if(!comments.isEmpty()){
                flow.setComment(comments.get(0).getFullMessage());
            }
 
            MdmFlowProcess processDefinition = MdmFlowCache.getProcessDefinition(task.getProcessDefinitionId());
 
            flow.setCategory(processDefinition.getCategory());
            flow.setCategoryName(MdmFlowCache.getCategoryName(processDefinition.getCategory()));
            flow.setProcessDefinitionId(processDefinition.getId());
            flow.setProcessDefinitionName(processDefinition.getName());
            flow.setProcessDefinitionKey(processDefinition.getKey());
            flow.setProcessDefinitionVersion(processDefinition.getVersion());
            flow.setProcessInstanceId(task.getProcessInstanceId());
            //flow.setStatus(status);
 
            flowList.add(flow);
        });
    }
 
    List<Comment> lastStepComments(Task currentTask){
        List<HistoricTaskInstance> previousTasks = historyService.createHistoricActivityInstanceQuery()
            .processInstanceId(currentTask.getProcessInstanceId())
            .activityType("userTask") // 直接查询用户任务类型
            .orderByHistoricActivityInstanceEndTime()
            .desc()
            .list()
            .stream()
            .filter(activity -> !activity.getActivityId().equals(currentTask.getTaskDefinitionKey()))
            .map(activity -> historyService.createHistoricTaskInstanceQuery()
                .taskDefinitionKey(activity.getActivityId())
                .processInstanceId(activity.getProcessInstanceId())
                .orderByHistoricTaskInstanceEndTime()
                .desc()
                .list()
            )
            .flatMap(List::stream)
            .toList();
        List<Comment> comments;
        if (!previousTasks.isEmpty()) {
            return  taskService.getTaskComments(previousTasks.get(0).getId());
 
        }else{
            return Collections.emptyList();
        }
    }
    /**
     * 获取历史流程
     *
     * @param processInstanceId 流程实例id
     * @return HistoricProcessInstance
     */
    private HistoricProcessInstance getHistoricProcessInstance(String processInstanceId) {
        return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    }
 
    public IPage<FlowVO> selectAllTaskPage(IPage<FlowVO> page, String keyword) {
 
        List<FlowVO> flowList = new LinkedList<>();
 
        TaskQuery todoQuery = taskService.createTaskQuery().active().includeProcessVariables();
 
 
        addKeywordCondition(todoQuery, keyword);
 
        todoQuery.orderByTaskCreateTime().desc();
 
        // 构建列表数据
        FlowVO bladeFlow = new FlowVO();
        buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO
 
        // 计算总数
        long count = todoQuery.count();
        // 设置页数
        page.setSize(count);
        // 设置总数
        page.setTotal(count);
        // 设置数据
        page.setRecords(flowList);
        return page;
    }
 
    /**
     * 超时任务查询
     * @param page 分页信息
     * @param keyword 关键字
     * @return 分页数据
     */
    public IPage<FlowVO> selectOvertimePage(IPage<FlowVO> page, LocalDate createTimeBegin, LocalDate createTimeEnd, String assigneeName, String keyword) {
 
        List<FlowVO> flowList = new LinkedList<>();
        Date now = new Date();
        TaskQuery todoQuery = taskService.createTaskQuery().taskDueBefore(now).active().includeProcessVariables();
        if(Func.isNotEmpty(createTimeBegin)) {
            todoQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin));
        }
        if(Func.isNotEmpty(createTimeEnd)) {
            todoQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd.plusDays(1)));
        }
 
        if(Func.isNotEmpty(keyword)) {
            todoQuery.taskVariableValueLike("assigneeName", "%"+assigneeName+"%");
        }
 
        addKeywordCondition(todoQuery, keyword);
 
        todoQuery.orderByTaskCreateTime().desc();
 
        // 构建列表数据
        FlowVO bladeFlow = new FlowVO();
        buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO
 
        // 计算总数
        long count = todoQuery.count();
        // 设置页数
        page.setSize(count);
        // 设置总数
        page.setTotal(count);
        // 设置数据
        page.setRecords(flowList);
        return page;
    }
 
    /**
     * 流程查询 功能 的分页
     * @param page
     * @param createTimeBegin
     * @param createTimeEnd
     * @param keyword
     * @return
     */
    public IPage<FlowVO> selectSearchPage(IPage<FlowVO> page, LocalDate createTimeBegin, LocalDate createTimeEnd, String keyword) {
 
        List<FlowVO> flowList = new LinkedList<>();
        Date now = new Date();
        TaskQuery taskQuery = taskService.createTaskQuery().active().includeProcessVariables();
        if(Func.isNotEmpty(createTimeBegin)) {
            taskQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin));
        }
        if(Func.isNotEmpty(createTimeEnd)) {
            taskQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd.plusDays(1)));
        }
 
        addKeywordCondition(taskQuery, keyword);
 
        taskQuery.orderByTaskCreateTime().desc();
 
        // 构建列表数据
        FlowVO bladeFlow = new FlowVO();
        buildFlowTaskList(bladeFlow, flowList, taskQuery);//FlowEngineConstant.STATUS_TODO
 
        // 计算总数
        long count = taskQuery.count();
        // 设置页数
        page.setSize(count);
        // 设置总数
        page.setTotal(count);
        // 设置数据
        page.setRecords(flowList);
        return page;
    }
 
    public void deleteProcessInstance(String processInstancesId) {
        runtimeService.deleteProcessInstance(processInstancesId, "流程查询-删除");
        //TODO 删除出关联字段
    }
}