package com.lesingle.edu.common.enums;

import lombok.Getter;

/**
 * 是/否枚举
 * 用于替代数据库中的 0/1 魔法值
 */
@Getter
public enum YesNo {

    NO(0, "否"),
    YES(1, "是");

    private final Integer code;
    private final String description;

    YesNo(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 根据 code 获取枚举
     */
    public static YesNo fromCode(Integer code) {
        if (code == null) {
            return NO;
        }
        for (YesNo value : values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return NO;
    }

    /**
     * 根据 Boolean 获取枚举
     */
    public static YesNo fromBoolean(Boolean bool) {
        return Boolean.TRUE.equals(bool) ? YES : NO;
    }

    /**
     * 判断是否为"是"
     */
    public static boolean isYes(Integer code) {
        return YES.getCode().equals(code);
    }

    /**
     * 判断是否为"否"
     */
    public static boolean isNo(Integer code) {
        return NO.getCode().equals(code);
    }
}