package com.muyu.enterprise.util;

import org.apache.http.HttpStatus;

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

/**
 * @Author mengyu
 * @Description 自定义RESTFUL结果集封装类
 * @Version 1.0
 * @Data 2024-09-05 16:39:02
 */

public class R extends HashMap<String, Object> {
    public R() {
        //默认创建的R对象中包含了公共的属性
        put("code", HttpStatus.SC_OK);
        put("msg", "success");
    }

    /*
     * 覆盖继承的put函数，添加Key-Value数据
     */
    public R put(String key, Object value) {
        super.put(key, value);
        //把自己返回，用于链式调用
        return this;
    }

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

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static R error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }
}
