package com.cyksj.common.exception;


import com.cyksj.enums.GatewayApiCode;
import lombok.Getter;
import lombok.Setter;

/**
 * @author chan
 * @className BusinessRuntimeException
 * @description 业务业务异常类
 * @date 2020/11/4 4:34 下午
 */
@Getter
@Setter
public final class BusinessRuntimeException extends RuntimeException {

    private static final long serialVersionUID = 4236266714060887509L;

    public static final String ERR_DEF = "DEF001";

    private String code;

    private String msg;

    /**
     * 传入code 和Msg 构建BusinessException对象
     *
     * @param code
     * @param msg
     */
    public BusinessRuntimeException(String code, String msg) {
        super(msg);
        this.code = code;
    }

    public BusinessRuntimeException(Integer code, String msg) {
        super(msg);
        this.code = code.toString();
    }

    public BusinessRuntimeException(String msg) {
        super(msg);
        this.code = GatewayApiCode.METADATA_SYSTEM_ERROR.getCode().toString();
    }

    public BusinessRuntimeException(GatewayApiCode apiCode) {
        super(apiCode.getMsg());
        this.code = apiCode.getCode().toString();
    }


    /***
     * 提供一个简便的构建BusinessException对象方法
     *
     * @param patterm
     *            异常信息模板 如 xxxx {} xxxx
     * @param args
     *            传入模板所需要的参数
     * @return
     */
    public static BusinessRuntimeException getInstance(String patterm, Object... args) {
        String msg = java.text.MessageFormat.format(patterm, args);
        return new BusinessRuntimeException(ERR_DEF, msg);
    }

    public static BusinessRuntimeException getInstance(String msg) {
        return new BusinessRuntimeException(ERR_DEF, msg);
    }

    public static BusinessRuntimeException getGatewayApiCode(GatewayApiCode apiCode) {
        return new BusinessRuntimeException(apiCode);
    }

    /***
     * 提供一个简便的构建BusinessException带Exception的对象方法
     *
     * @param e
     *            异常参数
     * @param patterm
     *            异常信息模板 如 xxxx {} xxxx
     * @param args
     *            传入模板所需要的参数
     * @return
     */
    public static BusinessRuntimeException getInstance(Throwable e, String patterm, Object... args) {
        String msg = java.text.MessageFormat.format(patterm, args);
        return new BusinessRuntimeException(ERR_DEF, msg, e);
    }



    public BusinessRuntimeException(String code, String msg, Throwable e) {
        super(msg, e);
        this.code = code;
        this.msg = msg;
    }

    public BusinessRuntimeException(Throwable e, String msg) {
        super(e);
        this.msg = msg;
    }


}
