package cn.stylefeng.team.portal.api.enums;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.base.ReadableEnum;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

@Getter
public enum PortalScopeTypeEnum implements ReadableEnum<PortalScopeTypeEnum> {

    ITEM_1(1, "指定范围"),

    ITEM_2(2, "指定部门及以下");

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String message;

    PortalScopeTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static PortalScopeTypeEnum codeToEnum(String code) {
        if (null != code) {
            for (PortalScopeTypeEnum enumItem : PortalScopeTypeEnum.values()) {
                if (enumItem.getCode().toString().equals(code)) {
                    return enumItem;
                }
            }
        }
        return null;
    }

    public static String codeToMessage(String code) {
        if (null != code) {
            for (PortalScopeTypeEnum enumItem : PortalScopeTypeEnum.values()) {
                if (enumItem.getCode().toString().equals(code)) {
                    return enumItem.getMessage();
                }
            }
        }
        return "未知";
    }

    @Override
    public Object getKey() {
        return this.code;
    }

    @Override
    public Object getName() {
        return this.message;
    }

    @Override
    public PortalScopeTypeEnum parseToEnum(String originValue) {
        if (ObjectUtil.isEmpty(originValue)) {
            return null;
        }
        for (PortalScopeTypeEnum value : PortalScopeTypeEnum.values()) {
            if (value.code.toString().equals(originValue)) {
                return value;
            }
        }
        return null;
    }

}