package cn.reghao.bnt.web.admin.model.po;

import cn.reghao.bnt.web.admin.model.constant.RoleType;
import cn.reghao.bnt.web.util.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.validator.constraints.Length;

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

/**
 * @author reghao
 * @date 2019/03/14 21:46:13
 */
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@EqualsAndHashCode(callSuper = false, exclude = {"description", "menus"})
@ToString(exclude = {"menus"})
@Table(name = "rbac_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;
    @Column(nullable = false)
    private String viewPath;
    // Role 端维护 Role 和 Menu 之间的关系
    @ManyToMany
    @JoinTable(name = "rbac_role_menu",
            joinColumns = @JoinColumn(name = "role_id"),
            inverseJoinColumns = @JoinColumn(name = "menu_id"))
    @JsonIgnore
    private Set<Menu> menus;

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

    public Role(RoleType roleType) {
        this.name = roleType.name();
        this.description = roleType.getDesc();
    }
}
