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 lombok.AllArgsConstructor;
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.util.HashSet;
import java.util.Set;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Where(clause = "enabled = 1")
@ApiModel(value = "超级用户", description = "超级用户")
public class SuperUser extends BaseEntity {

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

    @Searchable
    private String phone;

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

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinTable(
            name = "super_user_authority",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")})
    @BatchSize(size = 20)
    @ExcelIgnore
    private Set<Authority> authorities = new HashSet<>();
}
