update 优化 pr235 解决小问题与优化写法
This commit is contained in:
parent
0e75aa6250
commit
663024ac7f
@ -1,5 +1,6 @@
|
|||||||
package com.ruoyi.common.core.domain;
|
package com.ruoyi.common.core.domain;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
@ -61,52 +62,51 @@ public class PageQuery implements Serializable {
|
|||||||
}
|
}
|
||||||
Page<T> page = new Page<>(pageNum, pageSize);
|
Page<T> page = new Page<>(pageNum, pageSize);
|
||||||
List<OrderItem> orderItems = buildOrderItem();
|
List<OrderItem> orderItems = buildOrderItem();
|
||||||
if (ObjectUtil.isNotNull(orderItems)) {
|
if (CollUtil.isNotEmpty(orderItems)) {
|
||||||
page.addOrder(orderItems);
|
page.addOrder(orderItems);
|
||||||
}
|
}
|
||||||
return page;
|
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() {
|
private List<OrderItem> buildOrderItem() {
|
||||||
if (StringUtils.isNotBlank(isAsc) && StringUtils.isNotBlank(orderByColumn)) {
|
if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) {
|
||||||
// 兼容前端排序类型
|
return null;
|
||||||
isAsc = isAsc.replace("ascending", "asc")
|
}
|
||||||
.replace("descending", "desc");
|
String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
|
||||||
|
orderBy = StringUtils.toUnderScoreCase(orderBy);
|
||||||
|
|
||||||
String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
|
// 兼容前端排序类型
|
||||||
orderBy = StringUtils.toUnderScoreCase(orderBy);
|
isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});
|
||||||
//分割orderByColumn和isAsc参数
|
|
||||||
String[] orderByArr = orderBy.split(",");
|
String[] orderByArr = orderBy.split(",");
|
||||||
String[] isAscArr = isAsc.split(",");
|
String[] isAscArr = isAsc.split(",");
|
||||||
if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
|
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("排序参数有误");
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user