package com.blueland.result;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author sweet
 */
public class R<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = 200;

    /** 失败 */
    public static final int FAIL = 500;

    private String resultMsg;
    private int resultCode;
    private T result;

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

    public static <T> R<T> ok(T data)
    {
        return restResult(data, SUCCESS, null);
    }

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

    public static <T> R<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data, FAIL, null);
    }

    public static <T> R<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T result, int resultCode, String resultMsg)
    {
        R<T> apiResult = new R<>();
        apiResult.setResultCode(resultCode);
        apiResult.setResult(result);
        apiResult.setResultMsg(resultMsg);
        return apiResult;
    }

    public int getResultCode()
    {
        return resultCode;
    }

    public void setResultCode(int code)
    {
        this.resultCode = code;
    }

    public String getResultMsg()
    {
        return resultMsg;
    }

    public void setResultMsg(String msg)
    {
        this.resultMsg = msg;
    }

    public T getResult()
    {
        return result;
    }

    public void setResult(T data)
    {
        this.result = data;
    }
}
