package com.bz.rmcp.dap.mq;

import com.alibaba.fastjson.JSON;
import com.bz.rmcp.dap.model.OfflineData;
import com.bz.rmcp.dap.service.DeviceService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.bz.rmcp.dap.config.RabbitMqConfig.DEAD_LETTER_QUEUE_NAME;

/**
 * @author linqingwei
 * @date 2021-08-09 17:26
 */
@Slf4j
@Component
public class DeadLetterQueueConsumer {

    @Autowired
    private DeviceService deviceService;

    /*
     * @description 监听设备离线死信队列
     * @param message
     * @param channel
     * @return
     * @author linqingwei
     **/
    @RabbitListener(queues = DEAD_LETTER_QUEUE_NAME)
    public void receive(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("死信队列收到消息：{}", msg);
        OfflineData data = JSON.parseObject(msg, OfflineData.class);
        deviceService.handleOffline(data);
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
