package cn.reghao.tnb.content.app.util.rabbit;

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.tnb.content.app.vod.service.VideoPlayService;
import cn.reghao.tnb.message.api.dto.MediaProgress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2024-04-19 14:40:47
 */
@Slf4j
@Component
public class MessageListeners {
    private final VideoPlayService videoPlayService;

    public MessageListeners(VideoPlayService videoPlayService) {
        this.videoPlayService = videoPlayService;
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "tnb.content.media", durable = "true"),
            key = "tnb.content.media",
            exchange = @Exchange(value = "amq.direct"))
    )
    public void messageQueueConsumer(@Payload String msg) {
        MediaProgress mediaProgress = JsonConverter.jsonToObject(msg, MediaProgress.class);
        if (mediaProgress != null) {
            videoPlayService.saveRecord(mediaProgress);
        }
    }
}
