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

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

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

    @Bean
    public Queue merchantIntegralTranQueue() {
        return new Queue(QueueTypeEnum.MERCHANT_INTEGRAL_TRAN.getQueue());
    }

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

    @Bean
    Binding merchantIntegralTranBinding(DirectExchange merchantIntegralTranDirect, Queue merchantIntegralTranQueue) {
        return BindingBuilder.bind(merchantIntegralTranQueue).to(merchantIntegralTranDirect).with(QueueTypeEnum.MERCHANT_INTEGRAL_TRAN.getQueueKey());
    }

    @Bean
    public Binding merchantIntegralTranTTLBinding(Queue merchantIntegralTranTTLQueue, DirectExchange merchantIntegralTranTTLDirect) {
        return BindingBuilder.bind(merchantIntegralTranTTLQueue).to(merchantIntegralTranTTLDirect)
                .with(QueueTTLTypeEnum.MERCHANT_INTEGRAL_TRAN_TTL.getQueueKey());
    }
}
