All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.icodegarden.commons.lang.util.PageHelperUtils Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package io.github.icodegarden.commons.lang.util;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.github.icodegarden.commons.lang.query.BaseQuery;
import io.github.icodegarden.commons.lang.query.NextQuerySupportArrayList;
import io.github.icodegarden.commons.lang.query.NextQuerySupportList;
import io.github.icodegarden.commons.lang.query.NextQuerySupportPage;

/**
 * 
 * @author Fangfang.Xu
 *
 */
public abstract class PageHelperUtils {

	/**
	 * 是否执行了分页,即是否执行了PageHelper.startPage 且 正在分页中
* 如果分页调用已经结束,则是false,因为LocalPage已被自动remove * * @return */ public static boolean isPage() { Page page = PageHelper.getLocalPage(); return page != null; } /** * 是否执行count,即是否执行了PageHelper.startPage 且 正在分页中 + count参数=true
* 否则false * * @return */ public static boolean isCount() { Page page = PageHelper.getLocalPage(); return page != null ? page.isCount() : false; } /** * 只转换类型 */ public static Page ofPage(Page page, Function elementConvertor) { Page newPage = new Page(page.getPageNum(), page.getPageSize()); newPage.setTotal(page.getTotal()); newPage.setPages(page.getPages()); newPage.setCount(page.isCount()); newPage.setOrderBy(page.getOrderBy()); convertAddAll(page, elementConvertor, newPage); return newPage; } /** *

自适应可能不进行count

*/ public static Page ofPageNoCountAdapt(Page page) { ofPageNoCountAdapt(page, BaseQuery.MAX_TOTAL_COUNT); return page; } /** *

自适应可能不进行count

* * @param maxTotal 当没有进行count时,限制的最多条数,例如10000条 */ public static Page ofPageNoCountAdapt(Page page, long maxTotal) { noCountAdapt(page, page, maxTotal); return page; } /** *

自适应可能不进行count

*/ public static Page ofPageNoCountAdapt(Page page, Function elementConvertor) { return ofPageNoCountAdapt(page, elementConvertor, BaseQuery.MAX_TOTAL_COUNT); } /** *

自适应可能不进行count

* * @param maxTotal 当没有进行count时,限制的最多条数,例如10000条 */ public static Page ofPageNoCountAdapt(Page page, Function elementConvertor, long maxTotal) { Page newPage = new Page(page.getPageNum(), page.getPageSize()); noCountAdapt(page, newPage, maxTotal); convertAddAll(page, elementConvertor, newPage); return newPage; } /** *

自适应可能不进行count

* * 分页的按正常处理
* 不分页但结果条数小于页大小,则总页数按当前页处理,总条数按(总页数-1)*每页大小+当前返回条数
* 不分页但结果条数等于页大小,则按最大值处理 * * @param maxTotal 当没有进行count时,限制的最多条数,例如10000条 */ private static void noCountAdapt(Page page, Page targetPage, long maxTotal) { if (page.isCount()) { targetPage.setTotal(page.getTotal()); targetPage.setPages(page.getPages()); } else { if (page.getResult().size() < page.getPageSize()) { targetPage.setTotal((page.getPages() - 1) * page.getPageSize() + page.getResult().size()); targetPage.setPages(page.getPageNum()); } else { targetPage.setTotal(maxTotal); targetPage.setPages((int) (maxTotal / page.getPageSize())); } } targetPage.setCount(page.isCount()); targetPage.setOrderBy(page.getOrderBy()); } private static void convertAddAll(Page page, Function elementConvertor, Page targetPage) { if (!page.getResult().isEmpty()) { List list = page.getResult().stream().map(t -> { return elementConvertor.apply(t); }).collect(Collectors.toList()); targetPage.addAll(list); } } // ---------------------------------------------------------- /** * Page 转 NextQuerySupportPage */ public static NextQuerySupportPage pageToNextQuerySupportPage(Page page, Function searchAfterSupplier) { NextQuerySupportList nextQuerySupportList = NextQuerySupportArrayList.newSupportSearchAfter(page, searchAfterSupplier); return new NextQuerySupportPage(page.getPageNum(), page.getPageSize(), page.getTotal(), page.isCount(), page.getOrderBy(), nextQuerySupportList); } /** * Page 转 NextQuerySupportPage */ public static NextQuerySupportPage pageToNextQuerySupportPage(Page page, Function elementConvertor, Function searchAfterSupplier) { NextQuerySupportList nextQuerySupportList = NextQuerySupportArrayList.newSupportSearchAfter(page, elementConvertor, searchAfterSupplier); return new NextQuerySupportPage(page.getPageNum(), page.getPageSize(), page.getTotal(), page.isCount(), page.getOrderBy(), nextQuerySupportList); } /** * 只转换类型 */ public static NextQuerySupportPage ofNextQuerySupportPage(NextQuerySupportPage page, Function elementConvertor) { NextQuerySupportList nextQuerySupportList = NextQuerySupportArrayList.newSupportSearchAfter(page, elementConvertor, one -> page.getSearchAfter()); return new NextQuerySupportPage(page.getPageNum(), page.getPageSize(), page.getTotal(), page.isCount(), page.getOrderBy(), nextQuerySupportList); } /** *

自适应可能不进行count

*/ public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page) { return ofNextQuerySupportPageNoCountAdapt(page, BaseQuery.MAX_TOTAL_COUNT); } /** *

自适应可能不进行count

* * @param maxTotal 当没有进行count时,限制的最多条数,例如10000条 */ public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page, long maxTotal) { noCountAdapt(page, page, maxTotal); return page; } /** *

自适应可能不进行count

*/ public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page, Function elementConvertor) { return ofNextQuerySupportPageNoCountAdapt(page, elementConvertor, BaseQuery.MAX_TOTAL_COUNT); } /** *

自适应可能不进行count

* * @param maxTotal 当没有进行count时,限制的最多条数,例如10000条 */ public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page, Function elementConvertor, long maxTotal) { NextQuerySupportPage newPage = ofNextQuerySupportPage(page, elementConvertor); noCountAdapt(page, newPage, maxTotal); return newPage; } }