package cn.reghao.tnb.gateway.log;

import cn.reghao.jutil.jdk.web.log.GatewayLog;
import cn.reghao.jutil.jdk.serializer.JsonConverter;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2024-04-19 14:38:30
 */
@Component
public class RabbitProducer {
    private final String routingKey = "tnb.log.gateway";
    private final RabbitTemplate rabbitTemplate;

    public RabbitProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendChatMessage(String msg) {
        String exchange = exchange = "amq.fanout";
        String routingKey = "tnb.message.chat.queue";
        routingKey = "";
        //如果交换机名称写错了。那么confirmCallback 回调失败的。
        //如果设置routingkey 是一个错误,那么returncallback 会进行回调调用
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
    }

    public void sendGatewayLog(GatewayLog gatewayLog) {
        String jsonPayload = JsonConverter.objectToJson(gatewayLog);
        rabbitTemplate.convertAndSend(routingKey, jsonPayload);
    }
}
