package org.jeecg.modules.rabbitmq;

import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DelayedMessageService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendOrderExpireMessage(String message,Integer time) {
        MessagePostProcessor processor = msg -> {
            // 设置延迟时间（25天毫秒数）
            msg.getMessageProperties().setDelay(time);
            // 强制持久化消息
            msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            return msg;
        };

        rabbitTemplate.convertAndSend(
                RabbitMQConfig.ORDER_EXPIRE_DELAY_EXCHANGE,
                RabbitMQConfig.ORDER_EXPIRE_DELAY_ROUTING_KEY,
                message,
                processor
        );
    }

    /**
     * 发送延迟消息：订单15分钟超时未支付取消
     *
     * @param message
     */
    public void sendOrderMessage(String message) {
        MessagePostProcessor processor = msg -> {
            // 设置延迟时间（15分钟毫秒数）
            msg.getMessageProperties().setDelay(15 * 60 * 1000);
            // 强制持久化消息
            msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            return msg;
        };

        rabbitTemplate.convertAndSend(
                RabbitMQConfig.ORDER_DELAY_EXCHANGE,
                RabbitMQConfig.ORDER_DELAY_ROUTING_KEY,
                message,
                processor
        );
    }
}