From 7fbacb8b3efe19288dcb742cad2355a45055aa57 Mon Sep 17 00:00:00 2001 From: N1KO Date: Fri, 8 Nov 2024 16:24:52 +0800 Subject: [PATCH] page --- .../frame/common/res/AbstractPageParam.java | 62 +++++++++ .../frame/common/response/PageDTO.java | 43 ------- .../frame/common/response/PageInfo.java | 101 --------------- .../frame/common/response/Pages.java | 26 ++-- .../frame/common/response/ResponsePage.java | 118 ------------------ .../frame/common/utils/PageUtil.java | 98 +++++++++++++++ 6 files changed, 176 insertions(+), 272 deletions(-) create mode 100644 frame-common/src/main/java/com/baogutang/frame/common/res/AbstractPageParam.java delete mode 100644 frame-common/src/main/java/com/baogutang/frame/common/response/PageDTO.java delete mode 100644 frame-common/src/main/java/com/baogutang/frame/common/response/PageInfo.java rename frame-core/src/main/java/com/baogutang/frame/core/utils/PageUtils.java => frame-common/src/main/java/com/baogutang/frame/common/response/Pages.java (86%) delete mode 100644 frame-common/src/main/java/com/baogutang/frame/common/response/ResponsePage.java create mode 100644 frame-common/src/main/java/com/baogutang/frame/common/utils/PageUtil.java diff --git a/frame-common/src/main/java/com/baogutang/frame/common/res/AbstractPageParam.java b/frame-common/src/main/java/com/baogutang/frame/common/res/AbstractPageParam.java new file mode 100644 index 0000000..a0deedd --- /dev/null +++ b/frame-common/src/main/java/com/baogutang/frame/common/res/AbstractPageParam.java @@ -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 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; + } + +} diff --git a/frame-common/src/main/java/com/baogutang/frame/common/response/PageDTO.java b/frame-common/src/main/java/com/baogutang/frame/common/response/PageDTO.java deleted file mode 100644 index 9e7575f..0000000 --- a/frame-common/src/main/java/com/baogutang/frame/common/response/PageDTO.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baogutang.frame.common.response; - - -import io.swagger.annotations.ApiModelProperty; -import lombok.*; - -import java.util.List; - -/** - * @param - * @author N1KO - */ -@Setter -@Getter -@ToString -@NoArgsConstructor -@AllArgsConstructor -@Builder -public class PageDTO { - - @ApiModelProperty("列表数据") - private List list; - - @ApiModelProperty("总记录数") - private Integer totalCount; - - @ApiModelProperty("每页记录数") - private Integer pageSize; - - @ApiModelProperty("总页数") - private Integer totalPage; - - @ApiModelProperty("当前页数") - private Integer currPage; - - public PageDTO(List list, PageInfo pageInfo) { - this.list = list; - this.totalCount = pageInfo.getTotalCount(); - this.pageSize = pageInfo.getPageSize(); - this.totalPage = pageInfo.getTotalPage(); - this.currPage = pageInfo.getCurrPage(); - } -} diff --git a/frame-common/src/main/java/com/baogutang/frame/common/response/PageInfo.java b/frame-common/src/main/java/com/baogutang/frame/common/response/PageInfo.java deleted file mode 100644 index ad5c011..0000000 --- a/frame-common/src/main/java/com/baogutang/frame/common/response/PageInfo.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/frame-core/src/main/java/com/baogutang/frame/core/utils/PageUtils.java b/frame-common/src/main/java/com/baogutang/frame/common/response/Pages.java similarity index 86% rename from frame-core/src/main/java/com/baogutang/frame/core/utils/PageUtils.java rename to frame-common/src/main/java/com/baogutang/frame/common/response/Pages.java index c59d7e4..f3379aa 100644 --- a/frame-core/src/main/java/com/baogutang/frame/core/utils/PageUtils.java +++ b/frame-common/src/main/java/com/baogutang/frame/common/response/Pages.java @@ -1,4 +1,4 @@ -package com.baogutang.frame.core.utils; +package com.baogutang.frame.common.response; import com.baomidou.mybatisplus.core.metadata.IPage; import io.swagger.annotations.ApiModel; @@ -12,28 +12,34 @@ import java.util.List; * 分页工具类 */ @ApiModel(value = "分页结果数据", description = "分页结果数据") -public class PageUtils implements Serializable { - private static final long serialVersionUID = 1L; +public class Pages implements Serializable { + + private static final long serialVersionUID = -4776072864343943792L; + /** * 总记录数 */ @ApiModelProperty("总记录数") private int totalCount; + /** * 每页记录数 */ @ApiModelProperty("每页记录数") private int pageSize; + /** * 总页数 */ @ApiModelProperty("总页数") private int totalPage; + /** * 当前页数 */ @ApiModelProperty("当前页数") private int currPage; + /** * 列表数据 */ @@ -43,7 +49,7 @@ public class PageUtils implements Serializable { /** * 分页 */ - public PageUtils(IPage page) { + public Pages(IPage page) { this.list = page.getRecords(); this.totalCount = (int) page.getTotal(); this.pageSize = (int) page.getSize(); @@ -59,7 +65,7 @@ public class PageUtils implements Serializable { * @param pageSize 每页记录数 * @param currPage 当前页数 */ - public PageUtils(List list, int totalCount, int pageSize, int currPage) { + public Pages(List list, int totalCount, int pageSize, int currPage) { this.list = list; this.totalCount = totalCount; this.pageSize = pageSize; @@ -68,12 +74,12 @@ public class PageUtils implements Serializable { } - public PageUtils() { + public Pages() { } - public static PageUtils getPages(IPage page) { - PageUtils iPage = new PageUtils<>(); + public static Pages getPages(IPage page) { + Pages iPage = new Pages<>(); iPage.setList(page.getRecords()); iPage.setTotalCount((int) page.getTotal()); iPage.setCurrPage((int) page.getCurrent()); @@ -90,9 +96,9 @@ public class PageUtils implements Serializable { * @param list 数据集 * @return PageUtils */ - public PageUtils getPages(Integer pageNum, Integer pageSize, List list) { + public Pages getPages(Integer pageNum, Integer pageSize, List list) { int pages = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1; - PageUtils iPage = new PageUtils<>(); + Pages iPage = new Pages<>(); iPage.setTotalCount(list.size()); iPage.setCurrPage(pageNum); iPage.setTotalPage(pages); diff --git a/frame-common/src/main/java/com/baogutang/frame/common/response/ResponsePage.java b/frame-common/src/main/java/com/baogutang/frame/common/response/ResponsePage.java deleted file mode 100644 index 10d44bd..0000000 --- a/frame-common/src/main/java/com/baogutang/frame/common/response/ResponsePage.java +++ /dev/null @@ -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 - */ -@Data -@AllArgsConstructor -@Builder -public class ResponsePage 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 data; - - public ResponsePage() { - } - - public static ResponsePage ok() { - return restResult(null, null, SUCCESS_CODE, null); - } - - public static ResponsePage ok(PageInfo pageInfo, List data) { - return restResult(pageInfo, data, ResponseEnum.COMMON_SUCCESS.getCode(), ResponseEnum.COMMON_SUCCESS.getMessage()); - } - - public static ResponsePage ok(PageInfo pageInfo, List data, int i, String message) { - return restResult(pageInfo, data, SUCCESS_CODE, null); - } - - public static ResponsePage ok(PageInfo pageInfo, List data, String message) { - return restResult(pageInfo, data, SUCCESS_CODE, message); - } - - public static ResponsePage result(PageInfo pageInfo, List data, int code, String message) { - return restResult(pageInfo, data, code, message); - } - - public static ResponsePage failed() { - return restResult(null, null, FAIL_CODE, null); - } - - public static ResponsePage failed(String message) { - return restResult(null, null, FAIL_CODE, message); - } - - public static ResponsePage failed(int code, String message) { - return restResult(null, null, code, message); - } - - public static ResponsePage failed(PageInfo pageInfo, List data) { - return restResult(pageInfo, data, FAIL_CODE, null); - } - - public static ResponsePage failed(PageInfo pageInfo, List data, String message) { - return restResult(pageInfo, data, FAIL_CODE, message); - } - - public static ResponsePage restResult(PageInfo pageInfo, List data, int code, String message) { - ResponsePage apiResult = new ResponsePage<>(); - apiResult.setCode(code); - apiResult.setData(new PageDTO(data, pageInfo)); - apiResult.setMessage(message); - return apiResult; - } - - public Boolean isSuccess() { - return (this.code == SUCCESS_CODE); - } - - public static ResponsePage restfulResult(ResponseEnum respEnum, PageInfo pageInfo, List data) { - ResponsePage apiResult = new ResponsePage<>(); - apiResult.setCode(respEnum.getCode()); - apiResult.setMessage(respEnum.getMessage()); - apiResult.setData(new PageDTO(data, pageInfo)); - return apiResult; - } - - -} diff --git a/frame-common/src/main/java/com/baogutang/frame/common/utils/PageUtil.java b/frame-common/src/main/java/com/baogutang/frame/common/utils/PageUtil.java new file mode 100644 index 0000000..cb78d8e --- /dev/null +++ b/frame-common/src/main/java/com/baogutang/frame/common/utils/PageUtil.java @@ -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 entity class + * @param query parameter + * @param return bean + * @return Page + */ + public static Pages page(UnaryOperator> queryFunction, U pageParam, Function beanCastFunction) { + Page pageReq = new Page<>(pageParam.getPageNum(), pageParam.getPageSize()); + Page pageRes = queryFunction.apply(pageReq); + List 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 + */ + public static void defaultOrders(List orders, QueryWrapper queryWrapper) { + defaultOrder(orders, queryWrapper, null); + } + + /** + * , + * + * @param orders + * @param queryWrapper + * @param defaultColumn + * @param T + */ + public static void defaultOrder(List orders, QueryWrapper queryWrapper, SFunction 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 + * @param r + */ + private static void defaultOrderProcess(QueryWrapper queryWrapper, SFunction defaultColumn) { + if (defaultColumn != null) { + queryWrapper.lambda() + .orderBy(true, false, defaultColumn); + } + } +}