package cn.axzo.msg.center.notices.common.exception;

import cn.axzo.msg.center.notices.common.enums.ReturnCodeEnum;
import lombok.Getter;
import lombok.Setter;

/**
 * 业务异常
 *
 * @author zhaoyong_sh
 * @see BizException
 * @since 2021-05-17 15:56
 */
@Getter
@Setter
public class BizException extends RuntimeException {

    private Integer bizCode;

    private String message;

    /**
     * 需返回的业务数据
     */
    private Object data;

    public BizException(Integer bizCode) {
        this.bizCode = bizCode;
    }

    public BizException(ReturnCodeEnum returnCode) {
        super(returnCode.getMessage());
        this.bizCode = returnCode.getCode();
        this.message = returnCode.getMessage();
    }

    public BizException(ReturnCodeEnum returnCode, String message) {
        super(returnCode.getMessage());
        this.bizCode = returnCode.getCode();
        this.message = message;
    }

    public BizException(Integer bizCode, Throwable cause) {
        super(null, cause);
        this.bizCode = bizCode;
    }

    public BizException(Integer bizCode, String message, Object data) {
        super(message);
        this.bizCode = bizCode;
        this.message = message;
        this.data = data;
    }

    public BizException(Integer bizCode, String message, Object data, Throwable cause) {
        super(message, cause);
        this.bizCode = bizCode;
        this.message = message;
        this.data = data;
    }

    public static void error(boolean b, ReturnCodeEnum returnCode){
        if(!b){
            throw new BizException(returnCode.getCode(), returnCode.getMessage(), null);
        }
    }

    public static void error(boolean b, ReturnCodeEnum returnCode, String message){
        if(!b){
            throw new BizException(returnCode.getCode(), message, null);
        }
    }

    public static void error(boolean b, Integer bizCode, String message) {
        if(!b){
            throw new BizException(bizCode, message, null);
        }
    }

    public static void error(boolean b, Integer bizCode, String message, Object data) {
        if(!b){
            throw new BizException(bizCode, message, data);
        }
    }

    public static void error(ReturnCodeEnum returnCode){
        throw new BizException(returnCode.getCode(), returnCode.getMessage(), null);
    }

}
