package com.yxcheng.tax.mq.rabbit.producer;


import com.alibaba.fastjson.JSON;
import com.yxcheng.tax.mq.rabbit.config.AmqpConfig;
import com.yxcheng.token.common.model.MqMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class RabbitMQMessageProducer {
    private static Logger logger = LoggerFactory.getLogger(RabbitMQMessageProducer.class);

    @Autowired
    private AmqpConfig config;

    @Autowired
    private RabbitTemplate template;


    public void sendPushTokenTopic(MqMessage mqMessage) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String json = JSON.toJSONString(mqMessage);
        Message message = MessageBuilder.withBody(json.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
        template.convertAndSend(config.getExchangeName(),config.getPushTokenRoutingKey(),message,correlationData);
    }

    public void sendPushTokenNewTopic(MqMessage mqMessage) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String json = JSON.toJSONString(mqMessage);
        Message message = MessageBuilder.withBody(json.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
        template.convertAndSend(config.getExchangeName(),config.getPushTokenNewRoutingKey(),message,correlationData);
    }
}
