package com.buy507.mall.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.buy507.mall.dto.ReceivingQueueEnum;

/**
 * 自动确认收货消息的发出者
 */
@Component
public class AutoConfirmReceivingSender {
	
    private static Logger LOGGER = LoggerFactory.getLogger(AutoConfirmReceivingSender.class);
    
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(Long orderId, final long delayTimes){
        //给延迟队列发送消息
        amqpTemplate.convertAndSend(ReceivingQueueEnum.QUEUE_TTL_RECEIVING_CONFIRM.getExchange(), ReceivingQueueEnum.QUEUE_TTL_RECEIVING_CONFIRM.getRouteKey(), orderId, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //给消息设置延迟毫秒值
                message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                return message;
            }
        });
        LOGGER.info("Confirm receiving send orderId:{}", orderId);
    }
}
