|
@@ -1,5 +1,6 @@
|
|
|
package com.yushu.framework.common.pojo;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
|
import com.yushu.framework.common.exception.ErrorCode;
|
|
|
import com.yushu.framework.common.exception.ServiceException;
|
|
@@ -8,6 +9,7 @@ import lombok.Data;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
+import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -17,6 +19,14 @@ import java.util.Objects;
|
|
|
*/
|
|
|
@Data
|
|
|
public class CommonResult<T> implements Serializable {
|
|
|
+ /**
|
|
|
+ * 成功
|
|
|
+ */
|
|
|
+ private static final String SUCCESS = "success";
|
|
|
+ /**
|
|
|
+ * 失败
|
|
|
+ */
|
|
|
+ private static final String FAIL = "fail";
|
|
|
|
|
|
/**
|
|
|
* 错误码
|
|
@@ -25,15 +35,28 @@ public class CommonResult<T> implements Serializable {
|
|
|
*/
|
|
|
private Integer code;
|
|
|
/**
|
|
|
+ * success、fail
|
|
|
+ */
|
|
|
+ private String result;
|
|
|
+ /**
|
|
|
* 返回数据
|
|
|
*/
|
|
|
private T data;
|
|
|
/**
|
|
|
* 错误提示,用户可阅读
|
|
|
*
|
|
|
- * @see ErrorCode#getMsg() ()
|
|
|
+ * @see ErrorCode#getMessage() ()
|
|
|
*/
|
|
|
- private String msg;
|
|
|
+ private String message;
|
|
|
+ /**
|
|
|
+ * 返回记录条数或者操作记录条数
|
|
|
+ */
|
|
|
+ private Long count;
|
|
|
+ /**
|
|
|
+ * 记录总条数
|
|
|
+ */
|
|
|
+ private Long total;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 将传入的 result 对象,转换成另外一个泛型结果的对象
|
|
@@ -45,26 +68,28 @@ public class CommonResult<T> implements Serializable {
|
|
|
* @return 新的 CommonResult 对象
|
|
|
*/
|
|
|
public static <T> CommonResult<T> error(CommonResult<?> result) {
|
|
|
- return error(result.getCode(), result.getMsg());
|
|
|
+ return error(result.getCode(), result.getMessage());
|
|
|
}
|
|
|
|
|
|
public static <T> CommonResult<T> error(Integer code, String message) {
|
|
|
Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
|
|
|
CommonResult<T> result = new CommonResult<>();
|
|
|
result.code = code;
|
|
|
- result.msg = message;
|
|
|
+ result.message = message;
|
|
|
+ result.result = FAIL;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public static <T> CommonResult<T> error(ErrorCode errorCode) {
|
|
|
- return error(errorCode.getCode(), errorCode.getMsg());
|
|
|
+ return error(errorCode.getCode(), errorCode.getMessage());
|
|
|
}
|
|
|
|
|
|
public static <T> CommonResult<T> success(T data) {
|
|
|
CommonResult<T> result = new CommonResult<>();
|
|
|
result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
|
|
|
result.data = data;
|
|
|
- result.msg = "";
|
|
|
+ result.result = SUCCESS;
|
|
|
+ result.message = "";
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -76,13 +101,30 @@ public class CommonResult<T> implements Serializable {
|
|
|
return error(serviceException.getCode(), serviceException.getMessage());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 多个数据
|
|
|
+ *
|
|
|
+ * @param records
|
|
|
+ * @param total
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> CommonResult<List<T>> multi(List<T> records, long total) {
|
|
|
+ // 容错处理,出现总数比实际数少时,以实际数为准(page查询全部时,total为0)
|
|
|
+ int size = CollUtil.size(records);
|
|
|
+ CommonResult<List<T>> result = success(records);
|
|
|
+ result.setCount(Long.valueOf(CollUtil.size(records)));
|
|
|
+ result.setTotal(total);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========= 和 Exception 异常体系集成 =========
|
|
|
+
|
|
|
@JsonIgnore // 避免 jackson 序列化
|
|
|
public boolean isSuccess() {
|
|
|
return isSuccess(code);
|
|
|
}
|
|
|
|
|
|
- // ========= 和 Exception 异常体系集成 =========
|
|
|
-
|
|
|
@JsonIgnore // 避免 jackson 序列化
|
|
|
public boolean isError() {
|
|
|
return !isSuccess();
|
|
@@ -96,7 +138,7 @@ public class CommonResult<T> implements Serializable {
|
|
|
return;
|
|
|
}
|
|
|
// 业务异常
|
|
|
- throw new ServiceException(code, msg);
|
|
|
+ throw new ServiceException(code, message);
|
|
|
}
|
|
|
|
|
|
}
|