package com.chushang.common.mq.produce;

import apache.rocketmq.v2.Message;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @auther: zhao
 * @date: 2024/6/12 14:18
 */
@Component
public class MqProduceService<T> {

    @Resource
    RocketMQTemplate rocketMQTemplate;

    private String getDesi(String topic, String tag){
        if (StringUtils.isNotBlank(tag)) topic = topic + ":" + tag;
        return topic;
    }

    /**
     * 发送单向消息, 不关心发送结果
     */
    public void sendOneWayMsg(@NotBlank String topic,@NotBlank T message){
        rocketMQTemplate.sendOneWay(topic, MessageBuilder.withPayload(message).build());
    }

    /**
     * 发送单向消息, 不关心发送结果
     */
    public void sendOneWayMsg(@NotBlank String topic,String tag,@NotBlank T message){
        rocketMQTemplate.sendOneWay(getDesi(topic, tag), MessageBuilder.withPayload(message).build());
    }

    /**
     * 发送 topic 消息
     */
    public void send(@NotBlank String topic, @NotBlank T message){
        rocketMQTemplate.send(topic, MessageBuilder.withPayload(message).build());
    }

    /**
     * 带tag 的消息
     * tag 可为空
     */
    public void send(@NotBlank String topic,String tag, @NotBlank T message){
        rocketMQTemplate.send(getDesi(topic, tag), MessageBuilder.withPayload(message).build());
    }

    /**
     * 发送同步消息
     */
    public SendResult sendMsg(@NotBlank String topic,@NotBlank T message){
        return rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload(message)

                .build());
    }

    /**
     * 发送同步消息
     */
    public SendResult sendMsg(@NotBlank String topic,String tag,@NotBlank T message){
        return rocketMQTemplate.syncSend(getDesi(topic, tag), MessageBuilder.withPayload(message).build());
    }

    /**
     * 发送异步消息
     */
    public void sendAsyncMsg(@NotBlank String topic,@NotBlank T message, @NotNull SendCallback sendCallback){
        rocketMQTemplate.asyncSend(topic, MessageBuilder.withPayload(message).build(),sendCallback);
    }

    /**
     * 发送异步消息
     */
    public void sendAsyncMsg(@NotBlank String topic,String tag,@NotBlank T message, @NotNull SendCallback sendCallback){
        rocketMQTemplate.asyncSend(getDesi(topic, tag), MessageBuilder.withPayload(message).build(),sendCallback);
    }

    /**
     * 发送延时消息
     * 延时消息一共分为18个等级1-18分别含义为：1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     * delayLevel: 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     * messageTimeOut: 超时时间, 用于重试
     */
    public void  sendDelayMsg(@NotBlank String topic, @NotBlank T message, int messageTimeOut,int delayLevel){
        rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload(message).build(),messageTimeOut,delayLevel);
    }

    /**
     * 发送延时消息
     * 延时消息一共分为18个等级分别为：1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     * delayLevel: 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     * messageTimeOut: 超时时间, 用于重试
     */
    public void  sendDelayMsg(@NotBlank String topic,String tag, @NotBlank T message,int messageTimeOut, int delayLevel){
        rocketMQTemplate.syncSend(getDesi(topic, tag), MessageBuilder.withPayload(message).build(),messageTimeOut,delayLevel);
    }


}
