package com.jsowell.thirdparty.lianlian.common;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.jsowell.common.enums.thirdparty.ThirdPartyReturnCodeEnum;
import com.jsowell.thirdparty.lianlian.common.enums.ResultCode;

/**
 * 接口返回类
 *
 * @author 联联充电
 */
public class CommonResult<T> {

    /**
     * 状态码
     */
    @JsonProperty(value = "Ret")
    private long ret;

    /**
     * 响应信息
     */
    @JsonProperty(value = "Msg")
    private String msg;

    /**
     * 返回数据
     */
    @JsonProperty(value = "Data")
    private T data;

    @JsonProperty(value = "Sig")
    private String sig;

    protected CommonResult() {
    }

    protected CommonResult(long ret, String msg, T data, String sig) {
        this.ret = ret;
        this.msg = msg;
        this.data = data;
        this.sig = sig;
    }


    public static <T> CommonResult<T> success(Integer ret, String msg, T data, String sig) {
        return new CommonResult<T>(ret, msg, data, sig);
    }

    /**
     * 失败返回结果
     *
     * @param resultCode 错误码
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failed(ThirdPartyReturnCodeEnum resultCode) {
        return new CommonResult<T>(Long.parseLong(resultCode.getRet()), resultCode.getMsg(), null, null);
    }

    /**
     * 失败返回
     *
     * @param code 错误码
     * @param msg  提示信息
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failed(long code, String msg) {
        return new CommonResult<T>(code, msg, null, null);
    }

    /**
     * 失败返回结果
     *
     * @param msg 提示信息
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failed(String msg) {
        return failed(ResultCode.ERROR.getCode(), msg);
    }


    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;
    }

    public long getRet() {
        return ret;
    }

    public void setRet(long ret) {
        this.ret = ret;
    }

    public String getSig() {
        return sig;
    }

    public void setSig(String sig) {
        this.sig = sig;
    }
}
