package com.tzy.common.dto;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

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

/**
 * result
 */
@Data
@ToString
@NoArgsConstructor
public class Result implements Serializable {

    private Integer code = 0;

    private String msg = "SUCCESS";

    private boolean success = true;

    private Map<String ,Object> data = new HashMap<>();

    public static Result ok(){
        return new Result();
    }

    public static Result ok(String msg){
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }

    public static Result ok(Integer code,String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    public static Result error(){
        Result result = new Result();
        result.setCode(-1);
        result.setMsg("操作失败");
        result.setSuccess(false);
        return result;
    }
    public static Result error(Integer code,String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setSuccess(false);
        return result;
    }

    public Result put(String key, Object value){
        if(null == this.data) this.data = new HashMap<>();
        this.data.put(key,value);
        return this;
    }

    public String getString(String key){
        if(null == this.data) return null;
        return (String) this.data.get(key);
    }

    public Object get(String key){
        if(null == this.data) return null;
        return this.data.get(key);
    }


}
