You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.0 KiB
Java
82 lines
2.0 KiB
Java
package com.glxp.api.util.page;
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.glxp.api.util.StringUtils;
|
|
import lombok.Data;
|
|
|
|
/**
|
|
* 分页查询实体类
|
|
*
|
|
* @author Lion Li
|
|
*/
|
|
|
|
@Data
|
|
public class PageQuery {
|
|
|
|
/**
|
|
* 分页大小
|
|
*/
|
|
private Integer limit;
|
|
|
|
/**
|
|
* 当前页数
|
|
*/
|
|
private Integer page;
|
|
|
|
/**
|
|
* 排序列
|
|
*/
|
|
private String orderByColumn;
|
|
|
|
/**
|
|
* 排序的方向desc或者asc
|
|
*/
|
|
private String isAsc;
|
|
|
|
/**
|
|
* 当前记录起始索引 默认值
|
|
*/
|
|
public static final int DEFAULT_PAGE_NUM = 1;
|
|
|
|
/**
|
|
* 每页显示记录数 默认值 默认查全部
|
|
*/
|
|
public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
|
|
|
|
public <T> Page<T> build() {
|
|
Integer pageNum = ObjectUtil.defaultIfNull(getPage(), DEFAULT_PAGE_NUM);
|
|
Integer pageSize = ObjectUtil.defaultIfNull(getLimit(), DEFAULT_PAGE_SIZE);
|
|
if (pageNum <= 0) {
|
|
pageNum = DEFAULT_PAGE_NUM;
|
|
}
|
|
Page<T> page = new Page<>(pageNum, pageSize);
|
|
OrderItem orderItem = buildOrderItem();
|
|
if (ObjectUtil.isNotNull(orderItem)) {
|
|
page.addOrder(orderItem);
|
|
}
|
|
return page;
|
|
}
|
|
|
|
private OrderItem buildOrderItem() {
|
|
// 兼容前端排序类型
|
|
if ("ascending".equals(isAsc)) {
|
|
isAsc = "asc";
|
|
} else if ("descending".equals(isAsc)) {
|
|
isAsc = "desc";
|
|
}
|
|
if (StringUtils.isNotBlank(orderByColumn)) {
|
|
String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
|
|
orderBy = StringUtils.toUnderScoreCase(orderBy);
|
|
if ("asc".equals(isAsc)) {
|
|
return OrderItem.asc(orderBy);
|
|
} else if ("desc".equals(isAsc)) {
|
|
return OrderItem.desc(orderBy);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|