package com.kxs.system.biz.amqp.rabbit;

import com.kxs.common.mq.config.RabbitConfiguration;
import com.kxs.product.api.amqp.rabbit.RabbitGdActQueueMQ;
import com.kxs.system.api.amqp.rabbit.RabbitOrderQueueMQ;
import com.kxs.system.biz.service.KxsCampService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
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 java.io.IOException;

/**
 * rabbit 队列侦听器
 *
 * @author 没秃顶的码农
 * @date 2024-04-25
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class RabbitOrderQueueListener {

	private final KxsCampService kxsCampService;

	/**
	 * 监听 hello 队列的处理器
	 *
	 * @param message 消息
	 */
	@RabbitListener(queues = RabbitOrderQueueMQ.QUEUE_NAME, ackMode = "MANUAL")
	@RabbitHandler
	public void onMessage(String msg, Message message, Channel channel){
		log.info("消费端Payload: " + RabbitOrderQueueMQ.parse(msg));
        try {
			RabbitOrderQueueMQ.MsgEntity parse = RabbitOrderQueueMQ.parse(msg);
			//统计到奖金池
			kxsCampService.prizePoolIsRefreshed(parse);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
			log.error(e.getMessage(), e);
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException ex) {
				log.error("进入死信队列失败" + ex.getMessage(), ex);
			}
        }
    }

	/**
	 * 监听死信 队列的处理器
	 *
	 * @param message 消息
	 */
	@RabbitListener(queues = RabbitOrderQueueMQ.DEAD_QUEUE_NAME)
	@RabbitHandler
	public void onDeadMessage(String msg, Message message, Channel channel){
		log.info("死信队列Payload: " + RabbitOrderQueueMQ.parse(msg));
        try {
			RabbitOrderQueueMQ.MsgEntity parse = RabbitOrderQueueMQ.parse(msg);
        } catch (Exception e) {
			log.error(e.getMessage(), e);

        }
    }

//	/**
//	 * 监听死信 队列的处理器
//	 *
//	 * @param message 消息
//	 */
//	@RabbitListener(queues = RabbitGdActQueueMQ.QUEUE_NAME)
//	@RabbitHandler
//	public void onGdMessage(String msg, Message message, Channel channel){
//		log.info("广电报备队列Payload: " + RabbitGdActQueueMQ.parse(msg));
//        try {
//			RabbitGdActQueueMQ.MsgEntity parse = RabbitGdActQueueMQ.parse(msg);
//        } catch (Exception e) {
//			log.error(e.getMessage(), e);
//
//        }
//    }
}