package com.ningdatech.carapi.common.constant;

import java.util.Objects;

import lombok.Getter;

/**
 * <p>
 * GenderEnum
 * </p>
 *
 * @author WendyYang
 * @since 15:15 2022/11/4
 */
@Getter
public enum GenderEnum {

    /**
     * 性别编码定义
     */
    WOMAN(0, "女"),
    MAN(1, "男"),
    UNKNOWN(2, "未知"),
    OTHER(3, "其他");

    private final int code;
    private final String value;

    GenderEnum(int code, String value) {
        this.code = code;
        this.value = value;
    }

    public boolean eq(String val) {
        return this.getValue().equals(val);
    }

    public boolean eq(int code) {
        return this.getCode() == code;
    }

    public static GenderEnum getByCode(int code) {
        for (GenderEnum genderEnum : GenderEnum.values()) {
            if (genderEnum.getCode() == code) {
                return genderEnum;
            }
        }
        return GenderEnum.UNKNOWN;
    }

    public static String getDescByCode(int code) {
        for (GenderEnum genderEnum : GenderEnum.values()) {
            if (genderEnum.getCode() == code) {
                return genderEnum.getValue();
            }
        }
        return GenderEnum.UNKNOWN.getValue();
    }

    public static GenderEnum getByValue(String value) {
        if (Objects.isNull(value)) {
            return GenderEnum.UNKNOWN;
        }
        for (GenderEnum genderEnum : GenderEnum.values()) {
            if (genderEnum.getValue().contains(value)) {
                return genderEnum;
            }
        }
        return GenderEnum.UNKNOWN;
    }

}
