package com.friday.custody.utils;

import com.friday.custody.enums.RetCodeEnum;

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

public class R<T> extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    private T data;

    public T getData() {
        return data;
    }

    public R setData(T data) {
        this.data = data;
        return this;
    }

    public R() {
        put("code", RetCodeEnum.SUCCESS.getCode());
    }

    public R(String msg) {
        put("code", RetCodeEnum.SUCCESS.getCode());
        put("msg", msg);
    }
    public R(int code,String msg) {
        put("code", code);
        put("msg", msg);
    }

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

    public static R error(String msg) {
        return error(500, msg);
    }

    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 ok(String msg) {
        R r = new R();
        r.put("code", RetCodeEnum.SUCCESS.getCode());
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.put("code", RetCodeEnum.SUCCESS.getCode());
        r.putAll(map);
        return r;
    }

    public static R ok() {
        R r = new R();
        r.put("code", RetCodeEnum.SUCCESS.getCode());
        return r;
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}