package com.qy.worksheetsystem.model;

import java.util.HashMap;
import java.util.Map;

/**
 * @author he
 * @date 2022/2/28
 * @description  用于返回前端json数据的工具类
 */
public class MessageResult {

    // 状态码
    private int code;
    // 提示信息
    private String msg;

    // 封装有效数据
    private Map<String, Object> data = new HashMap<String, Object>();

    public static MessageResult success() {
        MessageResult result = new MessageResult();
        result.setCode(200);
        result.setMsg("success");
        return result;
    }

    public static MessageResult fail() {
        MessageResult result = new MessageResult();
        result.setCode(400);
        result.setMsg("fail");
        return result;
    }

    public static MessageResult noPermission() {
        MessageResult result = new MessageResult();
        result.setCode(401);
        result.setMsg("no permission");
        return result;
    }

    public static MessageResult error() {
        MessageResult result = new MessageResult();
        result.setCode(500);
        result.setMsg("error");
        return result;
    }

    public static MessageResult error(String error) {
        MessageResult result = new MessageResult();
        result.setCode(404);
        result.setMsg(error);
        return result;
    }

    public static MessageResult code(int code){
        MessageResult result = new MessageResult();
        result.setCode(code);
        result.setMsg("exception");
        return result;
    }

    public MessageResult add(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public Object getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}