refactor(ops): extract constants and optimize status list in audit handler
This commit is contained in:
@@ -8,7 +8,13 @@ import com.viewsh.module.ops.infrastructure.log.enumeration.LogType;
|
||||
import com.viewsh.module.ops.infrastructure.log.publisher.BusinessLogPublisher;
|
||||
import com.viewsh.module.iot.api.device.IotDeviceControlApi;
|
||||
import com.viewsh.module.iot.api.device.dto.IotDeviceServiceInvokeReqDTO;
|
||||
import com.viewsh.module.ops.dal.dataobject.workorder.OpsOrderDO;
|
||||
import com.viewsh.module.ops.dal.mysql.workorder.OpsOrderMapper;
|
||||
import com.viewsh.module.ops.enums.WorkOrderStatusEnum;
|
||||
import com.viewsh.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import jakarta.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.rocketmq.spring.annotation.ConsumeMode;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
@@ -52,6 +58,18 @@ public class CleanOrderAuditEventHandler implements RocketMQListener<String> {
|
||||
*/
|
||||
private static final int DEDUP_TTL_SECONDS = 300;
|
||||
|
||||
private static final String TRIGGER_SOURCE_QUERY = "IOT_BUTTON_QUERY";
|
||||
private static final String DEFAULT_AREA_NAME = "当前区域";
|
||||
private static final String TTS_TEMPLATE_QUERY = "当前位置:%s。待办工单:%d个";
|
||||
|
||||
private static final List<String> ACTIVE_STATUS_LIST = Arrays.asList(
|
||||
WorkOrderStatusEnum.QUEUED.getStatus(),
|
||||
WorkOrderStatusEnum.DISPATCHED.getStatus(),
|
||||
WorkOrderStatusEnum.CONFIRMED.getStatus(),
|
||||
WorkOrderStatusEnum.ARRIVED.getStatus(),
|
||||
WorkOrderStatusEnum.PAUSED.getStatus()
|
||||
);
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@@ -64,6 +82,9 @@ public class CleanOrderAuditEventHandler implements RocketMQListener<String> {
|
||||
@Resource
|
||||
private IotDeviceControlApi iotDeviceControlApi;
|
||||
|
||||
@Resource
|
||||
private OpsOrderMapper opsOrderMapper;
|
||||
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
try {
|
||||
@@ -96,6 +117,11 @@ public class CleanOrderAuditEventHandler implements RocketMQListener<String> {
|
||||
log.debug("[CleanOrderAuditEventHandler] 收到审计事件: eventId={}, auditType={}, message={}",
|
||||
event.getEventId(), event.getAuditType(), event.getMessage());
|
||||
|
||||
if (TRIGGER_SOURCE_QUERY.equals(event.getTriggerSource())) {
|
||||
handleQueryEvent(event);
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 确定日志级别和类型
|
||||
LogType logType = determineLogType(event.getAuditType());
|
||||
boolean isSuccess = determineSuccess(event.getAuditType());
|
||||
@@ -152,46 +178,83 @@ public class CleanOrderAuditEventHandler implements RocketMQListener<String> {
|
||||
* @param event 审计事件
|
||||
*/
|
||||
private void handleTtsRequest(CleanOrderAuditEventDTO event) {
|
||||
// 1. 从审计数据中提取 TTS 文本
|
||||
String ttsText = null;
|
||||
if (event.getData() != null && event.getData().containsKey("tts")) {
|
||||
ttsText = (String) event.getData().get("tts");
|
||||
}
|
||||
|
||||
if (ttsText == null || ttsText.isEmpty()) {
|
||||
log.warn("[CleanOrderAuditEventHandler] TTS 文本为空,跳过下发: eventId={}", event.getEventId());
|
||||
return;
|
||||
}
|
||||
|
||||
sendTts(event.getDeviceId(), ttsText);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理查询事件 (IOT_BUTTON_QUERY)
|
||||
*/
|
||||
private void handleQueryEvent(CleanOrderAuditEventDTO event) {
|
||||
log.info("[CleanOrderAuditEventHandler] Handling query event: eventId={}", event.getEventId());
|
||||
|
||||
Long deviceId = event.getDeviceId();
|
||||
if (deviceId == null) {
|
||||
log.warn("[CleanOrderAuditEventHandler] Query event missing deviceId: eventId={}", event.getEventId());
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 获取当前区域名称
|
||||
String areaName = DEFAULT_AREA_NAME;
|
||||
if (event.getOrderId() != null) {
|
||||
OpsOrderDO order = opsOrderMapper.selectById(event.getOrderId());
|
||||
if (order != null && order.getLocation() != null) {
|
||||
areaName = order.getLocation();
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 查询待办工单数量
|
||||
// status IN (QUEUED, DISPATCHED, CONFIRMED, ARRIVED, PAUSED)
|
||||
Long count = opsOrderMapper.selectCount(new LambdaQueryWrapperX<OpsOrderDO>()
|
||||
.eq(OpsOrderDO::getAssigneeDeviceId, deviceId)
|
||||
.in(OpsOrderDO::getStatus, ACTIVE_STATUS_LIST));
|
||||
|
||||
// 3. 构建 TTS 文本
|
||||
String ttsText = String.format(TTS_TEMPLATE_QUERY, areaName, count);
|
||||
|
||||
// 4. 下发 TTS
|
||||
sendTts(deviceId, ttsText);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发 TTS 语音播报
|
||||
*/
|
||||
private void sendTts(Long deviceId, String text) {
|
||||
try {
|
||||
// 1. 从审计数据中提取 TTS 文本
|
||||
String ttsText = null;
|
||||
if (event.getData() != null && event.getData().containsKey("tts")) {
|
||||
ttsText = (String) event.getData().get("tts");
|
||||
}
|
||||
|
||||
if (ttsText == null || ttsText.isEmpty()) {
|
||||
log.warn("[CleanOrderAuditEventHandler] TTS 文本为空,跳过下发: eventId={}", event.getEventId());
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 构建服务调用请求
|
||||
// 1. 构建参数
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("text", ttsText);
|
||||
params.put("text", text);
|
||||
params.put("volume", 80); // 默认音量 80%
|
||||
|
||||
IotDeviceServiceInvokeReqDTO reqDTO = IotDeviceServiceInvokeReqDTO.builder()
|
||||
.deviceId(event.getDeviceId())
|
||||
.deviceId(deviceId)
|
||||
.identifier("playVoice") // 语音播报服务标识符
|
||||
.params(params)
|
||||
.timeoutSeconds(30)
|
||||
.build();
|
||||
|
||||
// 3. 调用 IoT 模块 RPC 接口
|
||||
// 2. 调用 IoT 模块 RPC 接口
|
||||
var result = iotDeviceControlApi.invokeService(reqDTO);
|
||||
|
||||
if (result.getData() != null && result.getData().getSuccess()) {
|
||||
log.info("[CleanOrderAuditEventHandler] TTS 下发成功: eventId={}, deviceId={}, text={}",
|
||||
event.getEventId(), event.getDeviceId(), ttsText);
|
||||
log.info("[CleanOrderAuditEventHandler] TTS 下发成功: deviceId={}, text={}", deviceId, text);
|
||||
} else {
|
||||
log.warn("[CleanOrderAuditEventHandler] TTS 下发失败: eventId={}, deviceId={}, error={}",
|
||||
event.getEventId(), event.getDeviceId(),
|
||||
result.getData() != null ? result.getData().getErrorMsg() : "Unknown error");
|
||||
log.warn("[CleanOrderAuditEventHandler] TTS 下发失败: deviceId={}, error={}",
|
||||
deviceId, result.getData() != null ? result.getData().getErrorMsg() : "Unknown error");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("[CleanOrderAuditEventHandler] TTS 下发异常: eventId={}, deviceId={}",
|
||||
event.getEventId(), event.getDeviceId(), e);
|
||||
// TTS 失败不影响主流程,仅记录日志
|
||||
log.error("[CleanOrderAuditEventHandler] TTS 下发异常: deviceId={}", deviceId, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user