package cn.stylefeng.team.meta.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 TableAlterTypeEnum implements ReadableEnum<TableAlterTypeEnum> {

    ADD_COLUMN("ADD_COLUMN", "新增字段"),

    DROP_COLUMN("DROP_COLUMN", "删除字段"),

    CHANGE_COLUMN("CHANGE_COLUMN", "修改字段"),

    MODIFY_COLUMN("MODIFY_COLUMN", "修改字段的属性");

    @EnumValue
    @JsonValue
    private final String code;

    private final String message;

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

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

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

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

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

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

}