package com.xhkjedu.slive.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.xhkjedu.slive.model.liveplay.LCourseSectionPlayback;
import com.xhkjedu.slive.service.liveplay.CourseSectionPlaybackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 消息到达消费者监听类
 */
@Component
@Slf4j
public class MessageConsumer {
    @Resource
    private CourseSectionPlaybackService courseSectionPlaybackService;

    //处理收到的rabbit消息的回调方法
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.queue}")
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        System.out.println(LocalDateTime.now()+" 接收消息内容："+msg);
        LCourseSectionPlayback playback = JSON.parseObject(msg,LCourseSectionPlayback.class);
        courseSectionPlaybackService.updatePlayback(playback);
    }
}
