package org.dromara.common.mq.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mq.service.IRocketMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

/**
 * RocketMQ服务实现类
 *
 * @author wzj
 */
@Slf4j
@Service
public class RocketMQServiceImpl implements IRocketMQService {

    @Autowired(required = false)
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void sendMessage(String topic, Object message) {
        if (rocketMQTemplate == null) {
            log.error("RocketMQTemplate未初始化，无法发送消息");
            return;
        }

        try {
            String jsonMessage = JsonUtils.toJsonString(message);
            rocketMQTemplate.convertAndSend(topic, jsonMessage);
            log.info("发送消息到RocketMQ成功，topic: {}, message: {}", topic, jsonMessage);
        } catch (Exception e) {
            log.error("发送消息到RocketMQ失败，topic: {}, message: {}", topic, message, e);
        }
    }

    @Override
    public void sendMessage(String topic, String tag, Object message) {
        if (rocketMQTemplate == null) {
            log.error("RocketMQTemplate未初始化，无法发送消息");
            return;
        }

        try {
            String destination = topic + ":" + tag;
            String jsonMessage = JsonUtils.toJsonString(message);
            rocketMQTemplate.convertAndSend(destination, jsonMessage);
            log.info("发送消息到RocketMQ成功，destination: {}, message: {}", destination, jsonMessage);
        } catch (Exception e) {
            log.error("发送消息到RocketMQ失败，topic: {}, tag: {}, message: {}", topic, tag, message, e);
        }
    }

    @Override
    public void sendDelayMessage(String topic, String tag, Object message, int delayTimeLevel) {
        if (rocketMQTemplate == null) {
            log.error("RocketMQTemplate未初始化，无法发送延迟消息");
            return;
        }

        try {
            String destination = topic + ":" + tag;
            String jsonMessage = JsonUtils.toJsonString(message);
            rocketMQTemplate.asyncSend(destination, MessageBuilder.withPayload(jsonMessage).build(),
                new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        log.info("发送延迟消息到RocketMQ成功，destination: {}, message: {}, result: {}",
                            destination, jsonMessage, sendResult);
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        log.error("发送延迟消息到RocketMQ异常，destination: {}, message: {}",
                            destination, jsonMessage, throwable);
                    }
                }, 3000, delayTimeLevel);
        } catch (Exception e) {
            log.error("发送延迟消息到RocketMQ失败，topic: {}, tag: {}, message: {}", topic, tag, message, e);
        }
    }
}
