package com.ruoyi.common.utils;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;


/**
 * @author yangjian
 * @Classname R
 * @Description 响应信息主体
 */
@Setter
@Getter
@ToString
@Accessors(chain = true)
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    private static final int SUCCESS_CODE = 200;

    private static final int FAIL_CODE = 500;

    private int code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return result(SUCCESS_CODE, "操作成功");
    }

    public static <T> R<T> ok(T data) {
        return result(SUCCESS_CODE, "操作成功", data);
    }

    public static <T> R<T> error() {
        return result(FAIL_CODE, "未知异常，请联系管理员");
    }

    public static <T> R<T> error(T data) {
        return result(FAIL_CODE, "未知异常，请联系管理员", data);
    }

    public static <T> R<T> error(String msg) {
        return result(FAIL_CODE, msg);
    }

    public static <T> R<T> error(int code, String msg) {
        return result(code, msg);
    }

    private static <T> R<T> result(int code, String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    private static <T> R<T> result(int code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

}
