package com.red.circle.other.infra.database.rds.entity.gift;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.red.circle.framework.mybatis.entity.TimestampBaseEntity;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 礼物信息.
 * </p>
 *
 * @author pengliang
 * @since 2020-06-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_gift_config")
public class GiftConfig extends TimestampBaseEntity implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 主键标识.
   */
  @TableId(value = "id", type = IdType.ASSIGN_ID)
  private Long id;

  /**
   * 归属平台.
   */
  @TableField("sys_origin")
  private String sysOrigin;

  /**
   * 礼物图片.
   */
  @TableField("gift_photo")
  private String giftPhoto;

  /**
   * 礼物图片资源.
   */
  @TableField("gift_source_url")
  private String giftSourceUrl;

  /**
   * 礼物名称.
   */
  @TableField("gift_name")
  private String giftName;

  /**
   * 礼物code.
   */
  @TableField("gift_code")
  private String giftCode;

  /**
   * 礼物价值/糖果.
   */
  @TableField("gift_candy")
  private BigDecimal giftCandy;

  /**
   * 可兑积分.
   */
  @TableField("gift_integral")
  private BigDecimal giftIntegral;

  /**
   * 排序.
   */
  @TableField("sort")
  private Integer sort;

  /**
   * 特效.
   */
  @TableField("special")
  private String special;

  /**
   * 区域.
   */
  @TableField("regions")
  private String regions;

  /**
   * 类型.
   */
  @TableField("type")
  private String type;

  /**
   * 礼物tab.
   */
  @TableField("gift_tab")
  private String giftTab;

  /**
   * 0.未删除 1.已删除.
   */
  @TableField("is_del")
  private Boolean del;

  /**
   * 幸运礼物规格ID.
   */
  @TableField("standard_id")
  private Long standardId;

  /**
   * 过期时间
   */
  @TableField("expired_time")
  private Timestamp expiredTime;

  /**
   * 告白礼物 0.不是. 1.是.
   */
  @TableField("explanation_gift")
  private Boolean explanationGift;

  /**
   * 专属礼物归属人.
   */
  @TableField("user_id")
  private Long userId;


  public void removeSpecial(String specialStr) {
    if (Objects.isNull(specialStr) || specialStr.trim().length() == 0) {
      return;
    }
    if (Objects.isNull(this.special)) {
      return;
    }
    this.special = Arrays.stream(this.special.split(","))
        .filter(item -> !Objects.equals(item, specialStr))
        .collect(Collectors.joining(","));
  }

  public void addSpecial(String specialStr) {
    this.special = addSpecialToString(specialStr);
  }

  private String addSpecialToString(String specialStr) {
    if (Objects.isNull(specialStr) || specialStr.trim().length() == 0) {
      return this.special;
    }
    if (Objects.isNull(this.special) || this.special.trim().length() == 0) {
      return specialStr;
    }
    Set<String> specials = Arrays.stream(this.special.split(",")).collect(Collectors.toSet());
    specials.add(specialStr);
    return String.join(",", specials);
  }

}
