package com.cpop.core.base;

import com.cpop.common.constant.Constants;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author DB
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;

    /**
     * 失败
     */
    public static final int FAIL = Constants.FAIL;

    private int code;

    private String msg;

    private T result;

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

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

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

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

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

    public static <T> R<T> fail(T result) {
        return restResult(result, FAIL, "失败");
    }

    public static <T> R<T> fail(T result, String msg) {
        return restResult(result, 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 code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setResult(result);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public T getResult() {
        return result;
    }

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