package com.hdkj.lt.base.enums;

import lombok.SneakyThrows;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @Description: 枚举数据校验接口
 * @Author: wenhao
 * @Create: 2022/08/14 11:04
 * @Version: 1.0
 */
public interface BaseValidEnum<T> {

    String VALUES_METHOD = "values";

    T getCode();

    String getDesc();

    // 根据code获取枚举类实例
    @SneakyThrows(ReflectiveOperationException.class)
    static <T> BaseValidEnum<T> getByCode(T code, Class<?> clazz) {
        Method valuesMethod = clazz.getMethod(VALUES_METHOD);
        BaseValidEnum<T>[] validEnums = (BaseValidEnum<T>[]) valuesMethod.invoke(null);
        for (BaseValidEnum<T> value : validEnums) {
            if (Objects.equals(String.valueOf(value.getCode()), String.valueOf(code))) {
                return value;
            }
        }
        return null;
    }

}
