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

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

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

    @Bean
    public Queue userAssetsRechargeOrDeductionQueue() {
        return new Queue(QueueTypeEnum.USER_ASSETS_RECHARGE_OR_DEDUCTION.getQueue());
    }

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

    @Bean
    Binding userAssetsRechargeOrDeductionBinding(DirectExchange userAssetsRechargeOrDeductionDirect, Queue userAssetsRechargeOrDeductionQueue) {
        return BindingBuilder.bind(userAssetsRechargeOrDeductionQueue).to(userAssetsRechargeOrDeductionDirect).with(QueueTypeEnum.USER_ASSETS_RECHARGE_OR_DEDUCTION.getQueueKey());
    }

    @Bean
    public Binding userAssetsRechargeOrDeductionTTLBinding(Queue userAssetsRechargeOrDeductionTTLQueue, DirectExchange userAssetsRechargeOrDeductionTTLDirect) {
        return BindingBuilder.bind(userAssetsRechargeOrDeductionTTLQueue).to(userAssetsRechargeOrDeductionTTLDirect)
                .with(QueueTTLTypeEnum.USER_ASSETS_RECHARGE_OR_DEDUCTION_TTL.getQueueKey());
    }
}
