package cn.lihongjie.coal.user.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.role.entity.RoleEntity;

import jakarta.persistence.*;

import lombok.Data;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.Comment;

import java.util.ArrayList;
import java.util.List;

@Data
@Entity
@Cacheable

@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_user_org_id", columnList = "organization_id"))

public class UserEntity extends OrgCommonEntity {

    @Comment("用户名")
    private String username;

    @Comment("密码")
    private String password;

    @Comment("邮箱")
    private String email;

    @Comment("手机号")
    private String phone;

    @ManyToMany()
    @JoinTable(
            indexes = {
                @jakarta.persistence.Index(
                        name = "idx_user_roles_users_id",
                        columnList = "users_id"),
                @jakarta.persistence.Index(
                        name = "idx_user_roles_roles_id",
                        columnList = "roles_id")
            })
    @Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
    private List<RoleEntity> roles;

    @Transient private List<RoleEntity> otherRoles;

    @Comment("机构管理员标识")
    private Boolean orgAdmin;

    @Comment("系统管理员标识")
    private Boolean sysAdmin;



    public List<RoleEntity> allRoles() {

        List<RoleEntity> allRoles = new ArrayList<>(this.roles);
        if (this.otherRoles != null) {
            allRoles.addAll(this.otherRoles);
        }
        return allRoles;
    }
}