package com.izouma.jmrh.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.izouma.jmrh.annotations.Searchable;
import com.izouma.jmrh.config.Constants;
import com.izouma.jmrh.converter.EncryptConverter;
import com.izouma.jmrh.security.Authority;
import com.izouma.jmrh.security.DataCenterAuthority;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

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

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

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

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

    @JsonIgnore
    private String password;

    @Column(nullable = false)
    private Boolean enabled = true;

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

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
    @JoinTable(
            name = "user_data_center_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<DataCenterAuthority> dataCenterAuthorities = new HashSet<>();

    private String openId;

    private String sex;

    private String language;

    private String city;

    private String province;

    private String country;

    @Searchable
    @Convert(converter = EncryptConverter.class)
    @ExcelProperty("手机号")
    private String phone;

    @Column(unique = true)
    @ExcelProperty("邮箱")
    private String email;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "orgInfoId")
    private OrgInfo orgInfo;

    /*实名认证信息*/
    @Convert(converter = EncryptConverter.class)
    private String contactName;

    @Convert(converter = EncryptConverter.class)
    private String contactPhone;

    @Convert(converter = EncryptConverter.class)
    private String idNo;

    private String occupation;

    private String orgName;
    /*实名认证信息*/

    private LocalDateTime pwdUpdateAt;

    @JsonIgnore
    private String gAuthKey;
}
