package cn.estsh.i3plus.core.apiservice.mq;

import cn.estsh.i3plus.platform.common.util.QueueConstWords;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
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.stereotype.Component;

import java.io.IOException;

/**
 * @Description : 平台通用队列
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2019-01-14 17:01
 * @Modify:
 **/
@Component
public class CommonQueueReceive {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonQueueReceive.class);

    /**
     * 通用队列，绑定通用交换机
     * @param channel
     * @param message
     */
    @RabbitListener(bindings ={
            //绑定队列
            @QueueBinding(value = @Queue(value = QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.QUEUE_COMMON),
                    //绑定交换
                    exchange = @Exchange(type=ExchangeTypes.FANOUT,value = QueueConstWords.QUEUE_EXCHANGE_COMMON))
    })
    public void commonQueue(Object object, Channel channel, Message message){
        try {
            LOGGER.info("【{}】数据接收成功：{}",QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.QUEUE_COMMON,object);

            //信息已处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            LOGGER.error("【{}】处理出错，丢弃：{}",QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.QUEUE_COMMON,e.getMessage(),e);
            //丢弃这条消息
            try {
                // 未成功处理，重新发送
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 直达交换机交换机
     * @param channel
     * @param message
     */
    @RabbitListener(bindings ={
            //绑定队列
            @QueueBinding(value = @Queue(value = QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.DIRECT_EXCHANGE_KEY_ROLE),
            //绑定交换
            exchange = @Exchange(type=ExchangeTypes.DIRECT,value = QueueConstWords.QUEUE_EXCHANGE_DIRECT),
            key = QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.DIRECT_EXCHANGE_KEY_ROLE)
    })
    public void directQueue(Object object, Channel channel, Message message){
        try {
            LOGGER.info("【{}】数据接收成功：{}",QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.QUEUE_COMMON,object);

            //信息已处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            LOGGER.error("【{}】处理出错，丢弃：{}",QueueConstWords.CORE_QUEUE_PREFIX + QueueConstWords.QUEUE_COMMON,e.getMessage(),e);
            //丢弃这条消息
            try {
                // 未成功处理，重新发送
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
