package com.red.circle.other.infra.database.mongo.entity.sys;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.other.infra.database.mongo.dto.ActivityConfigRewards;
import com.red.circle.tool.core.date.TimestampUtils;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * 活动设置.
 *
 * @author pengliang on 2022/8/12
 */
@Data
@Accessors(chain = true)
@Document("activity_config")
public class ActivityConfig implements Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * 活动id.
   */
  @Id
  @JsonSerialize(using = ToStringSerializer.class)
  private Long id;

  /**
   * 归属系统.
   */
  private String sysOrigin;

  /**
   * 模版id.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long templateId;

  /**
   * 活动类型
   */
  private String activityType;

  /**
   * 开始时间.
   */
  private Timestamp startTime;

  /**
   * 结束时间.
   */
  private Timestamp endTime;

  /**
   * true 上架， false 下架.
   */
  private Boolean showcase;

  /**
   * 奖品信息1.
   */
  private List<ActivityConfigRewards> butOneRewards;

  /**
   * 奖品信息2.
   */
  private List<ActivityConfigRewards> butTwoRewards;

  /**
   * 活动礼物id,集合.
   */
  private List<String> giftIds;

  /**
   * banner 配置地址
   */
  private Long bannerId;

  /**
   * 备注.
   */
  private String remark;

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

  /**
   * 修改时间.
   */
  private Timestamp updateTime;

  /**
   * 创建时间.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long createUser;

  /**
   * 修改时间.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long updateUser;

  /**
   * true 已发送， false 未发送.
   */
  private Boolean awardStatus;

  /**
   * 0 未开始，1 开始，2 已结束.
   */
  public Integer status() {
    // 未开始
    if (Objects.isNull(this.startTime) || this.startTime.after(TimestampUtils.now())) {
      return 0;
    }
    // 已开始
    if (countDown() > 0) {
      return 1;
    }
    // 已结束
    return 2;
  }

  /**
   * 剩余时间.
   */
  public Long countDown() {
    if (Objects.isNull(this.startTime) || Objects.isNull(this.endTime)) {
      return 0L;
    }

    if (this.startTime.after(TimestampUtils.now())) {
      return 0L;
    }

    long countDown = this.endTime.getTime() - TimestampUtils.now().getTime();
    return countDown > 0 ? countDown : 0L;
  }

}
