package com.ankaibei.workFlow.webadmin.ankaibei.vo;

import com.alibaba.fastjson.JSON;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CodeEnum;
import lombok.Data;

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

/**
 * @author
 * @version 2019-10-08 16:17
 * Description:返回信息
 */
@Data
public class ResMsg<T> implements Serializable {

    private String code;

    private String msg;

    private T data;

    public ResMsg() {
    }

    public ResMsg(String code) {
        this.code = code;
    }

    public ResMsg(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResMsg(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public ResMsg(CodeEnum CodeEnum) {
        this(CodeEnum.getCode(), CodeEnum.toString());
    }

    public ResMsg(CodeEnum CodeEnum, T data) {
        this(CodeEnum.getCode(), CodeEnum.toString(), data);
    }

    public static String toJsonString(CodeEnum CodeEnum) {
        return JSON.toJSONString(new ResMsg(CodeEnum));
    }

    public static String toJsonString(CodeEnum CodeEnum, Object data) {
        return JSON.toJSONString(new ResMsg(CodeEnum, data));
    }

    public static ResMsg newInstance(CodeEnum CodeEnum, Object data) {
        return new ResMsg(CodeEnum, data);
    }

    public static ResMsg newInstance(String enumCode, String enumMsg, Object data) {
        return new ResMsg(enumCode, enumMsg, data);
    }

    public static ResMsg error(CodeEnum CodeEnum, String message, Object data) {
        return new ResMsg(CodeEnum.getCode(), message, data);
    }

    public static ResMsg error(String enumCode, String enumMsg) {
        return new ResMsg(enumCode, enumMsg, null);
    }

    public static ResMsg newInstance(CodeEnum CodeEnum) {
        return new ResMsg(CodeEnum, null);
    }

    public static String OK() {
        return toJsonString(CodeEnum.OK);
    }

    public static Map<Object, Object> getMap(Object content, Object totalElements) {
        Map<Object, Object> map = new HashMap<>();
        map.put("content", content);
        map.put("totalElements", totalElements);
        return map;
    }

}

