/* * Copyright (c) 2018. * http://www.ulabcare.com */ package com.cnbm.packing.vo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; import lombok.experimental.Accessors; import java.io.Serializable; /** * 处理结果类 * * @param 返回的数据类型 * @author jiff * @date 2018/11/7 * @since 1.0 */ @Data @AllArgsConstructor @NoArgsConstructor @ToString @ApiModel("处理结果类") @Accessors public class R implements Serializable { @NonNull @ApiModelProperty(value = "结果码", example = "0") private int code = IErrorCode.CODE_SUCCESSFUL; @ApiModelProperty(value = "结果说明", example = "成功") private String msg; @ApiModelProperty(value = "业务数据") private T data; public R code(int code) { this.code = code; return this; } public R msg(String msg) { this.msg = msg; return this; } public R data(T data) { this.data = data; return this; } public boolean ok() { return code == IErrorCode.CODE_SUCCESSFUL; } public static R failed() { return failed("系统错误!"); } public static R failed(String msg) { return failed(ApiErrorCode.FAILED.getCode(), msg); } public static R failed(int code, String msg) { return failed(code, msg, null); } public static R failed(int code, String msg, T data) { return r(code == ApiErrorCode.SUCCESSFUL.getCode() ? ApiErrorCode.FAILED.getCode() : code, msg, data); } public static R failed(IErrorCode errorCode) { return r(errorCode, null); } public static R r(IErrorCode errorCode, T data) { return r(errorCode.getCode(), errorCode.getMsg(), data); } public static R r(int code, String msg, T data) { return new R().code(code).msg(msg).data(data); } public static R unauthorized() { return failed(ApiErrorCode.UNAUTHORIZED); } public static R forbidden() { return failed(ApiErrorCode.FORBIDDEN); } public static R ok(T data) { return new R().data(data); } public static R ok(String msg, T data) { return new R().msg(msg).data(data); } }