package com.boying.common;
|
|
import java.io.Serializable;
|
|
/**
|
* @author kdq
|
* @version 1.0.0
|
* @ClassName R.java
|
* @Description TODO
|
* @createTime 2022年11月19日 09:13:00
|
*/
|
public class R<T> implements Serializable {
|
private static final long serialVersionUID = 1L;
|
/**
|
* 状态码 0:业务处理失败 1:业务处理成功 -1:出现异常
|
*/
|
private int code;
|
|
private String msg;
|
|
private T data;
|
|
public static <T> R<T> ok() {
|
return restResult(null, 0, (String)null);
|
}
|
|
public static <T> R<T> ok(T data) {
|
return restResult(data, 0, (String)null);
|
}
|
|
public static <T> R<T> ok(T data, String msg) {
|
return restResult(data, 0, msg);
|
}
|
|
public static <T> R<T> failed() {
|
return restResult(null, 1, (String)null);
|
}
|
|
public static <T> R<T> failed(String msg) {
|
return restResult(null, 1, msg);
|
}
|
|
public static <T> R<T> failed(T data) {
|
return restResult(data, 1, (String)null);
|
}
|
|
public static <T> R<T> failed(T data, String msg) {
|
return restResult(data, 1, msg);
|
}
|
|
static <T> R<T> restResult(T data, int code, String msg) {
|
R<T> apiResult = new R();
|
apiResult.setCode(code);
|
apiResult.setData(data);
|
apiResult.setMsg(msg);
|
return apiResult;
|
}
|
|
public static <T> R.RBuilder<T> builder() {
|
return new R.RBuilder();
|
}
|
|
@Override
|
public String toString() {
|
return "R(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
|
}
|
|
public R() {
|
}
|
|
public R(int code, String msg, T data) {
|
this.code = code;
|
this.msg = msg;
|
this.data = data;
|
}
|
|
public int getCode() {
|
return this.code;
|
}
|
|
public R<T> setCode(int code) {
|
this.code = code;
|
return this;
|
}
|
|
public String getMsg() {
|
return this.msg;
|
}
|
|
public R<T> setMsg(String msg) {
|
this.msg = msg;
|
return this;
|
}
|
|
public T getData() {
|
return this.data;
|
}
|
|
public R<T> setData(T data) {
|
this.data = data;
|
return this;
|
}
|
|
public static class RBuilder<T> {
|
private int code;
|
private String msg;
|
private T data;
|
|
RBuilder() {
|
}
|
|
public R.RBuilder<T> code(int code) {
|
this.code = code;
|
return this;
|
}
|
|
public R.RBuilder<T> msg(String msg) {
|
this.msg = msg;
|
return this;
|
}
|
|
public R.RBuilder<T> data(T data) {
|
this.data = data;
|
return this;
|
}
|
|
public R<T> build() {
|
return new R(this.code, this.msg, this.data);
|
}
|
|
@Override
|
public String toString() {
|
return "R.RBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ")";
|
}
|
}
|
}
|