package com.fzm.mall.server.admin.mq.config.finance_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
 * @date 2021/4/29 10:39
 * @description 积分转账
 * @since JDK 1.8
 */
@Configuration
public class PlatformIntegralTranConfig {

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

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

    @Bean
    public Queue platformIntegralTranQueue() {
        return new Queue(QueueTypeEnum.PLATFORM_INTEGRAL_TRAN.getQueue());
    }

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

    @Bean
    Binding platformIntegralTranBinding(DirectExchange platformIntegralTranDirect, Queue platformIntegralTranQueue) {
        return BindingBuilder.bind(platformIntegralTranQueue).to(platformIntegralTranDirect).with(QueueTypeEnum.PLATFORM_INTEGRAL_TRAN.getQueueKey());
    }

    @Bean
    public Binding platformIntegralTranTTLBinding(Queue platformIntegralTranTTLQueue, DirectExchange platformIntegralTranTTLDirect) {
        return BindingBuilder.bind(platformIntegralTranTTLQueue).to(platformIntegralTranTTLDirect)
                .with(QueueTTLTypeEnum.PLATFORM_INTEGRAL_TRAN_TTL.getQueueKey());
    }
}
