page
This commit is contained in:
parent
027d0b6a5f
commit
7fbacb8b3e
@ -0,0 +1,62 @@
|
|||||||
|
package com.baogutang.frame.common.res;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @description:
|
||||||
|
*
|
||||||
|
* @author: nikooh
|
||||||
|
* @date: 2024/11/08 : 16:18
|
||||||
|
*/
|
||||||
|
@ApiModel("通用Page请求参数")
|
||||||
|
@Data
|
||||||
|
public abstract class AbstractPageParam implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 2897098106246970752L;
|
||||||
|
|
||||||
|
@ApiModelProperty("页数")
|
||||||
|
@Setter
|
||||||
|
private Integer pageNum;
|
||||||
|
|
||||||
|
@ApiModelProperty("页面大小")
|
||||||
|
@Setter
|
||||||
|
private Integer pageSize;
|
||||||
|
|
||||||
|
@ApiModelProperty("排序条件")
|
||||||
|
private List<OrderBy> orders = new ArrayList<>();
|
||||||
|
|
||||||
|
@ApiModel("排序")
|
||||||
|
@Data
|
||||||
|
public static class OrderBy implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -2936335557980068706L;
|
||||||
|
|
||||||
|
@ApiModelProperty("排序列名")
|
||||||
|
private String columnName;
|
||||||
|
|
||||||
|
@ApiModelProperty("是否降序")
|
||||||
|
private boolean desc;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPageNum() {
|
||||||
|
pageNum = pageNum == null ? 1 : pageNum;
|
||||||
|
return pageNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPageSize() {
|
||||||
|
if (pageSize == null || pageSize > 100) {
|
||||||
|
pageSize = 10;
|
||||||
|
}
|
||||||
|
return pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,43 +0,0 @@
|
|||||||
package com.baogutang.frame.common.response;
|
|
||||||
|
|
||||||
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param <T>
|
|
||||||
* @author N1KO
|
|
||||||
*/
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
@ToString
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Builder
|
|
||||||
public class PageDTO<T> {
|
|
||||||
|
|
||||||
@ApiModelProperty("列表数据")
|
|
||||||
private List<T> list;
|
|
||||||
|
|
||||||
@ApiModelProperty("总记录数")
|
|
||||||
private Integer totalCount;
|
|
||||||
|
|
||||||
@ApiModelProperty("每页记录数")
|
|
||||||
private Integer pageSize;
|
|
||||||
|
|
||||||
@ApiModelProperty("总页数")
|
|
||||||
private Integer totalPage;
|
|
||||||
|
|
||||||
@ApiModelProperty("当前页数")
|
|
||||||
private Integer currPage;
|
|
||||||
|
|
||||||
public PageDTO(List<T> list, PageInfo pageInfo) {
|
|
||||||
this.list = list;
|
|
||||||
this.totalCount = pageInfo.getTotalCount();
|
|
||||||
this.pageSize = pageInfo.getPageSize();
|
|
||||||
this.totalPage = pageInfo.getTotalPage();
|
|
||||||
this.currPage = pageInfo.getCurrPage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,101 +0,0 @@
|
|||||||
package com.baogutang.frame.common.response;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
||||||
import io.swagger.annotations.ApiModel;
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.ToString;
|
|
||||||
import lombok.experimental.Accessors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author N1KO
|
|
||||||
*/
|
|
||||||
@ApiModel("分页信息")
|
|
||||||
@Data
|
|
||||||
@ToString
|
|
||||||
@Accessors(chain = true)
|
|
||||||
public class PageInfo {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当前页码
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("当前页码")
|
|
||||||
private Integer currPage = 1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 一页几个
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("一页多少个")
|
|
||||||
private Integer pageSize = Integer.MAX_VALUE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 总页数
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("总页数")
|
|
||||||
private Integer totalPage = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 总记录数
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("总记录数")
|
|
||||||
private Integer totalCount;
|
|
||||||
|
|
||||||
public PageInfo() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public PageInfo(Number pageSize, Number currentPage, Number totalCount, Number totalPage) {
|
|
||||||
this(pageSize, currentPage);
|
|
||||||
initTotleRecord(totalCount, totalPage);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PageInfo(Number pageSize, Number currentPage) {
|
|
||||||
this.pageSize = pageSize == null ? null : pageSize.intValue();
|
|
||||||
this.currPage = currentPage == null ? 1 : currentPage.intValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
public PageInfo initTotleRecord(Number totalRecord, Number totalPage) {
|
|
||||||
this.totalCount = totalRecord == null ? null : totalRecord.intValue();
|
|
||||||
this.totalPage = totalPage == null ? null : totalPage.intValue();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
public Integer getFrom() {
|
|
||||||
return pageSize != null ? (currPage - 1) * pageSize : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
public Integer getTo() {
|
|
||||||
Integer from = getFrom();
|
|
||||||
return from != null ? from + pageSize : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取总页数
|
|
||||||
**/
|
|
||||||
@JsonIgnore
|
|
||||||
public Integer getAllPage() {
|
|
||||||
totalPage = totalCount != null && pageSize != null ? (this.totalCount - 1) / this.pageSize + 1 : null;
|
|
||||||
return totalPage;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
public Type getType() {
|
|
||||||
return pageSize == null ? Type.ALL : Type.PAGE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 分页类型
|
|
||||||
*/
|
|
||||||
public static enum Type {
|
|
||||||
/**
|
|
||||||
* 全部
|
|
||||||
*/
|
|
||||||
ALL,
|
|
||||||
/**
|
|
||||||
* 分页
|
|
||||||
*/
|
|
||||||
PAGE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baogutang.frame.core.utils;
|
package com.baogutang.frame.common.response;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import io.swagger.annotations.ApiModel;
|
import io.swagger.annotations.ApiModel;
|
||||||
@ -12,28 +12,34 @@ import java.util.List;
|
|||||||
* 分页工具类
|
* 分页工具类
|
||||||
*/
|
*/
|
||||||
@ApiModel(value = "分页结果数据", description = "分页结果数据")
|
@ApiModel(value = "分页结果数据", description = "分页结果数据")
|
||||||
public class PageUtils<T> implements Serializable {
|
public class Pages<T> implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
private static final long serialVersionUID = -4776072864343943792L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 总记录数
|
* 总记录数
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty("总记录数")
|
@ApiModelProperty("总记录数")
|
||||||
private int totalCount;
|
private int totalCount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 每页记录数
|
* 每页记录数
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty("每页记录数")
|
@ApiModelProperty("每页记录数")
|
||||||
private int pageSize;
|
private int pageSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 总页数
|
* 总页数
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty("总页数")
|
@ApiModelProperty("总页数")
|
||||||
private int totalPage;
|
private int totalPage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前页数
|
* 当前页数
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty("当前页数")
|
@ApiModelProperty("当前页数")
|
||||||
private int currPage;
|
private int currPage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列表数据
|
* 列表数据
|
||||||
*/
|
*/
|
||||||
@ -43,7 +49,7 @@ public class PageUtils<T> implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 分页
|
* 分页
|
||||||
*/
|
*/
|
||||||
public PageUtils(IPage<T> page) {
|
public Pages(IPage<T> page) {
|
||||||
this.list = page.getRecords();
|
this.list = page.getRecords();
|
||||||
this.totalCount = (int) page.getTotal();
|
this.totalCount = (int) page.getTotal();
|
||||||
this.pageSize = (int) page.getSize();
|
this.pageSize = (int) page.getSize();
|
||||||
@ -59,7 +65,7 @@ public class PageUtils<T> implements Serializable {
|
|||||||
* @param pageSize 每页记录数
|
* @param pageSize 每页记录数
|
||||||
* @param currPage 当前页数
|
* @param currPage 当前页数
|
||||||
*/
|
*/
|
||||||
public PageUtils(List<T> list, int totalCount, int pageSize, int currPage) {
|
public Pages(List<T> list, int totalCount, int pageSize, int currPage) {
|
||||||
this.list = list;
|
this.list = list;
|
||||||
this.totalCount = totalCount;
|
this.totalCount = totalCount;
|
||||||
this.pageSize = pageSize;
|
this.pageSize = pageSize;
|
||||||
@ -68,12 +74,12 @@ public class PageUtils<T> implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public PageUtils() {
|
public Pages() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> PageUtils<T> getPages(IPage<T> page) {
|
public static <T> Pages<T> getPages(IPage<T> page) {
|
||||||
PageUtils<T> iPage = new PageUtils<>();
|
Pages<T> iPage = new Pages<>();
|
||||||
iPage.setList(page.getRecords());
|
iPage.setList(page.getRecords());
|
||||||
iPage.setTotalCount((int) page.getTotal());
|
iPage.setTotalCount((int) page.getTotal());
|
||||||
iPage.setCurrPage((int) page.getCurrent());
|
iPage.setCurrPage((int) page.getCurrent());
|
||||||
@ -90,9 +96,9 @@ public class PageUtils<T> implements Serializable {
|
|||||||
* @param list 数据集
|
* @param list 数据集
|
||||||
* @return PageUtils<T>
|
* @return PageUtils<T>
|
||||||
*/
|
*/
|
||||||
public PageUtils<T> getPages(Integer pageNum, Integer pageSize, List<T> list) {
|
public Pages<T> getPages(Integer pageNum, Integer pageSize, List<T> list) {
|
||||||
int pages = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
|
int pages = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
|
||||||
PageUtils<T> iPage = new PageUtils<>();
|
Pages<T> iPage = new Pages<>();
|
||||||
iPage.setTotalCount(list.size());
|
iPage.setTotalCount(list.size());
|
||||||
iPage.setCurrPage(pageNum);
|
iPage.setCurrPage(pageNum);
|
||||||
iPage.setTotalPage(pages);
|
iPage.setTotalPage(pages);
|
||||||
@ -1,118 +0,0 @@
|
|||||||
package com.baogutang.frame.common.response;
|
|
||||||
|
|
||||||
import com.baogutang.frame.common.constant.enums.ResponseEnum;
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author N1KO
|
|
||||||
* @param <T>
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Builder
|
|
||||||
public class ResponsePage<T> implements Serializable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 成功
|
|
||||||
*/
|
|
||||||
public static final int SUCCESS_CODE = 200;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 失败
|
|
||||||
*/
|
|
||||||
public static final int FAIL_CODE = 300;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 参数不合法
|
|
||||||
*/
|
|
||||||
public static final int PARAM_ILLEGAL_CODE = 301;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 响应码
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("响应码")
|
|
||||||
private int code;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 响应信息
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("响应信息")
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 业务数据
|
|
||||||
*/
|
|
||||||
@ApiModelProperty("业务数据")
|
|
||||||
private PageDTO<T> data;
|
|
||||||
|
|
||||||
public ResponsePage() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> ok() {
|
|
||||||
return restResult(null, null, SUCCESS_CODE, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> ok(PageInfo pageInfo, List<T> data) {
|
|
||||||
return restResult(pageInfo, data, ResponseEnum.COMMON_SUCCESS.getCode(), ResponseEnum.COMMON_SUCCESS.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> ok(PageInfo pageInfo, List<T> data, int i, String message) {
|
|
||||||
return restResult(pageInfo, data, SUCCESS_CODE, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> ok(PageInfo pageInfo, List<T> data, String message) {
|
|
||||||
return restResult(pageInfo, data, SUCCESS_CODE, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> result(PageInfo pageInfo, List<T> data, int code, String message) {
|
|
||||||
return restResult(pageInfo, data, code, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> failed() {
|
|
||||||
return restResult(null, null, FAIL_CODE, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> failed(String message) {
|
|
||||||
return restResult(null, null, FAIL_CODE, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> failed(int code, String message) {
|
|
||||||
return restResult(null, null, code, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> failed(PageInfo pageInfo, List<T> data) {
|
|
||||||
return restResult(pageInfo, data, FAIL_CODE, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> failed(PageInfo pageInfo, List<T> data, String message) {
|
|
||||||
return restResult(pageInfo, data, FAIL_CODE, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> restResult(PageInfo pageInfo, List<T> data, int code, String message) {
|
|
||||||
ResponsePage<T> apiResult = new ResponsePage<>();
|
|
||||||
apiResult.setCode(code);
|
|
||||||
apiResult.setData(new PageDTO<T>(data, pageInfo));
|
|
||||||
apiResult.setMessage(message);
|
|
||||||
return apiResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean isSuccess() {
|
|
||||||
return (this.code == SUCCESS_CODE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ResponsePage<T> restfulResult(ResponseEnum respEnum, PageInfo pageInfo, List<T> data) {
|
|
||||||
ResponsePage<T> apiResult = new ResponsePage<>();
|
|
||||||
apiResult.setCode(respEnum.getCode());
|
|
||||||
apiResult.setMessage(respEnum.getMessage());
|
|
||||||
apiResult.setData(new PageDTO<T>(data, pageInfo));
|
|
||||||
return apiResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,98 @@
|
|||||||
|
package com.baogutang.frame.common.utils;
|
||||||
|
|
||||||
|
import com.baogutang.frame.common.res.AbstractPageParam;
|
||||||
|
import com.baogutang.frame.common.response.Pages;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description:
|
||||||
|
* @author: nikooh
|
||||||
|
* @date: 2024/03/07 : 20:17
|
||||||
|
*/
|
||||||
|
public class PageUtil {
|
||||||
|
|
||||||
|
private PageUtil() {
|
||||||
|
// private empty constructor
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* general method of paging query
|
||||||
|
*
|
||||||
|
* @param queryFunction page query function
|
||||||
|
* @param pageParam page param
|
||||||
|
* @param beanCastFunction bean cast function
|
||||||
|
* @param <T> entity class
|
||||||
|
* @param <U> query parameter
|
||||||
|
* @param <R> return bean
|
||||||
|
* @return Page
|
||||||
|
*/
|
||||||
|
public static <T, U extends AbstractPageParam, R> Pages<R> page(UnaryOperator<Page<T>> queryFunction, U pageParam, Function<T, R> beanCastFunction) {
|
||||||
|
Page<T> pageReq = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
|
||||||
|
Page<T> pageRes = queryFunction.apply(pageReq);
|
||||||
|
List<R> rPageRes = pageRes.getRecords().stream()
|
||||||
|
.map(beanCastFunction)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
return new Pages<>(rPageRes, (int) pageRes.getTotal(), (int) pageRes.getSize(), (int) pageRes.getCurrent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param orders
|
||||||
|
* @param queryWrapper
|
||||||
|
* @param <T> T
|
||||||
|
*/
|
||||||
|
public static <T> void defaultOrders(List<AbstractPageParam.OrderBy> orders, QueryWrapper<T> queryWrapper) {
|
||||||
|
defaultOrder(orders, queryWrapper, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ,
|
||||||
|
*
|
||||||
|
* @param orders
|
||||||
|
* @param queryWrapper
|
||||||
|
* @param defaultColumn
|
||||||
|
* @param <T> T
|
||||||
|
*/
|
||||||
|
public static <T, R> void defaultOrder(List<AbstractPageParam.OrderBy> orders, QueryWrapper<T> queryWrapper, SFunction<T, R> defaultColumn) {
|
||||||
|
if (CollectionUtils.isEmpty(orders)) {
|
||||||
|
defaultOrderProcess(queryWrapper, defaultColumn);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
orders.forEach(orderBy -> {
|
||||||
|
//
|
||||||
|
if (StringUtils.isBlank(orderBy.getColumnName())) {
|
||||||
|
defaultOrderProcess(queryWrapper, defaultColumn);
|
||||||
|
} else {
|
||||||
|
if (orderBy.isDesc()) {
|
||||||
|
queryWrapper.orderByDesc(orderBy.getColumnName());
|
||||||
|
} else {
|
||||||
|
queryWrapper.orderByAsc(orderBy.getColumnName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param queryWrapper
|
||||||
|
* @param defaultColumn
|
||||||
|
* @param <T> t
|
||||||
|
* @param <R> r
|
||||||
|
*/
|
||||||
|
private static <T, R> void defaultOrderProcess(QueryWrapper<T> queryWrapper, SFunction<T, R> defaultColumn) {
|
||||||
|
if (defaultColumn != null) {
|
||||||
|
queryWrapper.lambda()
|
||||||
|
.orderBy(true, false, defaultColumn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user