package com.fzm.mall.server.front.mq.config;


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

/**
 * @author tdz
 * @date 2021/05/26 10:39
 * @description 资产转账
 * @since JDK 1.8
 */
@Configuration
public class MallAssetTranConfig {

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

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

    @Bean
    public Queue mallAssetTranQueue() {
        return new Queue(QueueTypeEnum.MALL_ASSET_TRAN.getQueue());
    }

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

    @Bean
    Binding mallAssetTranBinding(DirectExchange mallAssetTranDirect, Queue mallAssetTranQueue) {
        return BindingBuilder.bind(mallAssetTranQueue).to(mallAssetTranDirect).with(QueueTypeEnum.MALL_ASSET_TRAN.getQueueKey());
    }

    @Bean
    public Binding mallAssetTranTTLBinding(Queue mallAssetTranTTLQueue, DirectExchange mallAssetTranTTLDirect) {
        return BindingBuilder.bind(mallAssetTranTTLQueue).to(mallAssetTranTTLDirect)
                .with(QueueTTLTypeEnum.MALL_ASSET_TRAN_TTL.getQueueKey());
    }
}
