fuliqi
2025-01-03 329b1cb2b08a4043af1262872bb8de88cf665219
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
package com.ycl.listener.flowable;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ycl.common.utils.spring.SpringUtils;
import com.ycl.domain.entity.ProcessCoding;
import com.ycl.factory.FlowServiceFactory;
import com.ycl.mapper.ProcessCodingMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.el.FixedValue;
import org.flowable.engine.HistoryService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.util.List;
 
import static com.ycl.common.constant.ProcessOverTimeConstants.GREEN;
 
/**
 * 任务监听器
 * <p>
 * create(创建):在任务被创建且所有的任务属性设置完成后才触发
 * assignment(指派):在任务被分配给某个办理人之后触发
 * complete(完成):在配置了监听器的上一个任务完成时触发
 * delete(删除):在任务即将被删除前触发。请注意任务由completeTask正常完成时也会触发
 *
 * @author Tony
 * @date 2021/4/20
 */
@Slf4j
@Component
public class FlowableOverTimeListener implements TaskListener {
    /**
     * 黄码时间
     */
    private FixedValue yellowTime;
    /**
     * 红码时间
     */
    private FixedValue redTime;
    /**
     * 计时起始节点定义Id
     */
    private FixedValue startTaskId;
 
    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("触发超时监听器:{}", delegateTask);
        //Flowable的bean自己管理的需要手动获取
        ProcessCodingMapper processCodingMapper = SpringUtils.getBean(ProcessCodingMapper.class);
        HistoryService historyService = SpringUtils.getBean(HistoryService.class);
        //任务id
        String taskId = delegateTask.getId();
        //流程实例id
        String processInstanceId = delegateTask.getProcessInstanceId();
        ProcessCoding processCoding = new ProcessCoding();
        processCoding.setTaskId(taskId);
        processCoding.setProcessInsId(processInstanceId);
        processCoding.setTaskDefKey(delegateTask.getTaskDefinitionKey());
        processCoding.setStatus(GREEN);
        if (yellowTime != null && yellowTime.getValue(delegateTask) != null) {
            processCoding.setYellowTime(Integer.parseInt(yellowTime.getValue(delegateTask).toString()));
        }
        if (redTime != null && redTime.getValue(delegateTask) != null) {
            processCoding.setRedTime(Integer.parseInt(redTime.getValue(delegateTask).toString()));
        }
        //设置开始节点
        if (startTaskId != null && startTaskId.getValue(delegateTask) != null) {
            String taskDefKey = startTaskId.getValue(delegateTask).toString();
            //开始节点从历史节点查询 取多条中最早的一条
            List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .taskDefinitionKey(taskDefKey)
                    .orderByHistoricTaskInstanceStartTime()
                    .asc()
                    .list();
            if (!CollectionUtils.isEmpty(list)) {
                HistoricTaskInstance hisTask = list.get(0);
                processCoding.setStartTaskId(hisTask.getId());
            } else {
                //如果为空,说明流程首次走到taskDefKey节点
                processCoding.setStartTaskId(delegateTask.getId());
            }
        }
        processCodingMapper.insert(processCoding);
        log.info("添加节点到定时器");
    }
}