package com.icloud.server.utils;


import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @description:  响应消息体
 * @author: zjbing
 * @create: 2021-01-13 17:04
 **/
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private long total;

    @Getter
    @Setter
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, CodeDefs.SUCCESS, "成功",0);
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, CodeDefs.SUCCESS, msg,0);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, CodeDefs.SUCCESS, "成功",0);
    }

    public static <T> R<T> ok(T data,long total) {
        return restResult(data, CodeDefs.SUCCESS, "成功",total);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CodeDefs.SUCCESS, msg,0);
    }

    public static <T> R<T> failed() {
        return restResult(null, CodeDefs.FAIL, "失败",0);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, CodeDefs.FAIL, msg,0);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, CodeDefs.FAIL, "失败",0);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, CodeDefs.FAIL, msg,0);
    }

    public static <T> R<T> failed(Integer code) {
        return restResult(null, code, "失败",0);
    }

    public static <T> R<T> failed(Integer code, String msg) {
        return restResult(null, code, msg,0);
    }

    private static <T> R<T> restResult(T data, int code, String msg,long total) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setTotal(total);
        return apiResult;
    }
}
