| | |
| | | package com.ruoyi.common.core.page; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.metadata.OrderItem; |
| | | import lombok.Data; |
| | |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 分页 Page 增强对象 |
| | | * |
| | | * @param <T> 数据库实体 |
| | | * @param <K> vo实体 |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @Accessors(chain = true) |
| | | public class PagePlus<T,K> implements IPage<T> { |
| | | |
| | | protected List<T> records; |
| | | protected List<K> recordsVo; |
| | | protected long total; |
| | | protected long size; |
| | | protected long current; |
| | | protected List<OrderItem> orders; |
| | | protected boolean optimizeCountSql; |
| | | protected boolean isSearchCount; |
| | | protected boolean hitCount; |
| | | protected String countId; |
| | | protected Long maxLimit; |
| | | /** |
| | | * domain实体列表 |
| | | */ |
| | | private List<T> records = Collections.emptyList(); |
| | | |
| | | /** |
| | | * vo实体列表 |
| | | */ |
| | | private List<K> recordsVo = Collections.emptyList(); |
| | | |
| | | /** |
| | | * 总数 |
| | | */ |
| | | private long total = 0L; |
| | | |
| | | /** |
| | | * 页长度 |
| | | */ |
| | | private long size = 10L; |
| | | |
| | | /** |
| | | * 当前页 |
| | | */ |
| | | private long current = 1L; |
| | | |
| | | /** |
| | | * 排序字段信息 |
| | | */ |
| | | private List<OrderItem> orders = new ArrayList<>(); |
| | | |
| | | /** |
| | | * 自动优化 COUNT SQL |
| | | */ |
| | | private boolean optimizeCountSql = true; |
| | | |
| | | /** |
| | | * 是否进行 count 查询 |
| | | */ |
| | | private boolean isSearchCount = true; |
| | | |
| | | /** |
| | | * 是否命中count缓存 |
| | | */ |
| | | private boolean hitCount = false; |
| | | |
| | | /** |
| | | * countId |
| | | */ |
| | | private String countId; |
| | | |
| | | /** |
| | | * 最大limit |
| | | */ |
| | | private Long maxLimit; |
| | | |
| | | public PagePlus() { |
| | | this.records = Collections.emptyList(); |
| | | this.recordsVo = Collections.emptyList(); |
| | | this.total = 0L; |
| | | this.size = 10L; |
| | | this.current = 1L; |
| | | this.orders = new ArrayList(); |
| | | this.optimizeCountSql = true; |
| | | this.isSearchCount = true; |
| | | this.hitCount = false; |
| | | } |
| | | |
| | | public PagePlus(long current, long size) { |
| | |
| | | } |
| | | |
| | | public PagePlus(long current, long size, long total, boolean isSearchCount) { |
| | | this.records = Collections.emptyList(); |
| | | this.total = 0L; |
| | | this.size = 10L; |
| | | this.current = 1L; |
| | | this.orders = new ArrayList(); |
| | | this.optimizeCountSql = true; |
| | | this.isSearchCount = true; |
| | | this.hitCount = false; |
| | | if (current > 1L) { |
| | | this.current = current; |
| | | } |
| | | |
| | | this.size = size; |
| | | this.total = total; |
| | | this.isSearchCount = isSearchCount; |
| | | } |
| | | |
| | | public boolean hasPrevious() { |
| | | return this.current > 1L; |
| | | } |
| | | |
| | | public boolean hasNext() { |
| | | return this.current < this.getPages(); |
| | | } |
| | | |
| | | public void recordsToVo(Class<K> kClass) { |
| | | this.recordsVo = this.records.stream() |
| | | .map(any -> BeanUtil.toBean(any, kClass)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return this.optimizeCountSql; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isSearchCount() { |
| | | return this.total < 0L ? false : this.isSearchCount; |
| | | } |
| | | @Override |
| | | public long getPages() { |
| | | // 解决 github issues/3208 |
| | | return IPage.super.getPages(); |
| | | } |
| | | |
| | | public PagePlus<T, K> setSearchCount(boolean isSearchCount) { |
| | | this.isSearchCount = isSearchCount; |
| | | return this; |
| | | } |
| | | public static <T,K> PagePlus<T,K> of(long current, long size) { |
| | | return of(current, size, 0); |
| | | } |
| | | |
| | | public static <T,K> PagePlus<T,K> of(long current, long size, long total) { |
| | | return of(current, size, total, true); |
| | | } |
| | | |
| | | public static <T,K> PagePlus<T,K> of(long current, long size, boolean searchCount) { |
| | | return of(current, size, 0, searchCount); |
| | | } |
| | | |
| | | public static <T,K> PagePlus<T,K> of(long current, long size, long total, boolean searchCount) { |
| | | return new PagePlus<>(current, size, total, searchCount); |
| | | } |
| | | |
| | | } |
| | | |