package com.cf.imes.framework.common.exception;

import com.cf.imes.framework.common.exception.enums.ServiceErrorCodeRange;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Locale;

/**
 * 业务逻辑异常 Exception
 */
@Data
@EqualsAndHashCode(callSuper = true)
public final class ServiceException extends RuntimeException {

    /**
     * 业务错误码
     *
     * @see ServiceErrorCodeRange
     */
    private Integer code;
    /**
     * 错误提示
     */
    private String message;

    private Locale locale;

    /**
     * 占位参数
     */
    private transient Object[] args;

    /**
     * 异常需要的回调数据
     */
    private ErrorData errorData;

    /**
     * 空构造方法，避免反序列化问题
     */
    public ServiceException() {
    }

    public ServiceException(ErrorCode errorCode, ErrorData errorData, Object... args) {
        this.code = errorCode.getCode();
        this.message = errorCode.getMsg();
        this.errorData = errorData;
        this.args = args;
    }

    public ServiceException(ErrorCode errorCode, Object... args) {
        this.code = errorCode.getCode();
        this.message = errorCode.getMsg();
        this.args = args;
    }

    public ServiceException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ServiceException(ErrorCode errorCode, Locale locale, Object... args) {
        this.code = errorCode.getCode();
        this.locale = locale;
        this.message = errorCode.getMsg();
        this.args = args;
    }

    public ServiceException(Integer code, Locale locale, String message) {
        this.code = code;
        this.locale = locale;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public ServiceException setCode(Integer code) {
        this.code = code;
        return this;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public ServiceException setMessage(String message) {
        this.message = message;
        return this;
    }

}
