package com.dgt.graphdemo.result;

import com.dgt.graphdemo.exception.CommonBizException;
import com.dgt.graphdemo.exception.ExpCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author lugela
 * @Date 2017/10/27 下午10:59
 * restful接口通用返回结果
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 2988770413133778370L;
    /** 执行结果 */
 //   private boolean isSuccess;

    /** 错误码 */
   // private String errorCode;

    /** 错误原因 */
    private String message;

    /** 返回数据 */
    private T data;

    //状态码

    private String resultCode;

    /**
     * 返回成功的结果
     * @param data 需返回的结果
     * @param <T>
     * @return
     */
    public static <T> Result<T> newSuccessResult(T data){
        Result<T> result = new Result<>();
      //  result.isSuccess = true;
        result.data = data;
        return result;
    }

    public static <T> Result<T> successResult(T data){
        Result<T> result = new Result<>();
        result.resultCode ="0000";
        result.message = "成功";
        result.data = data;
        return result;
    }

    /**
     * 返回成功的结果
     * @param <T>
     * @return
     */
    public static <T> Result<T> newSuccessResult(){
        Result<T> result = new Result<>();
       // result.isSuccess = true;
        result.resultCode ="0000";
        result.message = "成功";
        return result;
    }

    /**
     * 返回失败的结果
     * PS：返回"未知异常"
     * @param <T>
     * @return
     */
    public static <T> Result<T> newFailureResult(){
        Result<T> result = new Result<>();
       // result.isSuccess = false;
        result.resultCode = ExpCodeEnum.UNKNOW_ERROR.getCode();
        result.message = ExpCodeEnum.UNKNOW_ERROR.getMessage();
        return result;
    }

    /**
     *
     * @param commonBizException 异常
     * @param <T>
     * @return
     */
    public static <T> Result<T> newFailureResult(CommonBizException commonBizException){
        Result<T> result = new Result<>();
       // result.isSuccess = false;
        result.resultCode = commonBizException.getCodeEnum().getCode();
        result.message = commonBizException.getCodeEnum().getMessage();
        return result;
    }

    /**
     * 返回失败的结果
     * @param commonBizException 异常
     * @param data 需返回的数据
     * @param <T>
     * @return
     */
    public static <T> Result<T> newFailureResult(CommonBizException commonBizException, T data){
        Result<T> result = new Result<>();
        //result.isSuccess = false;
        result.resultCode = commonBizException.getCodeEnum().getCode();
        result.message = commonBizException.getCodeEnum().getMessage();
        result.data = data;
        return result;
    }

}
