package cn.reghao.oss.console.account.model.po;

import cn.reghao.oss.console.util.db.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.annotations.Proxy;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.util.Set;

/**
 * @author reghao
 * @date 2019/03/14 21:46:13
 */
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false, exclude = {"description", "menus"})
@ToString(exclude = {"menus"})
@Proxy(lazy = false)
@Table(name = "sys_role")
@Entity
public class Role extends BaseEntity {
    // TODO 匹配小写英文字符报错
    // @Pattern(regexp = "^[a-z]+$", message = "角色只能是英文字符")
    @Pattern(regexp = "^\\w+$", message = "角色只能是英文字符")
    @Column(unique = true, nullable = false)
    private String name;
    @Length(max = 100, message = "对角色描述的长度不超过 100 个中文字符")
    private String description;
    // Role 端维护 Role 和 Menu 之间的关系
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_role_menu",
            joinColumns = @JoinColumn(name = "role_id"),
            inverseJoinColumns = @JoinColumn(name = "menu_id"))
    private Set<Menu> menus;

    public Role(String name, String description) {
        this.name = name;
        this.description = description;
    }
}
