package com.ruoyi.cai.mq;

import com.alibaba.fastjson.JSON;
import com.ruoyi.cai.mq.config.RoomCheckDelayMqConfig;
import com.ruoyi.cai.mq.config.RoomSettleDelayMqConfig;
import com.ruoyi.cai.mq.config.CommonDelayMqConfig;
import com.ruoyi.cai.mq.consumer.CalculateSalesQueueConsumer;
import com.ruoyi.cai.mq.consumer.CommonConsumer;
import com.ruoyi.cai.mq.consumer.WindowConsumer;
import com.ruoyi.cai.mq.dto.CommonDTO;
import com.ruoyi.cai.mq.dto.CommonDelayDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AmqpProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendCalculateSales(String message){
        rabbitTemplate.convertAndSend(CalculateSalesQueueConsumer.CALCULATE_SALES_EXCHANGE, CalculateSalesQueueConsumer.CALCULATE_SALES_KEY, message);
    }


    public <T extends CommonDTO> void sendCommonMq(T dto){
        rabbitTemplate.convertAndSend(CommonConsumer.COMMON_EXCHANGE,
                CommonConsumer.COMMON_KEY, JSON.toJSONString(dto));
    }

    public <T extends CommonDTO> void sendWindowMq(T dto){
        rabbitTemplate.convertAndSend(WindowConsumer.WINDOW_EXCHANGE,
                WindowConsumer.WINDOW_KEY, JSON.toJSONString(dto));
    }

    public void sendRoomCheckDelay(String message, Integer timeout){
        rabbitTemplate.convertAndSend(RoomCheckDelayMqConfig.EXCHANGE_NAME,
                RoomCheckDelayMqConfig.ROUTING_KEY,
                message,
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间，单位毫秒
                    return messagePostProcessor;
                });
    }

    public void sendRoomSettleDelay(String message, Integer timeout){
        rabbitTemplate.convertAndSend(RoomSettleDelayMqConfig.EXCHANGE_NAME,
                RoomSettleDelayMqConfig.ROUTING_KEY,
                message,
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间，单位毫秒
                    return messagePostProcessor;
                });
    }

    public void sendCommonDelayMq(CommonDelayDto dto, Integer timeout){
        rabbitTemplate.convertAndSend(CommonDelayMqConfig.EXCHANGE_NAME,
                CommonDelayMqConfig.ROUTING_KEY,
                JSON.toJSONString(dto),
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间，单位毫秒
                    return messagePostProcessor;
                });
    }
}
