package com.ghy.common.enums;


import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 但星霖
 * @Date 2023-03-30 11：26：22
 * 说明：系统配置枚举相关
 */
public enum SysGlobalConfigEnum implements IEnumType {

    ORDER_REMIND_AUDIO_CONFIG(1001, "订单提醒音频配置"),
    ;

    private final int code;
    private final String desc;

    private final static Map<Integer, SysGlobalConfigEnum> BY_CODE_MAP
            = Arrays.stream(SysGlobalConfigEnum.values()).collect(Collectors.toMap(SysGlobalConfigEnum::code, code -> code));

    private final static Map<String, SysGlobalConfigEnum> BY_NAME_MAP
            = Arrays.stream(SysGlobalConfigEnum.values()).collect(Collectors.toMap(code -> code.name().toLowerCase(), code -> code));


    /**
     * @param code 代码
     * @param desc 描述
     */
    SysGlobalConfigEnum(final int code, final String desc) {
        this.code = code;
        this.desc = desc;
    }

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

    @Override
    public String desc() {
        return this.desc;
    }

    /**
     * @param code 代码
     * @return 转换出来的状态码
     */
    public static SysGlobalConfigEnum parse(Integer code) {
        return BY_CODE_MAP.get(code);
    }

    public static SysGlobalConfigEnum parse(Integer code, SysGlobalConfigEnum defaultState) {
        return BY_CODE_MAP.getOrDefault(code, defaultState);
    }

    /**
     * @param name 名字
     * @return 转换出来的状态码
     */
    public static SysGlobalConfigEnum parse(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return BY_NAME_MAP.get(name.trim().toLowerCase());
    }

    public static SysGlobalConfigEnum parse(String name, SysGlobalConfigEnum defaultState) {
        if (StringUtils.isBlank(name)) {
            return defaultState;
        }
        return BY_NAME_MAP.getOrDefault(name.trim().toLowerCase(), defaultState);
    }
}




