package com.ningdatech.pmapi.user.entity.enumeration;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统角色枚举（内置角色）
 * </p>
 *
 * @author WendyYang
 * @since 14:15 2023/1/13
 */
@Getter
public enum RoleEnum {

    /**
     * 系统角色枚举
     */
    NORMAL_MEMBER("普通用户", 7),

    EXPERT("专家", 6),

    COMPANY_MANAGER("单位管理员", 5),

    EXPERT_ADMIN("专家管理员", 3),

    REGION_MANAGER("区域管理员", 4),

    SUPER_ADMIN("超级管理员", 1),

    VISITOR("访客", 2),

    DASHBOARD("工作台", 8);

    private final String desc;

    /**
     * 角色优先级
     */
    private final Integer level;

    RoleEnum(String desc, Integer level) {
        this.desc = desc;
        this.level = level;
    }

    public boolean eq(String name) {
        return this.name().equals(name);
    }

    public static RoleEnum mathByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        for (RoleEnum roleEnum : RoleEnum.values()) {
            if (roleEnum.name().equals(name)) {
                return roleEnum;
            }
        }
        return null;
    }

    public static RoleEnum checkHigherRole(List<String> roleCodes) {
        RoleEnum res = null;
        for (String roleCode : roleCodes) {
            RoleEnum roleEnum = mathByName(roleCode);
            if (Objects.isNull(res) ||
                    (Objects.nonNull(roleEnum) && roleEnum.getLevel() < res.getLevel())) {
                res = roleEnum;
            }
        }
        return res;
    }
}
