package cn.reghao.autodop.dmaster.rbac.model.po;

import cn.reghao.autodop.dmaster.util.db.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
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"})
@Entity
public class Role extends BaseEntity<Integer> {
    @Pattern(regexp = "^\\w+$", message = "只能是数字、英文字符和下划线")
    @Column(unique = true, nullable = false)
    private String title;
    @Pattern(regexp = "^[\\u4e00-\\u9fa5]{0,}$", message = "只能是中文字符")
    @Column(nullable = false)
    private String name;
    @Length(max = 100, message = "描述的长度不超过 100 个中文字符")
    private String description;
    // Role 端维护 Role 和 Menu 之间的关系
    @ManyToMany
    @JoinTable(name = "role_menu",
            joinColumns = @JoinColumn(name = "role_id"),
            inverseJoinColumns = @JoinColumn(name = "menu_id"))
    private Set<Menu> menus;
}
