package com.canrd.webmagic.common.utils; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.canrd.webmagic.domain.vo.BasePageVO; import org.springframework.util.CollectionUtils; import java.io.Serializable; import java.util.*; /** * @Author: dengbin * @Date: 2020/9/11 * 分页工具类 */ public class PageUtils implements Serializable { private static final long serialVersionUID = 4359709211352400087L; private static final String PAGE_INDEX = "pageNo"; private static final String PAGE_SIZE = "pageSize"; private static final String TOTAL_ROWS = "total"; private static final String TOTAL_PAGES = "pages"; private static final String RECORDS = "records"; private static final int DEFAULT_PAGE_INDEX = 1; private static final int DEFAULT_PAGE_SIZE = 10; private static final int DEFAULT_MAP_CAPACITY = 5; /** * 统一分页返回对象 * * @param list * @return */ public static Map<String, Object> getUnifiedPageReturn(IPage list) { if (list == null) { return getUnifiedEmptyPage(DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE); } Map<String, Object> resultMap = new HashMap<>(DEFAULT_MAP_CAPACITY); resultMap.put(PAGE_INDEX, list.getCurrent()); resultMap.put(PAGE_SIZE, list.getSize()); resultMap.put(RECORDS, list.getRecords()); resultMap.put(TOTAL_PAGES, list.getPages()); resultMap.put(TOTAL_ROWS, list.getTotal()); return resultMap; } /** * 返回空分页信息给前端 * 返回空或其他字段不满足前端要求,会报错 * * @return */ public static Map<String, Object> getUnifiedEmptyPage(int pageIndex, int pageSize) { Map<String, Object> resultMap = new HashMap<>(DEFAULT_MAP_CAPACITY); resultMap.put(PAGE_INDEX, pageIndex); resultMap.put(PAGE_SIZE, pageSize); resultMap.put(RECORDS, new ArrayList<>()); resultMap.put(TOTAL_PAGES, 0); resultMap.put(TOTAL_ROWS, 0); return resultMap; } public static Map<String, Object> getUnifiedEmptyPage(BasePageVO pageVO) { return getUnifiedEmptyPage(pageVO.getCurrent(), pageVO.getSize()); } /** * 将旧的分页转换成新的分页对象 * * @param oldPage 旧的分页对象,例如:xxDo的分页对象 * @param records 新的分页对象,例如:xxVo的分页对象 * @param <T> 新的分页对象的类型 * @return 统一的返回的分页对象 */ public static <T> Map<String, Object> transferPage(IPage<?> oldPage, List<T> records) { Page<T> newPage = new Page<>(); newPage.setCurrent(oldPage.getCurrent()); newPage.setSize(oldPage.getSize()); newPage.setRecords(records); newPage.setPages(oldPage.getPages()); newPage.setTotal(oldPage.getTotal()); return getUnifiedPageReturn(newPage); } /** * 统一分页返回对象 * * @param records 分页数据 * @param pageVO 分页参数 * @return */ public static Map<String, Object> getPageReturn(List records, BasePageVO pageVO) { if (records == null) { return getUnifiedEmptyPage(DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE); } if (CollectionUtils.isEmpty(records)) { return getUnifiedEmptyPage(Math.toIntExact(pageVO.getCurrent()), Math.toIntExact(pageVO.getSize())); } Map<String, Object> resultMap = new HashMap<>(DEFAULT_MAP_CAPACITY); resultMap.put(PAGE_INDEX, pageVO.getCurrent()); resultMap.put(PAGE_SIZE, pageVO.getSize()); resultMap.put(RECORDS, records); resultMap.put(TOTAL_PAGES, (pageVO.getTotal() + pageVO.getSize() - 1) / pageVO.getSize()); resultMap.put(TOTAL_ROWS, pageVO.getTotal()); return resultMap; } public static <T> Page<T> getRAMPageReturn(Integer current, Integer size, List<T> records) { if (Objects.isNull(current)) { current = 1; } if (Objects.isNull(size)) { size = 10; } int startIndex = (current - 1) * size; int endIndex = Math.min(current * size, records.size()); if (startIndex > endIndex) { startIndex = endIndex; } Page<T> page = new Page<>(); page.setCurrent(current); page.setSize(size); page.setRecords(records.subList(startIndex, endIndex)); int totalPages = size == 0 ? 0 : records.size() % size == 0 ? (records.size() / size) : (records.size() / size + 1); page.setPages(totalPages); page.setTotal(records.size()); return page; } }