package com.youchain.exception.handler;

import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
public class ApiResult {
    private Object data;//数据
    private String msg;//内容
    private String notify;//样式
    private int code;//编码

    public static ApiResult fail() {

        return result(400, "操作失败!", null);
    }

    public static ApiResult fail(int code, String msg, Object data) {
        return result(code, msg, data);
    }

    public static ApiResult success() {
        return result(200, "操作成功!", null);
    }

    public static ApiResult success(Object data) {
        return result(200, "操作成功!", data);
    }

    public static ApiResult success(String msg, Object data) {
        return result(200, msg, data);
    }

    public static ApiResult success(int code, String msg, Object data) {
        return result(code, msg, data);
    }

    public static ApiResult result(int code, String msg, Object data) {
        ApiResult rs = new ApiResult();
        if (code==200){
            rs.setNotify("success");
        }else {
            rs.setNotify("error");
        }
        rs.setCode(code);
        rs.setMsg(msg);
        rs.setData(data);
        return rs;
    }

    public HttpStatus getStatus() {
        return HttpStatus.valueOf(this.code);
    }
}
