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:42
 */
@Configuration
public class ChangeCoinsConfig {

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

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

    @Bean
    public Queue changeCoinsQueue() {
        return new Queue(QueueTypeEnum.CHANGE_COINS.getQueue());
    }

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

    @Bean
    Binding changeCoinsBinding(DirectExchange changeCoinsDirect, Queue changeCoinsQueue) {
        return BindingBuilder.bind(changeCoinsQueue).to(changeCoinsDirect).with(QueueTypeEnum.CHANGE_COINS.getQueueKey());
    }

    @Bean
    public Binding changeCoinsTTLBinding(Queue changeCoinsTTLQueue, DirectExchange changeCoinsTTLDirect) {
        return BindingBuilder.bind(changeCoinsTTLQueue).to(changeCoinsTTLDirect)
                .with(QueueTTLTypeEnum.CHANGE_COINS_TTL.getQueueKey());
    }

}
