package com.red.circle.wallet.domain.wallet;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.common.business.core.enums.ReceiptType;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.num.ArithmeticUtils;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Objects;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 积分单据.
 *
 * @author pengliang on 2021/8/21
 */
@Data
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DiamondReceipt implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 消费id.
   */
  @NotNull
  @JsonSerialize(using = ToStringSerializer.class)
  private String consumeId;

  /**
   * 追踪id.
   */
  @NotNull
  @JsonSerialize(using = ToStringSerializer.class)
  private Long trackId;

  /**
   * 用户id.
   */
  @NotNull
  @JsonSerialize(using = ToStringSerializer.class)
  private Long userId;

  /**
   * 归属平台.
   */
  @NotNull
  private SysOriginPlatformEnum sysOrigin;

  /**
   * 来源用户.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long originUserId;

  /**
   * 来源.
   */
  @NotBlank
  private String origin;

  /**
   * 描述.
   */
  private String originDesc;

  /**
   * 积分数量.
   */
  @NotNull
  private BigDecimal amount;

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

  /**
   * 类型.
   */
  private ReceiptType type;

  public void setCreateTime() {
    if (Objects.isNull(getCreateTime())) {
      setCreateTime(TimestampUtils.now());
    }
  }

  @JsonIgnore
  public String getSysOriginName() {
    return getSysOrigin().name();
  }

  @JsonIgnore
  public int getTypeVal() {
    return getType().getType();
  }

  public boolean checkAmountIsLteZero() {
    return ArithmeticUtils.lteZero(amount);
  }

  @JsonIgnore
  public String getChangeBalanceConsumeKey() {
    return "CHANGE_DIAMOND_BALANCE:" + getConsumeId();
  }
}
