package com.tzy.s.wx.dto;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
//@ToString
public class Result<T> implements Serializable {
    private static long serializable = 1L;
    //請求狀態
    private boolean success = true;
    //請求狀態碼
    private Integer errcode = 0;
    //請求描述
    private String errmsg = "成功";
    //返回數據
    private Map data;

    private Integer total;

    public Result(){
    }
    public void setErrcode(Integer errcode){
        this.errcode = errcode;
        if(this.errcode != 0) this.success = false;
    }

    public Integer getErrcode(){
        if(this.errcode != 0) this.success = false;
        return this.errcode;
    }

    public boolean isSuccess(){
        return null != this.errcode && this.errcode != 0 ? false :true;
    }

    /**
     * 無參
     * 请求成功
     * @return
     */
    public static Result<Object> ok(){
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setErrcode(0);
        r.setErrmsg("成功");
        return r;
    }
    /**
     * 请求成功
     * @param map
     * @return
     */
    public static Result<Object> ok(Map map){
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setErrcode(0);
        r.setErrmsg("成功");
        r.setData(map);
        return r;
    }
    /**
     * 失败
     * @return
     */
    public static Result<Object> error(){
        Result<Object> r = new Result<Object>();
        r.setSuccess(false);
        r.setErrcode(-1);
        r.setErrmsg("请求失败");
        return r;
    }


    /**
     * 返回失败信息
     * @param code
     * @param msg
     * @return
     */
    public static Result<Object> error(Integer code,String msg){
        Result<Object> r = new Result<Object>();
        r.setSuccess(false);
        r.setErrcode(code);
        r.setErrmsg(msg);
        return r;
    }

    /**
     * map 集合put 数据
     * @param key
     * @param value
     * @return
     */
    public Result put(String key ,Object value){
        if(null == this.data ) this.data = new HashMap();
        this.data.put(key,value);
        return this;
    }

    /**
     * 取值
     * @param key
     * @return
     */
    public Object get(String key){
        if(null != this.data && !this.data.isEmpty()){
            return this.data.get(key);
        }else{
            JSONObject jsonObject = this.toJson();
            if(null != jsonObject) return jsonObject.get(key);
            else return null;
        }
    }
    /**
     * 取值
     * @param key
     * @return
     */
    public String getString(String key){
        if(null != this.data && !this.data.isEmpty()){
            return (String) this.data.get(key);
        }else{
            JSONObject jsonObject = this.toJson();
            if(null != jsonObject) return jsonObject.getString(key);
            else return null;
        }
    }

    /**
     * 转换为json 值
     * @return
     */
    public JSONObject toJson(){
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(this);
        return jsonObject;
    }



}
