houzhongjian
2024-12-03 874287a4c02d0a980d8b97c4a691b4f37ec5e812
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.framework.flowable.core.listener;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.util.StrUtil;
5 import com.iailab.module.bpm.service.task.BpmActivityService;
6 import com.iailab.module.bpm.service.task.BpmTaskService;
7 import com.google.common.collect.ImmutableSet;
8 import lombok.extern.slf4j.Slf4j;
9 import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
10 import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
11 import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
12 import org.flowable.engine.delegate.event.FlowableActivityCancelledEvent;
13 import org.flowable.engine.history.HistoricActivityInstance;
14 import org.flowable.task.api.Task;
15 import org.springframework.context.annotation.Lazy;
16 import org.springframework.stereotype.Component;
17
18 import javax.annotation.Resource;
19 import java.util.List;
20 import java.util.Set;
21
22 /**
23  * 监听 {@link Task} 的开始与完成
24  *
25  * @author jason
26  */
27 @Component
28 @Slf4j
29 public class BpmTaskEventListener extends AbstractFlowableEngineEventListener {
30
31     @Resource
32     @Lazy // 解决循环依赖
33     private BpmTaskService taskService;
34     @Resource
35     @Lazy // 解决循环依赖
36     private BpmActivityService activityService;
37
38     public static final Set<FlowableEngineEventType> TASK_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
39             .add(FlowableEngineEventType.TASK_CREATED)
40             .add(FlowableEngineEventType.TASK_ASSIGNED)
41 //            .add(FlowableEngineEventType.TASK_COMPLETED) // 由于审批通过时,已经记录了 task 的 status 为通过,所以不需要监听了。
42             .add(FlowableEngineEventType.ACTIVITY_CANCELLED)
43             .build();
44
45     public BpmTaskEventListener(){
46         super(TASK_EVENTS);
47     }
48
49     @Override
50     protected void taskCreated(FlowableEngineEntityEvent event) {
51         taskService.updateTaskStatusWhenCreated((Task) event.getEntity());
52     }
53
54     @Override
55     protected void taskAssigned(FlowableEngineEntityEvent event) {
56         taskService.updateTaskExtAssign((Task)event.getEntity());
57     }
58
59     @Override
60     protected void activityCancelled(FlowableActivityCancelledEvent event) {
61         List<HistoricActivityInstance> activityList = activityService.getHistoricActivityListByExecutionId(event.getExecutionId());
62         if (CollUtil.isEmpty(activityList)) {
63             log.error("[activityCancelled][使用 executionId({}) 查找不到对应的活动实例]", event.getExecutionId());
64             return;
65         }
66         // 遍历处理
67         activityList.forEach(activity -> {
68             if (StrUtil.isEmpty(activity.getTaskId())) {
69                 return;
70             }
71             taskService.updateTaskStatusWhenCanceled(activity.getTaskId());
72         });
73     }
74
75 }