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


import com.alibaba.fastjson.JSON;

import com.tax.file.mq.rabbit.config.AmqpConfig;
import com.yxcheng.token.common.model.MqMessage;
import com.yxcheng.token.utils.StringUtil;
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.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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 sendApplyDownFileTopic(MqMessage mqMessage, String publishNo) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String json = JSON.toJSONString(mqMessage);
        Message message = MessageBuilder.withBody(json.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
        if(StringUtil.isNotEmpty(publishNo) && publishNo.equals("S0099")) {
            template.convertAndSend(config.getExchangeName(),config.getTestApplyFileRoutingKey(),message,correlationData);
        } else {
            template.convertAndSend(config.getExchangeName(),config.getApplyFileRoutingKey(),message,correlationData);
        }
    }


    public void sendDownFileTopic(MqMessage mqMessage, String publishNo) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String json = JSON.toJSONString(mqMessage);
        Message message = MessageBuilder.withBody(json.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
        if(StringUtil.isNotEmpty(publishNo) && publishNo.equals("S0099")) {
            template.convertAndSend(config.getExchangeName(),config.getTestDownFileRoutingKey(),message,correlationData);
        } else {
            template.convertAndSend(config.getExchangeName(),config.getDownFileRoutingKey(),message,correlationData);
        }
    }

    public void sendFixDownFileTopic(MqMessage mqMessage, String publishNo) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String json = JSON.toJSONString(mqMessage);
        Message message = MessageBuilder.withBody(json.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
        if(StringUtil.isNotEmpty(publishNo) && publishNo.equals("S0099")) {
            template.convertAndSend(config.getExchangeName(),config.getTestFixDownFileRoutingKey(),message,correlationData);
        } else {
            template.convertAndSend(config.getExchangeName(),config.getFixDownFileRoutingKey(),message,correlationData);
        }
    }

}
