package cn.iocoder.yudao.module.mes.service.pro.feedback.listener;
|
|
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEvent;
|
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEventListener;
|
import cn.iocoder.yudao.module.mes.service.pro.feedback.MesProFeedbackService;
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants;
|
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
|
import org.flowable.task.api.history.HistoricTaskInstance;
|
import jakarta.annotation.Resource;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Comparator;
|
import java.util.List;
|
|
/**
|
* 生产报工审批状态监听器
|
*
|
* 监听 BPM 流程实例状态变更事件,更新生产报工的审批状态
|
*
|
* @author 超级管理员
|
*/
|
@Component
|
@Slf4j
|
public class MesProFeedbackStatusListener extends BpmProcessInstanceStatusEventListener {
|
|
/**
|
* BPM 生产报工审批流程定义 key
|
*/
|
private static final String BPM_PROCESS_DEFINITION_KEY = "mes-pro-feedback-audit";
|
|
@Resource
|
private MesProFeedbackService feedbackService;
|
|
@Resource
|
private BpmTaskService bpmTaskService;
|
|
@Override
|
protected String getProcessDefinitionKey() {
|
return BPM_PROCESS_DEFINITION_KEY;
|
}
|
|
@Override
|
protected void onEvent(BpmProcessInstanceStatusEvent event) {
|
Long feedbackId;
|
try {
|
feedbackId = Long.parseLong(event.getBusinessKey());
|
} catch (NumberFormatException e) {
|
log.warn("[onEvent] businessKey({}) 不是有效的生产报工编号", event.getBusinessKey());
|
return;
|
}
|
String reason = event.getReason();
|
if (StrUtil.isEmpty(reason) && 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)
|
.max(Comparator.comparing(HistoricTaskInstance::getEndTime))
|
.orElse(null);
|
if (lastTask != null && lastTask.getTaskLocalVariables() != null) {
|
reason = (String) lastTask.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_REASON);
|
}
|
}
|
}
|
|
log.info("[onEvent] 收到BPM审批事件,feedbackId={}, status={},Reason={}", feedbackId, event.getStatus(), reason);
|
|
feedbackService.handleBpmProcessResult(feedbackId, event.getStatus(), reason);
|
}
|
|
}
|