package com.izouma.nineth.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.izouma.nineth.annotations.Searchable;
import com.izouma.nineth.config.RedisView;
import com.izouma.nineth.enums.AuthStatus;
import com.izouma.nineth.security.Authority;
import com.izouma.nineth.utils.UserAuthoritySerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.BatchSize;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@Table(indexes = {
        @Index(columnList = "phone"),
        @Index(columnList = "admin"),
        @Index(columnList = "invitor"),
        @Index(columnList = "memberId", unique = true),
})
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel(value = "用户", description = "用户")
public class User extends BaseEntity implements Serializable {

    @Size(min = 1, max = 50)
    @Column(nullable = false, unique = true)
    @Searchable
    @ExcelProperty("用户名")
    private String username;

    @Searchable
    @ExcelProperty("用户昵称")
    private String nickname;

    @ExcelProperty("头像")
    private String avatar;

    @ExcelIgnore
    @JsonView(RedisView.class)
    private String password;

    @ExcelProperty("是否删除")
    private boolean del = false;

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
    @JoinTable(
            name = "user_authority",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))},
            inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))})
    @BatchSize(size = 20)
    @ExcelIgnore
    @JsonSerialize(using = UserAuthoritySerializer.class, as = HashSet.class)
    private Set<Authority> authorities = new HashSet<>();

    @ExcelIgnore
    private String openId;

    @ExcelIgnore
    private String sex;

    @ExcelIgnore
    private String language;

    @ExcelIgnore
    private String city;

    @ExcelIgnore
    private String province;

    @ExcelIgnore
    private String country;

    @Searchable
    @ExcelProperty("手机号")
    private String phone;

    @ExcelProperty("邮箱")
    private String email;

    @ApiModelProperty("关注数量")
    @ExcelProperty("关注数量")
    private int follows;

    @ApiModelProperty("粉丝数量")
    @ExcelProperty("粉丝数量")
    private int followers;

    @ExcelProperty("销量")
    private int sales;

    @ExcelIgnore
    @Column(columnDefinition = "TEXT")
    private String intro;

    @ExcelProperty("背景")
    private String bg;

    @ExcelProperty("实名认证ID")
    private Long authId;

    @ApiModelProperty("实名审核状态")
    @ExcelProperty("实名审核状态")
    @Enumerated(EnumType.STRING)
    private AuthStatus authStatus;

    private String nftAccount;

    @JsonIgnore
    @ExcelIgnore
    private String kmsId;

    @JsonIgnore
    @ExcelIgnore
    private String publicKey;

    @JsonView(RedisView.class)
    @ExcelIgnore
    private String tradeCode;

    @ExcelProperty("是否是管理员")
    private boolean admin;

    @ApiModelProperty("分成比例")
    @ExcelProperty("分成比例")
    @Column(precision = 10, scale = 2)
    private BigDecimal shareRatio;

    /**
     * 会员id,多平台唯一标识，配置会员前缀可隔离多个平台
     */
    @ExcelProperty("会员ID")
    private String memberId;

    @ExcelIgnore
    private String settleAccountId;

    @ExcelProperty(value = "邀请人姓名")
    private String invitorName;

    @ExcelProperty(value = "邀请人手机号")
    private String invitorPhone;

    @ExcelProperty(value = "邀请码")
    private String inviteCode;

    private int minterProjectId;

    @ApiModelProperty(value = "邀请得空投")
    @ExcelProperty(value = "邀请得空投")
    private Long invitor;

    @Column(columnDefinition = "int(11) default 0")
    @ApiModelProperty(value = "邀请数量")
    @ExcelProperty(value = "邀请数量")
    private int inviteNum = 0;

    @Column(columnDefinition = "int(11) default 0")
    @ApiModelProperty(value = "获得得空投数量")
    @ExcelProperty(value = "获得得空投数量")
    private int inviteAirDrop = 0;

    @ApiModelProperty("是否绑定过银行卡")
    @ExcelProperty("用户名称")
    private boolean bankCard = false;

    @ApiModelProperty("App应用ID")
    @ExcelProperty("App应用ID")
    private String appClientId;

}
