package com.fzm.mall.server.admin.mq.config.goods_center;

import com.fzm.mall.server.admin.mq.enums.QueueTTLTypeEnum;
import com.fzm.mall.server.admin.mq.enums.QueueTypeEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangp
 * @description 批量发行使用权
 * @date 2020/10/20  17:39
 */
@Configuration
public class UsuNftConfig {

    @Bean
    DirectExchange usuNftDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueTypeEnum.NFT_USU.getExchange())
                .durable(true).build();
    }

    @Bean
    DirectExchange usuNftTTLDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueTTLTypeEnum.NFT_USU_TTL.getExchange())
                .durable(true).build();
    }

    @Bean
    public Queue usuNftQueue() {
        return new Queue(QueueTypeEnum.NFT_USU.getQueue());
    }

    @Bean
    public Queue usuNftTTLQueue() {
        return QueueBuilder.durable(QueueTTLTypeEnum.NFT_USU_TTL.getQueue())
                // 配置到期后转发的交换
                .withArgument("x-dead-letter-exchange", QueueTypeEnum.NFT_USU.getExchange())
                // 配置到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueTypeEnum.NFT_USU.getQueueKey()).build();
    }

    @Bean
    Binding usuNftBinding(DirectExchange usuNftDirect, Queue usuNftQueue) {
        return BindingBuilder.bind(usuNftQueue).to(usuNftDirect).with(QueueTypeEnum.NFT_USU.getQueueKey());
    }

    @Bean
    public Binding usuNftTTLBinding(Queue usuNftTTLQueue, DirectExchange usuNftTTLDirect) {
        return BindingBuilder.bind(usuNftTTLQueue).to(usuNftTTLDirect)
                .with(QueueTTLTypeEnum.NFT_USU_TTL.getQueueKey());
    }

}
