Procházet zdrojové kódy

佰穗莱分页代码优化

ChenYang před 1 rokem
rodič
revize
7db038b0a7

+ 4 - 4
ruoyi-admin/src/main/java/com/ruoyi/web/controller/warewms/org/OrgController.java

@@ -1,9 +1,9 @@
 package com.ruoyi.web.controller.warewms.org;
 
+import com.github.pagehelper.PageInfo;
 import com.ruoyi.ams.bionutrition.org.bean.dto.OrgDTO;
 import com.ruoyi.ams.bionutrition.org.service.IOrgSevice;
 import com.ruoyi.base.constant.type.BizEnum;
-import com.ruoyi.base.domain.BaseCustomer;
 import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
@@ -48,9 +48,9 @@ public class OrgController extends BaseController {
     @PreAuthorize("@ss.hasPermi('base:customer:list')")
     @GetMapping("/list")
     public TableDataInfo list(OrgDTO orgDTO) {
-        PageUtils.pageStart();
-        List<OrgDTO> orgList = orgSevice.getOrgDTOList(orgDTO);
-        return PageUtils.getDataTable(orgList);
+        PageUtils.startPage();
+        PageInfo<OrgDTO> orgPageInfo = orgSevice.getOrgPageInfo(orgDTO);
+        return getDataTable(orgPageInfo);
     }
 
     /**

+ 19 - 7
ruoyi-common/src/main/java/com/ruoyi/common/core/controller/BaseController.java

@@ -1,12 +1,5 @@
 package com.ruoyi.common.core.controller;
 
-import java.beans.PropertyEditorSupport;
-import java.util.Date;
-import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.InitBinder;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.ruoyi.common.constant.HttpStatus;
@@ -20,6 +13,14 @@ import com.ruoyi.common.utils.PageUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.sql.SqlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+
+import java.beans.PropertyEditorSupport;
+import java.util.Date;
+import java.util.List;
 
 /**
  * web层通用数据处理
@@ -82,6 +83,17 @@ public class BaseController
         return rspData;
     }
 
+
+    protected TableDataInfo getDataTable(PageInfo pageInfo)
+    {
+        TableDataInfo rspData = new TableDataInfo();
+        rspData.setCode(HttpStatus.SUCCESS);
+        rspData.setMsg("查询成功");
+        rspData.setRows(pageInfo.getList());
+        rspData.setTotal(pageInfo.getTotal());
+        return rspData;
+    }
+
     /**
      * 返回成功
      */

+ 61 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtil.java

@@ -0,0 +1,61 @@
+package com.ruoyi.common.utils;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.github.pagehelper.Page;
+import com.github.pagehelper.PageInfo;
+import com.google.common.collect.Lists;
+
+import java.util.List;
+
+/**
+ * 分页工具类
+ * @author
+ */
+public class PageUtil{
+
+    /**
+     * 开始分页
+     * @param list
+     * @param pageNum 页码
+     * @param pageSize 每页多少条数据
+     * @return  返回值为null请检查入参
+     */
+    public static <T> List<T> startPage(List<T> list, Integer pageNum,
+                                        Integer pageSize) {
+        if (list == null || ObjectUtil.isNull(pageNum) || ObjectUtil.isNull(pageSize)
+                || pageNum < 1 || pageSize < 1) {
+            return null;
+        }
+        if((list.size() == 0 || (list.size()/pageSize + 1) * pageSize < pageNum * pageSize)){
+            return Lists.newArrayList();
+        }
+
+        Integer count = list.size(); // 记录总数
+        Integer pageCount = 0; // 页数
+        if (count % pageSize == 0) {
+            pageCount = count / pageSize;
+        } else {
+            pageCount = count / pageSize + 1;
+        }
+
+        int fromIndex = 0; // 开始索引
+        int toIndex = 0; // 结束索引
+        pageNum = Math.min(pageNum, pageCount);
+        if (!pageNum.equals(pageCount)) {
+            fromIndex = (pageNum - 1) * pageSize;
+            toIndex = fromIndex + pageSize;
+        } else {
+            fromIndex = (pageNum - 1) * pageSize;
+            toIndex = count;
+        }
+
+        return list.subList(fromIndex, Math.min(toIndex, list.size()));
+    }
+
+
+    public static <E, T> PageInfo<T> convert(Page<E> page, Class<T> targetClass) {
+        if(CollectionUtil.isNotEmpty(page)) return PageInfo.of(new Page<>());
+        return page.toPageInfo(item -> ConvertUtils.sourceToTarget(item, targetClass));
+    }
+}

+ 0 - 32
ruoyi-common/src/main/java/com/ruoyi/common/utils/PageUtils.java

@@ -1,23 +1,16 @@
 package com.ruoyi.common.utils;
 
-import com.github.pagehelper.Page;
 import com.github.pagehelper.PageHelper;
-import com.ruoyi.common.constant.HttpStatus;
 import com.ruoyi.common.core.page.PageDomain;
-import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.core.page.TableSupport;
 import com.ruoyi.common.utils.sql.SqlUtil;
 
-import java.util.List;
-
 /**
  * 分页工具类
- *
  * @author ruoyi
  */
 public class PageUtils extends PageHelper
 {
-    private static Page page;
 
     /**
      * 设置请求分页数据
@@ -35,29 +28,4 @@ public class PageUtils extends PageHelper
         }
     }
 
-/****************************************************************/
-    /**
-     * 设置请求分页数据
-     */
-    public static void pageStart() {
-        PageDomain pageDomain = TableSupport.buildPageRequest();
-        /*pageDomain.setPageSize(pageSize);
-        pageDomain.setPageNum(pageNum);*/
-        Integer pageNum = pageDomain.getPageNum();
-        Integer pageSize = pageDomain.getPageSize();
-        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
-            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
-            Boolean reasonable = pageDomain.getReasonable();
-            page = PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
-        }
-    }
-
-    public static TableDataInfo getDataTable(List<?> list) {
-        TableDataInfo rspData = new TableDataInfo();
-        rspData.setCode(HttpStatus.SUCCESS);
-        rspData.setMsg("查询成功");
-        rspData.setRows(list);
-        rspData.setTotal(page.getTotal());
-        return rspData;
-    }
 }

+ 3 - 0
warewms-ams/src/main/java/com/ruoyi/ams/bionutrition/org/service/IOrgSevice.java

@@ -1,5 +1,6 @@
 package com.ruoyi.ams.bionutrition.org.service;
 
+import com.github.pagehelper.PageInfo;
 import com.ruoyi.ams.bionutrition.org.bean.dto.OrgDTO;
 import com.ruoyi.ams.bionutrition.org.bean.entity.OrgEntity;
 import com.ruoyi.framework.service.CrudService;
@@ -8,6 +9,8 @@ import java.util.List;
 
 public interface IOrgSevice extends CrudService<OrgEntity, OrgDTO> {
 
+    PageInfo<OrgDTO> getOrgPageInfo(OrgDTO orgDTO);
+
     /**
      * 获取机构列表
      * @author

+ 9 - 9
warewms-ams/src/main/java/com/ruoyi/ams/bionutrition/org/service/impl/OrgServiceImpl.java

@@ -6,35 +6,35 @@ import cn.hutool.core.lang.Assert;
 import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
-import com.baomidou.mybatisplus.core.toolkit.IdWorker;
+import com.github.pagehelper.Page;
+import com.github.pagehelper.PageInfo;
+import com.google.common.collect.Lists;
 import com.ruoyi.ams.bionutrition.org.bean.dto.OrgDTO;
 import com.ruoyi.ams.bionutrition.org.bean.entity.OrgEntity;
 import com.ruoyi.ams.bionutrition.org.mapper.OrgMapper;
 import com.ruoyi.ams.bionutrition.org.service.IOrgSevice;
-import com.ruoyi.base.constant.type.BizEnum;
 import com.ruoyi.common.utils.ConvertUtils;
+import com.ruoyi.common.utils.PageUtil;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.service.impl.CrudServiceImpl;
-import org.apache.commons.compress.utils.Lists;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
-import java.util.Objects;
 
 @Service
 public class OrgServiceImpl extends CrudServiceImpl<OrgMapper, OrgEntity, OrgDTO> implements IOrgSevice {
 
-    @Autowired
-    private OrgMapper orgMapper;
+    @Override
+    public PageInfo<OrgDTO> getOrgPageInfo(OrgDTO orgDTO) {
+        Page<OrgEntity> orgEntitiesPage = (Page<OrgEntity>)baseDao.selectOrgs(BeanUtil.beanToMap(orgDTO));
+        return PageUtil.convert(orgEntitiesPage, OrgDTO.class) ;
+    }
 
     @Override
     public List<OrgDTO> getOrgDTOList(OrgDTO orgDTO) {
         List<OrgEntity> orgEntities = baseDao.selectList(getWrapper(BeanUtil.beanToMap(orgDTO)));
-//        List<OrgEntity> orgEntities = orgMapper.selectOrgs(BeanUtil.beanToMap(orgDTO));
         return CollectionUtil.isNotEmpty(orgEntities)?
                 ConvertUtils.sourceToTarget(orgEntities, OrgDTO.class) : Lists.newArrayList();
     }