package com.tzy.listener.mq;

import com.rabbitmq.client.Channel;
import com.tzy.common.constant.MqConstans;
import com.tzy.common.utils.bean.JSONTools;
import com.tzy.luckybag.dto.LuckyBagMQDTO;
import com.tzy.luckybag.service.IAppLiveLuckyBagService;
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.io.IOException;

@Slf4j
@Component
public class LuckyBagConsumer {

    @Resource
    private IAppLiveLuckyBagService appLiveLuckyBagService;

    @RabbitHandler
    @RabbitListener(queues = {MqConstans.QUEUE_LUCKY_BAG_EXPIRED})
    public void notifyConsumer(String dataStr, Channel channel, Message message) throws IOException {
        log.info("福袋开奖 {}", dataStr);
        LuckyBagMQDTO luckyBagMQDTO = JSONTools.jsonStr2obj(dataStr, LuckyBagMQDTO.class);
        try {
            appLiveLuckyBagService.startLottery(luckyBagMQDTO);
//
        } catch (Exception e) {
            log.error("福袋开奖异常id:{}", luckyBagMQDTO.getBagId(), e);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
