|
@@ -1,13 +1,23 @@
|
|
package com.warewms.common.core.domain.base.page;
|
|
package com.warewms.common.core.domain.base.page;
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.text.CharSequenceUtil;
|
|
import cn.hutool.core.text.CharSequenceUtil;
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.warewms.common.exception.ServiceException;
|
|
|
|
+import com.warewms.common.utils.StringUtils;
|
|
|
|
+import com.warewms.common.utils.sql.SqlUtil;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
import static com.warewms.common.core.domain.base.page.PageSort.ASC;
|
|
import static com.warewms.common.core.domain.base.page.PageSort.ASC;
|
|
import static com.warewms.common.core.domain.base.page.PageSort.DESC;
|
|
import static com.warewms.common.core.domain.base.page.PageSort.DESC;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 分页数据
|
|
* 分页数据
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
*/
|
|
*/
|
|
public class PageDomain
|
|
public class PageDomain
|
|
@@ -24,6 +34,19 @@ public class PageDomain
|
|
/** 排序的方向desc或者asc */
|
|
/** 排序的方向desc或者asc */
|
|
private String isAsc = "asc";
|
|
private String isAsc = "asc";
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 当前记录起始索引 默认值
|
|
|
|
+ */
|
|
|
|
+ public static final int DEFAULT_PAGE_NUM = 1;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 每页显示记录数 默认值 默认查全部
|
|
|
|
+ */
|
|
|
|
+ public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
|
|
|
|
+
|
|
|
|
+ private static final String SEPARATOR = ",";
|
|
|
|
+
|
|
|
|
+
|
|
public Integer getPageNum()
|
|
public Integer getPageNum()
|
|
{
|
|
{
|
|
return pageNum;
|
|
return pageNum;
|
|
@@ -63,5 +86,58 @@ public class PageDomain
|
|
this.isAsc = CharSequenceUtil.equals(ASC.getDescription(), isAsc) ? ASC.getDescription() : DESC.getDescription(); ;
|
|
this.isAsc = CharSequenceUtil.equals(ASC.getDescription(), isAsc) ? ASC.getDescription() : DESC.getDescription(); ;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public <T> Page<T> build() {
|
|
|
|
+ Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
|
|
|
|
+ Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
|
|
|
|
+ if (pageNum <= 0) {
|
|
|
|
+ pageNum = DEFAULT_PAGE_NUM;
|
|
|
|
+ }
|
|
|
|
+ Page<T> page = new Page<>(pageNum, pageSize);
|
|
|
|
+ List<OrderItem> orderItems = buildOrderItem();
|
|
|
|
+ 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.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(SEPARATOR);
|
|
|
|
+ String[] isAscArr = isAsc.split(SEPARATOR);
|
|
|
|
+ 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("排序参数有误");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
}
|
|
}
|