package com.nms.swspkmas_standalone.response; import io.swagger.annotations.ApiModelProperty; /** * 通用返回结果封装类 */ public class CommonResult { /** * 状态码 */ @ApiModelProperty("状态码") private long code; /** * 提示信息 */ @ApiModelProperty("错误信息") private String message; /** * 是否成功 */ @ApiModelProperty("接口成功失败标识") private boolean success; /** * 数据封装 */ @ApiModelProperty("接口返回数据") private T data; protected CommonResult() { } protected CommonResult(long code, String message, T data,boolean success) { this.code = code; this.message = message; this.data = data; this.success=success; } /** * 成功返回结果 * */ public static CommonResult success() { return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null,true); } /** * 成功返回结果 * * @param data 获取的数据 */ public static CommonResult success(T data) { return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data,true); } /** * 成功返回结果 * * @param data 获取的数据 * @param message 提示信息 */ public static CommonResult success(T data, String message) { return new CommonResult(ResultCode.SUCCESS.getCode(), message, data,true); } /** * 失败返回结果 * @param errorCode 错误码 */ public static CommonResult failed(IErrorCode errorCode) { return new CommonResult(errorCode.getCode(), errorCode.getMessage(), null,false); } /** * 失败返回结果 * @param errorCode 错误码 * @param message 错误信息 */ public static CommonResult failed(IErrorCode errorCode,String message) { return new CommonResult(errorCode.getCode(), message, null,false); } /** * 失败返回结果 * @param message 提示信息 */ public static CommonResult failed(String message) { return new CommonResult(ResultCode.FAILED.getCode(), message, null,false); } /** * 失败返回结果 */ public static CommonResult failed() { return failed(ResultCode.FAILED); } /** * 参数验证失败返回结果 */ public static CommonResult validateFailed() { return failed(ResultCode.VALIDATE_FAILED); } /** * 参数验证失败返回结果 * @param message 提示信息 */ public static CommonResult validateFailed(String message) { return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), message, null,false); } /** * 未登录返回结果 */ public static CommonResult unauthorized(T data) { return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data,false); } /** * 未授权返回结果 */ public static CommonResult forbidden(T data) { return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data,false); } public long getCode() { return code; } public void setCode(long code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } }