package com.sky.ioc.tool;


import com.sky.ioc.constant.Constant;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/***
 * 接口返回消息封装管理工具
 * @author LunCe
 */
@Data
@Slf4j
public class ReturnMsg<T> {

    private int code;

    private String msg;

    private T data;

    public static <T> ReturnMsg<T> ok()
    {
        return restResult(null, Constant.MSG_CODE.SUCCESS, "操作成功");
    }

    public static <T> ReturnMsg<T> ok(T data)
    {
        return restResult(data, Constant.MSG_CODE.SUCCESS, "操作成功");
    }

    public static <T> ReturnMsg<T> ok(T data, String msg)
    {
        return restResult(data, Constant.MSG_CODE.SUCCESS, msg);
    }

    public static <T> ReturnMsg<T> fail()
    {
        return restResult(null, Constant.MSG_CODE.SERVER_ERROR, "操作失败");
    }

    public static <T> ReturnMsg<T> fail(String msg)
    {
        return restResult(null, Constant.MSG_CODE.SERVER_ERROR, msg);
    }

    public static <T> ReturnMsg<T> fail(T data)
    {
        return restResult(data, Constant.MSG_CODE.SERVER_ERROR, "操作失败");
    }

    public static <T> ReturnMsg<T> fail(T data, String msg)
    {
        return restResult(data, Constant.MSG_CODE.SERVER_ERROR, msg);
    }

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

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

}
