package com.usoftchina.bi.core.base;

import com.fasterxml.jackson.databind.JavaType;
import com.usoftchina.bi.core.utils.JsonUtils;

import java.io.Serializable;

public class RepEntity<T> implements Serializable {
    /**
     * 错误码
     */
    private int code;
    /**
     * 错误消息
     */
    private String msg;
    /**
     * 结果
     */
    private T data;

    public RepEntity() {
    }

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

    public RepEntity(RepCode repCode) {
        this.code = repCode.getCode();
        this.msg = repCode.getMsg();
    }

    public RepEntity(RepCode repCode, T data) {
        this(repCode);
        if (data != null){
            this.data = data;
        }
    }

    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 T getData() {
        return data;
    }

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

    /**
     * json字符串转换RepEntity对象
     *
     * @param jsonString
     * @param <T>
     * @return
     */
    public static <T> RepEntity<T> fromJsonString(String jsonString,  Class<T> targetClass) {
        return JsonUtils.fromJsonString(jsonString, RepEntity.class, targetClass);
    }

    /**
     * json字符串转换RepEntity对象
     *
     * @param jsonString
     * @param targetType
     * @param <T>
     * @return
     */
    public static <T> RepEntity<T> fromJsonString(String jsonString, JavaType targetType) {
        return JsonUtils.fromJsonString(jsonString, JsonUtils.getJavaType(RepEntity.class, targetType));
    }
}
