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

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

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

    @Bean
    public Queue issueCoinsQueue() {
        return new Queue(QueueTypeEnum.ISSUE_COINS.getQueue());
    }

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

    @Bean
    Binding issueCoinsBinding(DirectExchange issueCoinsDirect, Queue issueCoinsQueue) {
        return BindingBuilder.bind(issueCoinsQueue).to(issueCoinsDirect).with(QueueTypeEnum.ISSUE_COINS.getQueueKey());
    }

    @Bean
    public Binding issueCoinsTTLBinding(Queue issueCoinsTTLQueue, DirectExchange issueCoinsTTLDirect) {
        return BindingBuilder.bind(issueCoinsTTLQueue).to(issueCoinsTTLDirect)
                .with(QueueTTLTypeEnum.ISSUE_COINS_TTL.getQueueKey());
    }

}
