package com.sw.appcloudv1.entities;

/**
 * Created by shiwn on 2022/2/23 16:15
 */
public class ResultData<T> {
    /**
     * 默认：0成功，1失败，205结果为null，500系统异常
     */
    private Integer code;
    private String msg;
    private T data;

    public String getMsg() {
        return msg;
    }

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

    public Integer getCode() {
        return code;
    }

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

    public T getData() {
        return data;
    }

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

    public ResultData() {
        this.code = 0;
        this.msg = "操作成功";
    }

    public ResultData(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultData(Integer code, T obj) {
        this.code = code;
        this.data = obj;
    }

    private ResultData(T data) {
        this.code = 0;
        this.msg = "操作成功";
        this.data = data;
    }


    /**
     * 操作成功，无返回数据
     *
     * @return 0
     */
    public static ResultData success() {
        return new ResultData();
    }

    /**
     * @Description: 操作执行成功，并判断返回值
     * @Param: [data]
     * @Author: shiwn
     * @Date: 2020/8/3 15:56
     */
    public static ResultData success(Object data) {
        //  判断空值
        if (data != null) {
            return new ResultData(data);
        } else {
            return ResultData.emptyData();
        }
    }


    /**
     * 操作失败
     *
     * @param code code
     * @param msg  操作信息
     * @return ResultData
     */
    public static ResultData fail(Integer code, String msg) {
        return new ResultData(code, msg);
    }

    /**
     * @Description: 操作成功，但返回值为null
     * @Param: [data]
     * @Author: shiwn
     * @Date: 2020/8/3 15:57
     */
    public static ResultData emptyData() {
        return new ResultData(205, "查询结果为空");
    }

    public static ResultData fail(String msg) {
        return new ResultData(1, msg);
    }

    public static ResultData fail() {
        return new ResultData(1, "操作失败");
    }
}
