content: package com.platform.utils;
  
  import org.springframework.data.domain.Page;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.LinkedHashMap;
  import java.util.List;
  import java.util.Map;
  
  public class PageUtil implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      private static Integer DEFAULT_MAX_COUNT = 5000;
  
      // 当前页码
      private Integer pageNum = 1;
      // 分页条数
      private Integer pageSize = 20;
  
      public Integer getPageNum() {
          return pageNum;
      }
  
      public void setPageNum(Integer pageNum) {
          this.pageNum = pageNum;
      }
  
      public Integer getPageSize() {
          return pageSize;
      }
  
      public void setPageSize(Integer pageSize) {
          this.pageSize = pageSize;
          if (this.pageSize > 0) {
              this.pageSize = this.pageSize > DEFAULT_MAX_COUNT ? DEFAULT_MAX_COUNT : this.pageSize;
          } else {
              this.pageSize = 20;
          }
      }
  
      public PageUtil(Integer pageNum, Integer pageSize) {
          this.pageNum = pageNum;
          this.pageSize = pageSize;
          if (this.pageSize > 0) {
              this.pageSize = this.pageSize > DEFAULT_MAX_COUNT ? DEFAULT_MAX_COUNT : this.pageSize;
          } else {
              this.pageSize = 20;
          }
      }
  
      public Integer getOffset() {
          pageSize = pageSize == null ? 20 : pageSize;
          pageNum = pageNum == null ? 1 : pageNum;
          int offset = pageNum > 0 ? (pageNum - 1) * pageSize : 0;
          return offset;
      }
  
      /**
       * List 分页
       */
      public static List toPage(int page, int size , List list) {
          int fromIndex = page * size;
          int toIndex = page * size + size;
          if(fromIndex > list.size()){
              return new ArrayList();
          } else if(toIndex >= list.size()) {
              return list.subList(fromIndex,list.size());
          } else {
              return list.subList(fromIndex,toIndex);
          }
      }
  
      /**
       * Page 数据处理，预防redis反序列化报错
       */
      public static Map<String,Object> toPage(Page page) {
          Map<String,Object> map = new LinkedHashMap<>(2);
          map.put("content",page.getContent());
          map.put("totalElements",page.getTotalElements());
          return map;
      }
  
      /**
       * 自定义分页
       */
      public static Map<String,Object> toPage(Object object, Object totalElements) {
          Map<String,Object> map = new LinkedHashMap<>(2);
          map.put("content",object);
          map.put("totalElements",totalElements);
          return map;
      }
  
  }
  