package com.izouma.jiashanxia.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.izouma.jiashanxia.annotations.Searchable;
import com.izouma.jiashanxia.config.Constants;
import com.izouma.jiashanxia.security.Authority;
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 org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

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;

    @JsonIgnore
    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
    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;

    private BigDecimal amount;

    private Long parent;

    @ApiModelProperty(value = "企业id")
    private Long companyId;

    @ApiModelProperty(value = "是否团队创始人")
    private Boolean  teamFounder;

    @ApiModelProperty("是否微信授权")
    private boolean wxAuthorized;

    private boolean isVip;

    @Transient
    private String sessionKey;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "companyId", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @NotFound(action = NotFoundAction.IGNORE)
    private Company company;
}
