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/28 11:25
 * @description 发行商户积分
 * @since JDK 1.8
 */
@Configuration
public class IssueMerchantIntegralConfig {

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

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

    @Bean
    public Queue issueMerchantIntegralQueue() {
        return new Queue(QueueTypeEnum.ISSUE_MERCHANT_INTEGRAL.getQueue());
    }

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

    @Bean
    Binding issueMerchantIntegralBinding(DirectExchange issueMerchantIntegralDirect, Queue issueMerchantIntegralQueue) {
        return BindingBuilder.bind(issueMerchantIntegralQueue).to(issueMerchantIntegralDirect).with(QueueTypeEnum.ISSUE_MERCHANT_INTEGRAL.getQueueKey());
    }

    @Bean
    public Binding issueMerchantIntegralTTLBinding(Queue issueMerchantIntegralTTLQueue, DirectExchange issueMerchantIntegralTTLDirect) {
        return BindingBuilder.bind(issueMerchantIntegralTTLQueue).to(issueMerchantIntegralTTLDirect)
                .with(QueueTTLTypeEnum.ISSUE_MERCHANT_INTEGRAL_TTL.getQueueKey());
    }

}
