package com.caimei.www.pojo;

import lombok.Data;
import java.io.Serializable;

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/7/14
 */
@Data
public class JsonModel<T> implements Serializable {
    /** 状态码 */
    private int code;
    /** 提示信息 */
    private String msg;
    /** 返回的数据 */
    private T data;

    private JsonModel() {}

    private JsonModel(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private JsonModel(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static JsonModel success() {
        return new JsonModel<>(0, "操作成功");
    }

    public static<T> JsonModel<T> success(T data) {
        return new JsonModel<>(0, "操作成功", data);
    }

    public static<T> JsonModel<T> success(String msg, T data) {
        return new JsonModel<>(0, msg, data);
    }

    public static<T> JsonModel<T> success(int code, String msg, T data) {
        return new JsonModel<>(code, msg, data);
    }

    public static JsonModel error() {
        return new JsonModel<>(-1, "操作失败");
    }

    public static JsonModel error(String msg) {
        return new JsonModel<>(-1, msg);
    }

    public static JsonModel error(int code, String msg) {
        return new JsonModel<>(code, msg);
    }

    public static<T> JsonModel<T> error(T data) {
        return new JsonModel<>(-1, "操作失败", data);
    }

    public static<T> JsonModel<T> error(String msg, T data) {
        return new JsonModel<>(-1, msg, data);
    }

    public static<T> JsonModel<T> error(int code, String msg, T data) {
        return new JsonModel<>(code, msg, data);
    }

    @Override
    public String toString() {
        return "JsonModel{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }

    private static final long serialVersionUID = 1L;
}
