gaoluyang
6 天以前 7bd10d34e8a8dc2437ec8551eb511e37634d7306
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
package cn.iocoder.yudao.module.mes.service.pro.mps.listener;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEvent;
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEventListener;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants;
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
import cn.iocoder.yudao.module.mes.service.pro.mps.MesProMpsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.stereotype.Component;
 
import java.util.Comparator;
import java.util.List;
 
/**
 * 生产主计划(MPS)审批状态监听器
 *
 * 监听 BPM 流程实例状态变更事件,把协同办公的审批结果回写到 MPS 状态
 *
 * @author 超级管理员
 */
@Component
@Slf4j
public class MesProMpsStatusListener extends BpmProcessInstanceStatusEventListener {
 
    /**
     * MPS 审批流程定义 Key
     */
    private static final String BPM_PROCESS_DEFINITION_KEY = "mes-pro-mps-audit";
 
    @Resource
    private MesProMpsService mpsService;
 
    @Resource
    private BpmTaskService bpmTaskService;
 
    @Override
    protected String getProcessDefinitionKey() {
        return BPM_PROCESS_DEFINITION_KEY;
    }
 
    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        Long mpsId;
        try {
            mpsId = Long.parseLong(event.getBusinessKey());
        } catch (NumberFormatException e) {
            log.warn("[onEvent] businessKey({}) 不是有效的生产计划编号", event.getBusinessKey());
            return;
        }
        String reason = event.getReason();
        Long auditUserId = null;
        if (event.getId() != null) {
            // 当 event.getReason() 为空时,尝试从最后一个完成的任务中获取审批意见与审批人
            List<HistoricTaskInstance> tasks = bpmTaskService.getTaskListByProcessInstanceId(event.getId(), false);
            if (CollUtil.isNotEmpty(tasks)) {
                HistoricTaskInstance lastTask = tasks.stream()
                        .filter(task -> task.getEndTime() != null)
                        // 排除多实例或签满足条件后,被系统自动取消的兄弟任务(deleteReason 非空)
                        .filter(task -> task.getDeleteReason() == null)
                        .max(Comparator.comparing(HistoricTaskInstance::getEndTime))
                        .orElse(null);
                if (lastTask != null) {
                    if (StrUtil.isEmpty(reason) && lastTask.getTaskLocalVariables() != null) {
                        reason = (String) lastTask.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_REASON);
                    }
                    if (StrUtil.isNotEmpty(lastTask.getAssignee())) {
                        try {
                            auditUserId = Long.parseLong(lastTask.getAssignee());
                        } catch (NumberFormatException ignored) {
                            log.warn("[onEvent] 审批人({}) 不是有效的用户编号", lastTask.getAssignee());
                        }
                    }
                }
            }
        }
 
        log.info("[onEvent] 收到BPM审批事件,mpsId={}, status={}, reason={}, auditUserId={}",
                mpsId, event.getStatus(), reason, auditUserId);
        mpsService.handleBpmProcessResult(mpsId, event.getStatus(), reason, auditUserId);
    }
 
}