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 IssueNftConfig {

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

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

    @Bean
    public Queue issueNftQueue() {
        return new Queue(QueueTypeEnum.NFT_ISSUE.getQueue());
    }

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

    @Bean
    Binding issueNftBinding(DirectExchange issueNftDirect, Queue issueNftQueue) {
        return BindingBuilder.bind(issueNftQueue).to(issueNftDirect).with(QueueTypeEnum.NFT_ISSUE.getQueueKey());
    }

    @Bean
    public Binding issueNftTTLBinding(Queue issueNftTTLQueue, DirectExchange issueNftTTLDirect) {
        return BindingBuilder.bind(issueNftTTLQueue).to(issueNftTTLDirect)
                .with(QueueTTLTypeEnum.NFT_ISSUE_TTL.getQueueKey());
    }

}
