package com.kfzs.duanduan.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  BaseMsg</p>
 * @ <p>Description:</p>
 * @ date:  2017/12/13 10:23
 * @ QQ:    315096953
 */

public class BaseMsg {

    private String msg;

    private Integer code = 0;

    private Integer total = 0;

    private Object data;

    public String getMsg() {
        return msg == null ? "" : msg;
    }

    public String getMessage(String defaultmsg) {
        return msg == null ? defaultmsg : msg;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }


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

    /**
     * 默认为0表示有错误
     *
     * @return @NotNull
     */
    public Integer getCode() {
        return code;
    }

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

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    /**
     * @return @NotNull
     */
    public Object getData() {
        if (data == null) {
            data = new Object();
        }
        return data;
    }

    /**
     * 失败返回null
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getData(Class<T> clazz) {
        if (data == null) {
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(data), clazz);
    }

    /**
     * 如果data里是数组，直接这样获取
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> List<T> getDatas(Class<T> clazz) {
        if (data == null) {
            return null;
        }
        return JSONArray.parseArray(JSONObject.toJSONString(data), clazz);
    }


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

}
