package com.qmth.teachcloud.common.enums;

import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 角色数据权限类型
 * @Author: CaoZixuan
 * @Date: 2022-12-06
 */
public enum DataPermissionTypeEnum {
    SELF("仅本人数据权限", 1),
    SELF_ORG("本部门数据权限", 2),
    SELF_ORG_BELOW("本部门及下级部门数据权限", 3),
    ALL("全部数据权限", 4);

    @ApiModelProperty("描述")
    private final String desc;

    @ApiModelProperty("范围大小权重")
    private final int weight;

    DataPermissionTypeEnum(String desc, int weight) {
        this.desc = desc;
        this.weight = weight;
    }

    public String getDesc() {
        return desc;
    }

    public int getWeight() {
        return weight;
    }

    public static List<EnumResult> listTypes() {
        List<EnumResult> list = new ArrayList<EnumResult>();
        for (DataPermissionTypeEnum value : DataPermissionTypeEnum.values()) {
            EnumResult result = new EnumResult();
            result.setName(value.name());
            result.setOrdinal(value.ordinal());
            result.setCode(null);
            result.setDesc(value.getDesc());
            list.add(result);
        }
        return list;
    }

    /**
     * 根据数据权限权重查询枚举
     *
     * @param weight 权重
     * @return 数据权限类型
     */
    public static DataPermissionTypeEnum findByWeight(int weight) {
        List<DataPermissionTypeEnum> dataPermissionTypes = Arrays.stream(DataPermissionTypeEnum.values())
                .filter(e -> weight == e.getWeight()).collect(Collectors.toList());
        if (dataPermissionTypes.size() > 1) {
            throw ExceptionResultEnum.ERROR.exception("数据权限枚举类 权重值异常");
        }
        if (dataPermissionTypes.size() == 0) {
            return null;
        } else {
            return dataPermissionTypes.get(0);
        }
    }
}
