package com.prolog.eis.dto.result;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * @Author wangkang
 * @Description
 * @CreateTime 2021/7/5 10:20
 */
public class McsResult<T> implements Serializable {

    @ApiModelProperty(value = "是否成功")
    private boolean ret;
    @ApiModelProperty(value = "消息对象")
    private String message;
    @ApiModelProperty(value = "消息代码")
    private T data;

    public McsResult() {

    }

    @Override
    public String toString() {
        return "WmsResult{" +
                "ret=" + ret +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public static <T> McsResult<T> newInstance(boolean ret, String message) {
        return new McsResult<T>(ret, message, null);
    }

    public static <T> McsResult<T> newInstance(boolean ret, String message, T data) {
        return new McsResult<T>(ret, message, data);
    }

    public static <T> McsResult<T> newInstance(boolean ret, String code, String message, T data) {
        return new McsResult<T>(ret, code, message, data);
    }

    public McsResult(boolean ret, String message, T data) {
        this.ret = ret;
        this.message = message;
        this.data = data;
    }

    public McsResult(boolean ret, String code, String message, T data) {
        this.ret = ret;
        this.message = message;
        this.data = data;
    }

    public boolean isret() {
        return ret;
    }

    public void setret(boolean ret) {
        this.ret = ret;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public T getData() {
        return data;
    }

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

    public static <S> McsResult<S> parseJsonString(String jsonstr,
                                                   TypeReference<McsResult<S>> typeReference) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        McsResult<S> rest = mapper.readValue(jsonstr, typeReference);
        return rest;
    }

    public static <S> McsResult<S> parseJsonString(String jsonstr) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        McsResult<S> rest = mapper.readValue(jsonstr, new TypeReference<McsResult<S>>() {
        });
        return rest;
    }

    public static <S> McsResult<List<S>> parseJsonStringForList(String jsonstr) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        McsResult<List<S>> rest = mapper.readValue(jsonstr, new TypeReference<McsResult<List<S>>>() {
        });
        return rest;
    }


}
