package cn.hanghui.outapi.platform.common.exception;

import cn.hanghui.outapi.platform.common.entity.rest.RestCode;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.common.exception
 * @class BusinessException
 * @date 2024/3/5 下午4:03
 * @description
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BusinessException extends RuntimeException {

    public int code;

    private String msg;

    private Exception sysException;

    public BusinessException(Exception sysException) {
        this.code = RestCode.FAIL.getCode();
        this.msg = RestCode.FAIL.getMsg();
        this.sysException = sysException;
    }

    public BusinessException(String msg, Exception sysException) {
        if (sysException instanceof BusinessException) {
            BusinessException e = (BusinessException) sysException;
            this.msg = e.msg;
            this.code = e.code;
        } else {
            this.code = RestCode.FAIL.getCode();
            this.msg = msg;
        }
        this.sysException = sysException;
    }

    public BusinessException() {
        this.code = RestCode.FAIL.getCode();
        this.msg = RestCode.FAIL.getMsg();
    }

    public BusinessException(String msg) {
        this.code = RestCode.FAIL.getCode();
        this.msg = msg;
    }

    public BusinessException(int code ,String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BusinessException(RestCode respCodeEnum) {
        this.code = respCodeEnum.getCode();
        this.msg = respCodeEnum.getMsg();
    }

    @Override
    public String getMessage() {
        return code + ":" + msg;
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        if (ObjectUtil.isNotEmpty(sysException) && !(sysException instanceof BusinessException)) {
            return sysException;
        }
        return this;
    }


}
