titan.lightbatis.common.CommonResult Maven / Gradle / Ivy
package titan.lightbatis.common;
import titan.lightbatis.result.PageList;
/**
* 通用返回对象
* Created by macro on 2019/4/19.
*/
public class CommonResult {
private long code;
private String message;
private T data;
protected CommonResult() {
}
protected CommonResult(long code, String message, T data) {
this.code = code;
this.message = message;
// if (data instanceof PageList){
// this.data = (T) new PageResult((PageList)data);
// } else {
//
// }
this.data = data;
}
/**
* 成功返回结果
*
* @param data 获取的数据
*/
public static CommonResult success(T data) {
if (data instanceof PageList) {
CommonPageResult result = new CommonPageResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
PageList list = (PageList)data;
result.setTotalSize(list.getTotalSize());
return result;
}
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
}
/**
* 成功返回结果
*
* @param data 获取的数据
* @param message 提示信息
*/
public static CommonResult success(T data, String message) {
if (data instanceof PageList) {
CommonPageResult result = new CommonPageResult(ResultCode.SUCCESS.getCode(), message, data);
PageList list = (PageList)data;
result.setTotalSize(list.getTotalSize());
return result;
}
return new CommonResult(ResultCode.SUCCESS.getCode(), message, data);
}
/**
* 失败返回结果
* @param errorCode 错误码
*/
public static CommonResult failed(IErrorCode errorCode) {
return new CommonResult(errorCode.getCode(), errorCode.getMessage(), null);
}
/**
* 失败返回结果
* @param errorCode 错误码
* @param message 错误信息
*/
public static CommonResult failed(IErrorCode errorCode, String message) {
return new CommonResult(errorCode.getCode(), message, null);
}
/**
* 失败返回结果
* @param message 提示信息
*/
public static CommonResult failed(String message) {
return new CommonResult(ResultCode.FAILED.getCode(), message, null);
}
/**
* 失败返回结果
*/
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);
}
/**
* 未登录返回结果
*/
public static CommonResult unauthorized(T data) {
return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
}
/**
* 未授权返回结果
*/
public static CommonResult forbidden(T data) {
return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
}
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;
}
}