package com.zcloud.entity;

/**
 * 说明：TODO
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */

import com.zcloud.util.CodeMessageEnum;
import org.apache.http.HttpStatus;

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

/**
 * @description: R类
 **/
public class Result extends HashMap<String, Object> {
    /**
     * 序列ID
     */
    private static final long serialVersionUID = 1L;

    /**
     * R的无参构造, 初始化信息
     */
    public Result() {
        put("code", 0);
        put("msg", "success");
        put("result", "success");
    }

    /**
     * error1: 返回默认error
     *
     * @return 返回默认error
     */
    public static Result error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常, 请联系管理员");
    }

    /**
     * error2
     *
     * @param msg 错误信息
     * @return 返回自定义信息的error
     */
    public static Result error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static Result error(CodeMessageEnum codeMessageEnum, String uri) {
        return error(codeMessageEnum.getCode(), codeMessageEnum.getMessage(), uri);
    }

    /**
     * 原先R的code和msg被覆盖
     *
     * @param code 错误码
     * @param msg  错误信息
     * @return 自定义的错误码和错误信息
     */
    public static Result error(int code, String msg) {
        Result r = new Result();
        r.put("code", code);
        r.put("result", "error");
        r.put("msg", msg);
        return r;
    }

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

    public static Result error(String code, String msg, String uri) {
        Result r = new Result();
        r.put("code", code);
        r.put("result", "error");
        r.put("msg", msg);
        r.put("uri", uri);
        return r;
    }

    /**
     * ok1
     * 加入了msg
     *
     * @param msg
     * @return
     */
    public static Result ok(String msg) {
        Result r = new Result();
        r.put("msg", msg);
        return r;
    }

    /**
     * ok2: 加入了map
     *
     * @param map
     * @return
     */
    public static Result ok(Map<String, Object> map) {
        Result r = new Result();
        r.putAll(map);
        return r;
    }

    /**
     * ok3: 直接返回"0", "success"
     *
     * @return
     */
    public static Result ok() {
        return new Result();
    }

    /**
     * 放入自定义的key和value, 然后返回
     *
     * @param key
     * @param value
     * @return
     */
    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 得到这个对象的code
     *
     * @return
     */
    public Integer getCode() {
        return (Integer) this.get("code");
    }
}



