package com.red.circle.mq.rabbit.business.model;

import com.red.circle.tool.core.date.DateUtils;
import com.red.circle.tool.core.json.JacksonUtils;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import lombok.Data;

/**
 * @author pengliang on 2024/1/17
 */
@Data
public class MessageRabbitEvent implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 消费id.
   */
  private String consumeId;

  /**
   * 队列名称.
   */
  private String queueName;

  /**
   * 消息body.
   */
  private String body;

  /**
   * 创建时间.
   */
  private Date createTime;

  public static MessageRabbitEvent toSerializable(String body) {
    return JacksonUtils.readValue(body, MessageRabbitEvent.class);
  }

  public boolean msgCreateIntervalGeMinute(long minute) {
    if (Objects.isNull(createTime)) {
      return true;
    }
    return DateUtils.toDurationMinutes(createTime, DateUtils.now()) >= minute;
  }

  public static MessageRabbitEventBuilder builder() {
    return new MessageRabbitEventBuilder();
  }

  public static class MessageRabbitEventBuilder {

    private String consumeId;
    private String queueName;
    private String body;

    MessageRabbitEventBuilder() {

    }

    public MessageRabbitEventBuilder consumeId(String consumeId) {
      this.consumeId = consumeId;
      return this;
    }

    public MessageRabbitEventBuilder queueName(String queueName) {
      this.queueName = queueName;
      return this;
    }

    public MessageRabbitEventBuilder body(Object body) {
      this.body = JacksonUtils.toJson(body);
      return this;
    }

    public MessageRabbitEvent build() {
      MessageRabbitEvent messageEvent = new MessageRabbitEvent();
      messageEvent.setConsumeId(this.consumeId);
      messageEvent.setQueueName(this.queueName);
      messageEvent.setBody(this.body);
      messageEvent.setCreateTime(DateUtils.now());
      return messageEvent;
    }
  }
}
