package com.izouma.nineth.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.izouma.nineth.JsonView.UserView;
import com.izouma.nineth.annotations.Searchable;
import com.izouma.nineth.config.Constants;
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.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel(value = "用户", description = "用户")
public class User extends BaseEntity implements Serializable {

    @Pattern(regexp = Constants.Regex.USERNAME)
    @Size(min = 1, max = 50)
    @Column(nullable = false, unique = true)
    @Searchable
    private String username;

    @Searchable
    private String nickname;

    private String avatar;

    @JsonView(UserView.Redis.class)
    private String password;

    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<>();

    private String openId;

    private String sex;

    private String language;

    private String city;

    private String province;

    private String country;

    @Searchable
    private String phone;

    private String email;

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

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

    private int sales;

    private String intro;

    private String bg;

    private Long authId;

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

    private String nftAccount;

    @JsonIgnore
    private String kmsId;

    @JsonIgnore
    private String publicKey;

    @JsonIgnore
    private String tradeCode;

    private boolean admin;

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

    private String memberId;

    private String settleAccountId;
}
