package com.qs.serve.common.util; import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.pagehelper.PageHelper; import com.qs.serve.common.framework.exception.BusinessException; import lombok.experimental.UtilityClass; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; /** * 翻页工具类 * @Author YenHex * @Date 2021/6/9 * @Version: 1.0 **/ @UtilityClass public class PageUtil { private static final String PAGE_NUM = "pageNum"; private static final String PAGE_SIZE = "pageSize"; private static final String ORDER_TYPE = "orderType"; private static final String ORDER_PROP = "orderProp"; private static final String ORDER_TYPE_ASC = "ASC"; private static final String ORDER_TYPE_DESC = "DESC"; private static final int MAX_PAGE_SIZE = 66; public static Page getMbpPage(){ Integer pageNum = getPageNum(); Integer pageSize = getPageSize(); String orderProp = getOrderProp(); String orderType = getOrderType(); if ( pageNum ==null || pageNum<1 || pageSize==null || pageSize<1) { throw new BusinessException("翻页参数异常",500); } Page page = new Page<>(); page.setCurrent(pageNum); page.setSize(pageSize); if(StringUtils.hasText(orderProp)){ OrderItem orderItem = new OrderItem(); orderItem.setColumn(orderProp); orderItem.setAsc(orderType.equals(ORDER_TYPE_ASC)); List orderItems = new ArrayList<>(); orderItems.add(orderItem); page.setOrders(orderItems); } return page; } public static Integer getPageNum(){ Integer pageNum=null; String pageNumStr = ServletUtils.getParameter(PAGE_NUM); if(StringUtils.hasText(pageNumStr)){ pageNum = Integer.parseInt(pageNumStr); } return pageNum; } public static Integer getPageSize(){ String pageSizeStr = ServletUtils.getParameter(PAGE_SIZE); Integer pageSize = null; if(StringUtils.hasText(pageSizeStr)){ pageSize = Integer.parseInt(pageSizeStr); } if(pageSize!=null && pageSize > MAX_PAGE_SIZE){ throw new BusinessException("超出数据额度",500); } return pageSize; } public static String getOrderProp(){ String orderProp = ServletUtils.getParameter(ORDER_PROP); if(StringUtils.hasText(orderProp)){ orderProp = orderProp.trim(); if(orderProp.contains(".")){ String[] strings = orderProp.split("\\."); return strings[0]+"."+strings[1]; } return WordUtil.toLine(orderProp); } return null; } public static String getOrderType(){ String orderType = ServletUtils.getParameter(ORDER_TYPE); if(StringUtils.hasText(orderType)){ if(orderType.equalsIgnoreCase(ORDER_TYPE_DESC)){ return ORDER_TYPE_DESC; } } return ORDER_TYPE_ASC; } public static Integer getStartRow(){ Integer pageNum = getPageNum(); Integer pageSize = getPageSize(); if (pageNum==null || pageSize==null) { throw new BusinessException("翻页参数异常",500); } return (pageNum-1)*pageSize; } /** * 启动翻页(翻页失败,会抛出异常) * @return 是否使用翻页 */ public static boolean startPage(){ return startPage(true); } /** * * @param throwEx 是否抛异常 * @return 是否使用翻页 */ public static boolean startPage(boolean throwEx){ return startPage(null,null,throwEx); } /** * 执行翻页 * @param object POJO * @return 是否使用翻页 */ public static boolean startPage(T object){ return startPage(object,null,true); } public static boolean startPage(String prefix){ return startPage(null,prefix,true); } /** * * @param throwEx 是否抛异常 * @return 是否使用翻页 */ public static boolean startPage(T object,String prefix,boolean throwEx){ Integer pageNum = getPageNum(); Integer pageSize = getPageSize(); String orderProp = getOrderProp(); String orderType = getOrderType(); if (pageNum!=null && pageNum>0 && pageSize!=null && pageSize>0) { if(!StringUtils.isEmpty(orderProp)){ if(prefix==null && object!=null){ prefix = WordUtil.toLine(object.getClass().getSimpleName()); } if(prefix!=null){ PageHelper.startPage(pageNum, pageSize).setOrderBy(prefix+"."+orderProp+" "+orderType); }else { PageHelper.startPage(pageNum, pageSize).setOrderBy(orderProp+" "+orderType); } return true; } PageHelper.startPage(pageNum, pageSize); return true; }else{ if(throwEx){ throw new BusinessException("翻页参数异常",500); } return false; } } }