001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
004 *    Licensed under the Apache License, Version 2.0 (the "License");
005 *    you may not use this file except in compliance with the License.
006 *    You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 *    Unless required by applicable law or agreed to in writing, software
011 *    distributed under the License is distributed on an "AS IS" BASIS,
012 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *    See the License for the specific language governing permissions and
014 *    limitations under the License.
015 */
016package org.dromara.warm.flow.core.utils.page;
017
018import java.util.ArrayList;
019import java.util.List;
020
021/**
022 * 分页
023 *
024 * @author warm
025 * @since 2023/5/17 1:28
026 */
027public class Page<T> implements OrderBy {
028
029    /**
030     * 当前记录起始索引
031     */
032    private int pageNum = 1;
033
034    /**
035     * 每页显示记录数
036     */
037    private int pageSize = 10;
038
039    /**
040     * 数据
041     */
042    private List<T> list;
043
044    /**
045     * 总量
046     */
047    private long total;
048
049    /**
050     * 排序列
051     */
052    private String orderBy;
053
054    /**
055     * 排序的方向desc或者asc
056     */
057    private String isAsc = "desc";
058
059    public Page() {
060        this.pageNum = 1;
061        this.pageSize = 10;
062    }
063
064    public Page(int pageNum, int pageSize) {
065        this.pageNum = pageNum;
066        this.pageSize = pageSize;
067    }
068
069    public Page(int pageNum, int pageSize, String orderBy, String isAsc) {
070        this.pageNum = pageNum;
071        this.pageSize = pageSize;
072        this.orderBy = orderBy;
073        this.isAsc = isAsc;
074    }
075
076    public Page(List<T> list, long total) {
077        this.list = list;
078        this.total = total;
079    }
080
081    public Page(long total) {
082        this.list = new ArrayList<>();
083        this.total = total;
084    }
085
086    public static <T> Page<T> empty() {
087        return new Page<>(0);
088    }
089
090    /**
091     * 计算分页起始页
092     *
093     * @param pageNum  当前页码
094     * @param size     每页显示记录数
095     * @return 分页结果
096     */
097    public static <T> Page<T> pageOf(Integer pageNum, Integer size) {
098        return new Page<>(pageNum, size);
099    }
100
101    public int getPageNum() {
102        return pageNum;
103    }
104
105    public void setPageNum(int pageNum) {
106        this.pageNum = pageNum;
107    }
108
109    public int getPageSize() {
110        return pageSize;
111    }
112
113    public void setPageSize(int pageSize) {
114        this.pageSize = pageSize;
115    }
116
117    public List<T> getList() {
118        return list;
119    }
120
121    public void setList(List<T> list) {
122        this.list = list;
123    }
124
125    public long getTotal() {
126        return total;
127    }
128
129    public void setTotal(long total) {
130        this.total = total;
131    }
132
133    @Override
134    public String getOrderBy() {
135        return orderBy;
136    }
137
138    public Page<T> setOrderBy(String orderBy) {
139        this.orderBy = orderBy;
140        return this;
141    }
142
143    @Override
144    public String getIsAsc() {
145        return isAsc;
146    }
147
148    public Page<T> setIsAsc(String isAsc) {
149        this.isAsc = isAsc;
150        return this;
151    }
152
153}