package com.docus.server.collect.user.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 基于注解的绑定交换器、队列、路由键设置
 * 1. Queue配置：value=队列名称、durable=是否持久化(默认true)、exclusive=排他队列只在当前connection可用(默认false)、autoDelete=如无消息是否自动删除(默认false)
 * 2. Exchange配置：value=交换器名称、type=类型(默认direct)、durable=是否持久化(默认true)、autoDelete=如无消息是否自动删除(默认false)
 * 3. QueueBinding配置：key=路由键(string数组，支持* # 匹配)，*必须匹配一个单词，#匹配0个或N个单词，用.分隔
 * 4. RabbitListener配置： bindings=Queue配置+Exchange配置+QueueBinding配置
 * 注：如果代码创建交换器等且配置绑定关系，注解只需监听队列即可，如：@RabbitListener(queues = "direct.queue")
 * <p>
 * rabbitmq 被动接收用户信息
 *
 * @author linruifeng
 * @see com.docus.server.collect.user.mq.impl.UserRabbitMqCollectServiceImpl#addUser(String)
 * @see com.docus.server.collect.user.mq.test.RabbitMQProducer#topicMQ03()
 */
@Component
@Slf4j
public class RabbitMqUserConsumer {
    @Resource(name = "userRabbitMqCollectServiceImpl")
    private IUserMqCollectService userMqCollectService;

    /**
     * topic n:1 类型 交换器队列 消费(普通会员注册提醒)
     */
    /*@RabbitListener(
        bindings = {@QueueBinding(value = @Queue(value = "topic.queue.01"),
            exchange = @Exchange(value = "topic.exchange", type = "topic"),
            key = {"*.reg.msg"})})*/
    public void addUser(String message) {
        log.info("rabbitmq message:{}", message);
        userMqCollectService.addUser(message);
    }

    /**
     * topic n:1 类型 交换器队列 消费(超级会员注册提醒)
     */
   /* @RabbitListener(bindings = {
        @QueueBinding(value = @Queue(value = "topic.queue.02"),
            exchange = @Exchange(value = "topic.exchange", type = "topic"),
            key = {"*.*.reg.msg.#"})})*/
    public void updateUser(String message) {
        log.info("rabbitmq message:{}", message);
        userMqCollectService.updateUser(message);
    }
}
