package com.xindazhou.message.mq.consumer;

import com.xindazhou.framework.mq.redis.core.stream.AbstractRedisStreamMessageListener;
import com.xindazhou.message.service.PushService;
import com.xindazhou.vehicle.api.dto.mq.VehicleAlarmMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

/**
 * 车辆报警消息监听器
 *
 * 监听来自车辆服务的报警消息，并推送给客户端
 *
 * @author xindazhou
 */
@Slf4j
@Component
public class VehicleAlarmMessageListener extends AbstractRedisStreamMessageListener<VehicleAlarmMessage> {

    @Resource
    private PushService pushService;

    public VehicleAlarmMessageListener() {
        // 无参构造函数，通过泛型自动推断消息类型
    }

    @Override
    public void onMessage(VehicleAlarmMessage message) {
        log.info("收到车辆报警消息: userId={}, vehicleId={}, alarmType={}", 
                message.getUserId(), message.getVehicleId(), message.getAlarmType());
        
        try {
            // 推送给客户端
            pushService.pushToUser(
                    message.getUserId(),
                    "vehicle_alarm",
                    message.getAlarmData()
            );
            log.info("车辆报警消息已推送给客户端: userId={}, vehicleId={}", 
                    message.getUserId(), message.getVehicleId());
        } catch (Exception e) {
            log.error("推送车辆报警消息失败: userId={}, vehicleId={}", 
                    message.getUserId(), message.getVehicleId(), e);
            throw e; // 抛出异常，触发重试机制
        }
    }

}

