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

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

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

    @Bean
    public Queue platformIntegralAddQueue() {
        return new Queue(QueueTypeEnum.PLATFORM_INTEGRAL_ADD.getQueue());
    }

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

    @Bean
    Binding platformIntegralAddBinding(DirectExchange platformIntegralAddDirect, Queue platformIntegralAddQueue) {
        return BindingBuilder.bind(platformIntegralAddQueue).to(platformIntegralAddDirect).with(QueueTypeEnum.PLATFORM_INTEGRAL_ADD.getQueueKey());
    }

    @Bean
    public Binding platformIntegralAddTTLBinding(Queue platformIntegralAddTTLQueue, DirectExchange platformIntegralAddTTLDirect) {
        return BindingBuilder.bind(platformIntegralAddTTLQueue).to(platformIntegralAddTTLDirect)
                .with(QueueTTLTypeEnum.PLATFORM_INTEGRAL_ADD_TTL.getQueueKey());
    }
}
