ResponseResult.java 6.05 KB
package com.yanzuoguang.util.vo;

import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 接口之间的通讯结果
 *
 * @author 颜佐光
 */
public class ResponseResult<T> extends BaseVo {

    private static final long serialVersionUID = -508702000019378459L;

    /**
     * 请求状态码
     */
    @ApiModelProperty(value = "返回状态", notes = "00表示成功,其他状态表示失败,具体含义参照具体实现接口", required = true, example = "00")
    private String code;

    /**
     * 服务消息
     */
    @ApiModelProperty(value = "错误消息", notes = "提示错误消息等等", required = true, example = "查询成功")
    private String message;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据", notes = "返回的具体数据", required = true)
    private T data;

    /**
     * 数据源,用于显示异常数据
     */
    @ApiModelProperty(value = "异常数据", notes = "当抛出异常时的数据,通常和code进行组合", required = true)
    private Object target;

    /**
     * 检查数据是否合法,不合法则抛出异常,合法则返回数据
     */
    public T check() {
        if (StringHelper.compare(code, ResultConstants.SUCCESS)) {
            return this.data;
        }
        throw new CodeException(this.code, this.message, this.target);
    }

    /**
     * 获取结果状态码
     *
     * @return
     */
    public String getCode() {
        return code;
    }

    /**
     * 设置请求结果状态码
     *
     * @param code
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * 获取结果消息
     *
     * @return
     */
    public String getMessage() {
        return message;
    }

    /**
     * 设置结果消息
     *
     * @param message
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 获取数据
     *
     * @return
     */
    public T getData() {
        return data;
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * 获取数据源
     *
     * @return 数据源
     */
    public Object getTarget() {
        return target;
    }

    /**
     * 设置数据源
     *
     * @param target
     */
    public void setTarget(Object target) {
        this.target = target;
    }

    /**
     * 构造函数
     */
    public ResponseResult() {
        this(ResultConstants.SUCCESS, "操作成功");
    }


    /**
     * 构造函数
     *
     * @param code    结果状态
     * @param message 结果消息
     */
    public ResponseResult(String code, String message) {
        this(code, message, null);
    }

    /**
     * 构造函数
     *
     * @param code    结果状态
     * @param message 结果熊希
     * @param data    结果
     */
    public ResponseResult(String code, String message, T data) {
        this(code, message, data, null);
    }

    /**
     * 构造函数
     *
     * @param code    结果状态
     * @param message 结果熊希
     * @param data    结果
     */
    public ResponseResult(String code, String message, T data, Object target) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.target = target;
    }

    /**
     * 构造成功结果
     *
     * @param data 数据
     * @param <T>  数据类型
     * @return 一个请求成功的数据集合
     */
    public static final <T extends Object> ResponseResult<T> result(T data) {
        return result(data, false);
    }

    /**
     * 构造成功结果,不允许为空
     *
     * @param data 数据
     * @param <T>  数据类型
     * @return 一个请求成功的数据集合
     */
    public static final <T extends Object> ResponseResult<T> resultAllowNull(T data) {
        return result(data, true);
    }


    /**
     * 构造成功结果
     *
     * @param data      数据
     * @param allowNull 允许为空
     * @param <T>       数据类型
     * @return 一个请求成功的数据集合
     */
    public static final <T extends Object> ResponseResult<T> result(T data, boolean allowNull) {
        ResponseResult<T> ret = new ResponseResult<T>();
        ret.setData(data);
        initDataStatus(ret, data, allowNull);
        return ret;
    }

    protected static void initDataStatus(ResponseResult ret, Object data, boolean allowNull) {
        if (!allowNull && StringHelper.isEmpty(data)) {
            ret.setCode(ResultConstants.RESULT_EMPTY);
            ret.setMessage("结果为空");
        } else {
            ret.setCode(ResultConstants.SUCCESS);
            ret.setMessage("处理成功");
        }
    }

    /**
     * 处理错误
     *
     * @param target 错误源对象
     * @return 处理的错误
     */
    public static final ResponseResult error(Object target) {
        return error(ResultConstants.UNKNOW_ERROR, "未知异常", target);
    }

    /**
     * 处理错误
     *
     * @param code    错误码
     * @param message 错误消息
     * @return 处理的错误
     */
    public static final ResponseResult error(String code, String message) {
        return error(code, message, null);
    }

    /**
     * 处理错误
     *
     * @param message 错误消息
     * @param target  错误标记
     * @return 处理的错误
     */
    public static final ResponseResult error(String message, Object target) {
        return error(ResultConstants.UNKNOW_ERROR, message, null);
    }

    /**
     * 处理错误
     *
     * @param code    错误码
     * @param message 错误消息
     * @param target  错误标记
     * @return 处理的错误
     */
    public static final ResponseResult error(String code, String message, Object target) {
        ResponseResult ret = new ResponseResult(code, message, null, target);
        return ret;
    }
}