package com.sonic.lion.enums;

import com.sonic.common.exception.BizException;
import com.sonic.common.rpc.ApiResultCode;
import com.sonic.common.utils.MessageUtils;
import lombok.Getter;

/**
 * @description: 弹窗消息的定义
 * @author: code
 * @create: 2021-12-17 10:25
 **/
@Getter
public enum BizResultCode implements ApiResultCode {

    MISS_PARAM_ERROR("0001", "Missing parameter"),

    STATUS_ERROR("", "状态错误"),

    EXISTS_ERROR("", "数据已存在"),

    ;


    private final String errorCode;
    private final String errorMsg;

    BizResultCode(String errorCode, String errorMsg) {
        this.errorCode = getAppId() + errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * 得到 message 的值.
     *
     * @return 属性 message 的值.
     */
    @Override
    public String getErrorMsg() {
        return errorMsg;
    }

    /**
     * @return 当前服务的AppId
     */
    @Override
    public String getAppId() {
        return "1001";
    }

    /**
     * 校验方法
     *
     * @param expect
     */
    public void check(boolean expect) {
        if (expect) {
            throw new BizException(this.getErrorCode(), MessageUtils.get(this.name()));
        }
    }

    /**
     * 校验方法
     * @param expect
     * @param code
     * @param message
     */
    public static void check(boolean expect, String code, String message) {
        if (expect) {
            throw new BizException(code, message);
        }
    }
}
