|
@@ -0,0 +1,56 @@
|
|
|
+package com.gxzc.zen.common.util
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.plugins.Page
|
|
|
+import com.baomidou.mybatisplus.plugins.pagination.Pagination
|
|
|
+
|
|
|
+/**
|
|
|
+ * 逻辑分页工具类
|
|
|
+ * @author NorthLan
|
|
|
+ * @date 2018/3/21
|
|
|
+ * @url https://noahlan.com
|
|
|
+ */
|
|
|
+object LogicPagination {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 逻辑分页 获取分页后的list
|
|
|
+ * @param current 当前页码
|
|
|
+ * @param size 每页容量
|
|
|
+ */
|
|
|
+ fun <T> paginationList(data: MutableList<T>, current: Int, size: Int): MutableList<T> {
|
|
|
+ var fromIndex = (current - 1) * size
|
|
|
+ if (fromIndex > data.size) {
|
|
|
+ fromIndex = data.size
|
|
|
+ }
|
|
|
+ var toIndex = current * size
|
|
|
+ if (toIndex > data.size) {
|
|
|
+ toIndex = data.size
|
|
|
+ }
|
|
|
+ return data.subList(fromIndex, toIndex)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 逻辑分页 获取分页信息(包含分页后的数据)
|
|
|
+ * @param current 当前页码
|
|
|
+ * @param size 每页容量
|
|
|
+ * @return 返回值参数示例
|
|
|
+ * {
|
|
|
+ * "total": 9,
|
|
|
+ * "size": 4,
|
|
|
+ * "pages": 3,
|
|
|
+ * "current": 1,
|
|
|
+ * "records": [
|
|
|
+ * {
|
|
|
+ * "id": 1,
|
|
|
+ * "enable": true,
|
|
|
+ * "key": "gender",
|
|
|
+ * "value": "1",
|
|
|
+ * "label": "男",
|
|
|
+ * "sort": 1
|
|
|
+ * }
|
|
|
+ * ]
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ fun <T> pagination(data: MutableList<T>, current: Int, size: Int): Pagination {
|
|
|
+ return Page<T>(current, size).setRecords(paginationList(data, current, size)).setTotal(data.size)
|
|
|
+ }
|
|
|
+}
|