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);
|
}
|
|
}
|