|
@@ -1,5 +1,6 @@
|
|
|
package com.ruoyi.common.core.domain;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
@@ -61,52 +62,51 @@ public class PageQuery implements Serializable {
|
|
|
}
|
|
|
Page<T> page = new Page<>(pageNum, pageSize);
|
|
|
List<OrderItem> orderItems = buildOrderItem();
|
|
|
- if (ObjectUtil.isNotNull(orderItems)) {
|
|
|
+ if (CollUtil.isNotEmpty(orderItems)) {
|
|
|
page.addOrder(orderItems);
|
|
|
}
|
|
|
return page;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建排序
|
|
|
+ *
|
|
|
+ * 支持的用法如下:
|
|
|
+ * {isAsc:"asc",orderByColumn:"id"} order by id asc
|
|
|
+ * {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc
|
|
|
+ * {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc
|
|
|
+ * {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc
|
|
|
+ */
|
|
|
private List<OrderItem> buildOrderItem() {
|
|
|
- if (StringUtils.isNotBlank(isAsc) && StringUtils.isNotBlank(orderByColumn)) {
|
|
|
- // 兼容前端排序类型
|
|
|
- isAsc = isAsc.replace("ascending", "asc")
|
|
|
- .replace("descending", "desc");
|
|
|
-
|
|
|
- String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
|
|
|
- orderBy = StringUtils.toUnderScoreCase(orderBy);
|
|
|
- //分割orderByColumn和isAsc参数
|
|
|
- String[] orderByArr = orderBy.split(",");
|
|
|
- String[] isAscArr = isAsc.split(",");
|
|
|
- if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
|
|
|
+ if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
|
|
|
+ orderBy = StringUtils.toUnderScoreCase(orderBy);
|
|
|
+
|
|
|
+ // 兼容前端排序类型
|
|
|
+ isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});
|
|
|
+
|
|
|
+ String[] orderByArr = orderBy.split(",");
|
|
|
+ String[] isAscArr = isAsc.split(",");
|
|
|
+ if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
|
|
|
+ throw new ServiceException("排序参数有误");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<OrderItem> list = new ArrayList<>();
|
|
|
+ // 每个字段各自排序
|
|
|
+ for (int i = 0; i < orderByArr.length; i++) {
|
|
|
+ String orderByStr = orderByArr[i];
|
|
|
+ String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i];
|
|
|
+ if ("asc".equals(isAscStr)) {
|
|
|
+ list.add(OrderItem.asc(orderByStr));
|
|
|
+ } else if ("desc".equals(isAscStr)) {
|
|
|
+ list.add(OrderItem.desc(orderByStr));
|
|
|
+ } else {
|
|
|
throw new ServiceException("排序参数有误");
|
|
|
}
|
|
|
- if (isAscArr.length == 1) {
|
|
|
- //isAsc只提供了一个 则全部字段都是如此排序
|
|
|
- if ("asc".equals(isAsc)) {
|
|
|
- return OrderItem.ascs(orderBy);
|
|
|
- } else if ("desc".equals(isAsc)) {
|
|
|
- return OrderItem.descs(orderBy);
|
|
|
- } else {
|
|
|
- throw new ServiceException("isAsc参数有误");
|
|
|
- }
|
|
|
- }
|
|
|
- ArrayList<OrderItem> list = new ArrayList<>();
|
|
|
- //每个字段各自排序
|
|
|
- for (int i = 0; i < isAscArr.length; i++) {
|
|
|
- String isAscStr = isAscArr[i];
|
|
|
- String orderByStr = orderByArr[i];
|
|
|
- if ("asc".equals(isAscStr)) {
|
|
|
- list.add(OrderItem.asc(orderByStr));
|
|
|
- } else if ("desc".equals(isAscStr)) {
|
|
|
- list.add(OrderItem.desc(orderByStr));
|
|
|
- } else {
|
|
|
- throw new ServiceException("isAsc参数有误");
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
}
|
|
|
- return null;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
}
|