package cn.teammodel.common;

import lombok.Data;

import java.io.Serializable;

@Data
public class R<T> implements Serializable {

    private int code;

    private T data;

    private String message;

    public R(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public R(int code, T data) {
        this(code, data, "");
    }

    public R(ErrorCode errorCode) {
        this(errorCode.getCode(), null, errorCode.getMessage());
    }
    public R(ErrorCode errorCode, T data) {
        this(errorCode.getCode(), data, errorCode.getMessage());
    }

    public static <T> R<T> success(T data) {
        return new R<>(ErrorCode.SUCCESS, data);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(ErrorCode.SYSTEM_ERROR.getCode(), null, msg);
    }

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

    public static <T> R<T> error(ErrorCode errorCode) {
        return new R<>(errorCode);
    }
}