package com.yxcheng.token.common.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.exception.MyBaseException;
import com.yxcheng.token.utils.StringUtil;

/**
 * 返回结果类，封装了统一的返回json结果格式，使用方法参考下面的例子: <br/>
 * {"data":[],"errorCode":-1,"errorMsg":"系统异常，请稍后再试","success":false} <br/>
 * {"data":[{"adress":"beijing","age":20,"name":"test"},{"adress":"shanghai","age":30,"name":"abc"}],"errorCode":0,"errorMsg":"","success":true}
 * <br/>
 * {"data":[{"adress":"hangzhou","age":50,"name":"ttt"}],"errorCode":0,"errorMsg":"","success":true} <br/>
 * <p>
 * 使用方法举例：<br/>
 * <p>
 * <b>1、返回异常结果</b><br/>
 * SystemException e = new SystemException(SystemErrorEnum.SYSTEM_ERROR);<br/>
 * BWJsonResult result = new BWJsonResult(e);<br/>
 * System.out.println(result.toString());<br/>
 * <p>
 * <b>2、返回List正常结果</b><br/>
 * public Class ProductVO extends BaseJsonModel { ... } 定义bean对象，必须继承基类 BaseJsonModel <br/>
 * List &lt;ProductVO&gt list = new ArrayList&lt;ProductVO&gt();<br/>
 * BWJsonResult &lt;ProductVO&gt result = new BWJsonResult&lt;ProductVO&gt(list);<br/>
 * System.out.println(result.toString());<br/>
 * <p>
 * <b>3、返回Object正常结果</b><br/>
 * public Class ProductVO extends BaseJsonModel { ... } 定义bean对象，必须继承基类 BaseJsonModel <br/>
 * ProductVO m3 = new ProductVO();<br/>
 * BWJsonResult &lt;ProductVO&gt result = new BWJsonResult&lt;ProductVO&gt(m3);<br/>
 * System.out.println(result.toString());<br/>
 * 
 * @author dwh
 * @version
 * @since JDK 1.8
 * @see BaseJsonModel
 */
public class JsonResult<T> extends BaseJsonModel
{
    private static final long serialVersionUID = -7532190660864165247L;
    
    /**
     * 返回值的成功标志
     * 判断反馈结构是成功还是失败，一律根据此字段。
     */
    @JSONField
    private boolean success = true;
    
    /**
     * 错误码，成功时为0，失败时由SystemErrorEnum枚举定义
     */
    @JSONField
    private String errorCode = "0";
    
    /**
     * 信息字段，成功时开发者自填，失败时由SystemErrorEnum枚举定义
     */
    @JSONField
    private String message = "成功";

    @JSONField
    private String retrySecond = "";
    
    /**
     * 返回值list,所有的返回对象值都在data中，它是一个集合
     */
    @JSONField
    private T data = null;
    
    /**
     *
     * BaseJsonResult:(构造方法，入参为自定义异常). <br/>
     * 如果异常不为空，则success设置为false，返回异常码和异常信息
     *
     * @author wuyuegang
     * @param exception
     * @since JDK 1.8
     */
    public JsonResult(MyBaseException exception)
    {
        if (exception != null)
        {
            this.success = false;
            this.errorCode = exception.getErrorCode();
            this.message = exception.getErrorMsg();
        }
    }



    public JsonResult(){

    }

    
    /**
     * 
     * Creates a new instance of BWJsonResult.
     *
     * @param data
     */
    public JsonResult(T data)
    {
        this.data = data;
    }


    public JsonResult errorResponse(ResultEnum errorEnum){
        this.errorCode = errorEnum.getCode();
        this.message = errorEnum.getMessage();
        this.success = false;
        return this;
    }

    public JsonResult errorResponse(ResultEnum errorEnum, String errorCode, String errorMessage, String retrySecond){
        this.errorCode = errorEnum.getCode();
        this.message = errorEnum.getMessage();
        if(StringUtil.isNotEmpty(errorMessage)) {
            this.message = "(" + errorCode + ")" + errorMessage;
        }
        this.retrySecond = retrySecond;
        this.success = false;
        return this;
    }


    public JsonResult success(ResultEnum errorEnum){
        this.errorCode = errorEnum.getCode();
        this.message = errorEnum.getMessage();
        this.success = true;
        return this;
    }

    public JsonResult success(String errorCode,String message){
        this.errorCode = errorCode;
        this.message = message;
        this.success = true;
        return this;
    }

    public JsonResult error(String errorCode,String message){
        this.errorCode = errorCode;
        this.message = message;
        this.success = false;
        return this;
    }

    public static JsonResult errorResult(String errorCode,String message){
        JsonResult result = new JsonResult();
        result.setErrorCode(errorCode);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }


    public static <T>JsonResult<T> erroCrRpcResult(CrRpcResult<T> crRpcResult){
        JsonResult<T> result = new JsonResult<T>();
        result.setErrorCode(crRpcResult.getCode());
        result.setMessage(crRpcResult.getMessage());
        result.setSuccess(false);
        return result;
    }


    public JsonResult successResponse() {
        return success(ResultEnum.ok);
    }
    
    public boolean isSuccess()
    {
        return success;
    }
    
    public void setSuccess(boolean success)
    {
        this.success = success;
    }
    
    public String getErrorCode()
    {
        return errorCode;
    }

    public String getMessage() {
        return message;
    }

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

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public T getData()
    {
        return data;
    }

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

    public String getRetrySecond() {
        return retrySecond;
    }

    public void setRetrySecond(String retrySecond) {
        this.retrySecond = retrySecond;
    }
}
