package com.chy.borrow.exception;

import lombok.Data;

/**
 * @author ：沈晔浩
 * @date ：Created in 2021/9/14 17:22
 * @description：异常消息返回
 */
@Data
public class Result<T> {
    //是否成功
    private Boolean success;
    //状态码
    private Integer code;
    //提示信息
    private String msg;
    //数据
    private T data;

    public Result() {

    }

    //自定义返回结果的构造方法
    public Result(Boolean success, Integer code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result successResult() {
        return new Result(true, 200, "success", null);
    }

    public static Result successMsg(String msg) {
        return new Result(true, 200, msg, null);
    }

    public static Result successResult(Object data) {
        return new Result(true, 200, "success", data);
    }

    public static Result errorResult(ErrorEnum errorEnum) {
        return new Result(false, errorEnum.getErrorCode(), errorEnum.getErrorMsg(), null);
    }


    //自定义异常返回的结果
    public static Result defineError(DefinitionException de) {
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        result.setData(null);
        return result;
    }

    //其他异常处理方法返回的结果
    public static Result otherError(ErrorEnum errorEnum) {
        Result result = new Result();
        result.setMsg(errorEnum.getErrorMsg());
        result.setCode(errorEnum.getErrorCode());
        result.setSuccess(false);
        result.setData(null);
        return result;
    }

    //404异常处理
    public static Result Error_404(Integer code, String msg) {
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(code);
        result.setSuccess(false);
        result.setData(null);
        return result;
    }
}
