package com.dayou.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 类说明：统一返回对象
 *
 * @author: wucl
 * @since: 2022/11/28
 * created with IntelliJ IDEA.
 */
@Data
public class RestResponse<T> implements Serializable {
    /**
     * code码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public RestResponse() {
    }

    public RestResponse(T t) {
        this.data = t;
    }


    public static <T> RestResponse<T> success(T t, String msg, Integer code) {
        RestResponse r2 = new RestResponse<T>(t);
        r2.code = code;
        r2.msg = msg;
        return r2;
    }

    public static <T> RestResponse error(T t, String msg, Integer code) {
        RestResponse r2 = new RestResponse<T>(t);
        r2.code = code;
        r2.msg = msg;
        return r2;
    }

    public static <T> RestResponse<T> success(T t) {
        return success(t, "success", 200);
    }

    public static <T> RestResponse<T> success() {
        return success(null, "success", 200);
    }

    public static <T> RestResponse<T> approval() {
        return success(null, "success", 250);
    }

    public static <T> RestResponse<T> data(T t) {
        return success(t);
    }

    public static <T> RestResponse<T> data(T t, Integer code) {
        return success(t, null, code);
    }

    public static <T> RestResponse error(String msg) {
        return error(null, msg, 500);
    }

    public static <T> RestResponse error(String msg, Integer code) {
        return error(null, msg, code);
    }

    public static <T> RestResponse error() {
        return error(null, "发生了一些错误", 500);
    }


    public static <T> RestResponse<T> success(T t, String msg) {
        return success(t, msg, 200);
    }
}
