From 6d4dbbb1add8a4181d857cf96abc1065b9478345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Tue, 1 Feb 2022 13:00:59 +0800 Subject: [PATCH] feat($Pagination): create functional pagination response composer --- .../maf/common/bean/PaginationBase.java | 12 ++-- .../FunctionalPaginationResponseComposer.java | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/FunctionalPaginationResponseComposer.java diff --git a/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java b/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java index 94181eef..30ff793d 100644 --- a/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java +++ b/common/src/main/java/com/jmsoftware/maf/common/bean/PaginationBase.java @@ -1,7 +1,7 @@ package com.jmsoftware.maf.common.bean; +import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.NumberUtil; -import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import org.hibernate.validator.constraints.Range; @@ -54,10 +54,10 @@ public class PaginationBase { @JsonIgnore public String getOrderByStatement() { - if (!StrUtil.isBlank(orderBy)) { - return String.format("%s `%s` %s", "ORDER BY", orderBy, orderRule); + if (!CharSequenceUtil.isBlank(this.orderBy)) { + return String.format("%s `%s` %s", "ORDER BY", this.orderBy, this.orderRule); } - return orderByStatement; + return this.orderByStatement; } /** @@ -68,8 +68,8 @@ public String getOrderByStatement() { * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 4:37 PM */ public boolean hasNextPage(@NotNull PageResponseBodyBean pageResponseBodyBean) { - if (NumberUtil.compare(pageResponseBodyBean.getTotal(), (long) currentPage * pageSize) > 0) { - currentPage += 1; + if (NumberUtil.compare(pageResponseBodyBean.getTotal(), (long) this.currentPage * this.pageSize) > 0) { + this.currentPage += 1; return true; } return false; diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/FunctionalPaginationResponseComposer.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/FunctionalPaginationResponseComposer.java new file mode 100644 index 00000000..55d626b2 --- /dev/null +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/FunctionalPaginationResponseComposer.java @@ -0,0 +1,62 @@ +package com.jmsoftware.maf.springcloudstarter.function; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.PageUtil; +import com.jmsoftware.maf.common.bean.PageResponseBodyBean; +import com.jmsoftware.maf.common.bean.PaginationBase; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; +import lombok.val; + +import java.util.List; +import java.util.function.Function; + +import static java.lang.Math.toIntExact; + +/** + *

FunctionalPaginationResponseComposer

+ *

+ * Change description here. + * + * @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 2/1/22 1:13 AM + **/ +@Slf4j +@SuppressWarnings("unused") +public class FunctionalPaginationResponseComposer { + private FunctionalPaginationResponseComposer() { + } + + public static List composePages( + @NonNull T paginationBase, + @NonNull Function> callback + ) { + val result = CollUtil.newArrayList(); + while (true) { + val page = callback.apply(paginationBase); + if (CollUtil.isEmpty(page.getList())) { + CollUtil.addAll(result, page.getList()); + } + if (!paginationBase.hasNextPage(page)) { + val totalPage = PageUtil.totalPage(toIntExact(page.getTotal()), paginationBase.getPageSize()); + log.warn("Reached the end of the page. totalPage: {}, currentPage: {}, pageSize: {}", totalPage, + paginationBase.getCurrentPage(), paginationBase.getPageSize()); + break; + } + } + return result; + } + + private static boolean hasNextPage( + @NonNull T paginationBase, + @NonNull PageResponseBodyBean pageResponse + ) { + val totalPage = PageUtil.totalPage(toIntExact(pageResponse.getTotal()), paginationBase.getPageSize()); + if (paginationBase.getCurrentPage() < totalPage) { + paginationBase.setCurrentPage(paginationBase.getCurrentPage() + 1); + return true; + } + log.warn("Reached the end of the page. totalPage: {}, currentPage: {}, pageSize: {}", totalPage, + paginationBase.getCurrentPage(), paginationBase.getPageSize()); + return false; + } +}