All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.web.beans.PageInfo Maven / Gradle / Ivy

The newest version!
package com.rt.web.beans;

import com.rt.core.beans.Property;
import com.rt.core.util.RTUtil;

/**
 * 计算分页信息
 * 通过总记录数和当前需要显示记录数 计算总页数,当前页,前一页,下一页等信息
 */
public class PageInfo extends Property {

    private static final long serialVersionUID = 1L;

    /**
     * 属性名称
     */
    public static final String ID = "pageInfo";
    /**
     * 显示页数
     */
    private final static String SHOW_PAGE = "showPage";
    /**
     * 总记录数
     */
    private final static String TOTAL_ROW = "totalRow";
    /**
     * 总记页数
     */
    private final static String TOTAL_PAGE = "totalPage";
    /**
     * 当前页
     */
    private final static String NOW_PAGE = "nowPage";
    /**
     * 开始记录数
     */
    public final static String START_ROW = "startRow";
    /**
     * 每次查询最大值
     */
    public final static String MAX_ROW = "maxRow";
    private final static String FIRST_PAGE = "firstPage";
    private final static String LAST_PAGE = "lastPage";
    private final static String PREV_PAGE = "prevPage";
    private final static String NEXT_PAGE = "nextPage";

    /**
     * 默认构造函数
     */
    public PageInfo() {
    }

    /**
     * 计算分页
     *
     * @param startRow startRow
     * @param maxRow   maxRow
     * @param totalRow totalRow
     */
    public void computePage(String startRow, String maxRow, String totalRow) {
        computePage(RTUtil.tolong(startRow), RTUtil.tolong(maxRow),
                RTUtil.tolong(totalRow));
    }

    /**
     * 计算分页
     *
     * @param startRow startRow
     * @param maxRow   maxRow
     * @param totalRow totalRow
     */
    public void computePage(long startRow, long maxRow, long totalRow) {
        // 赋值
        setStartRow(startRow);
        setMaxRow(maxRow);
        setTotalRow(totalRow);
        // 计算
        setTotalPage();
        setNowPage();
        setPrevPage();
        setNextPage();
        setFirstPage();
        setLastPage();
    }

    /**
     * 计算当前页
     */
    private void setNowPage() {
        if (getMaxRow() > 0) {
            put(NOW_PAGE, (getStartRow() / getMaxRow()) + 1);
        } else {
            put(NOW_PAGE, 0);
        }
    }

    /**
     * 计算总页数和偏移量
     */
    private void setTotalPage() {
        // 计算当前页
        // 计算总页数
        long totalPage;
        if (getMaxRow() > getTotalRow()) {
            totalPage = 1;
        } else {
            totalPage = getTotalRow() / getMaxRow();
            // 如果产生余数,总页数+1.
            if (getTotalRow() % getMaxRow() != 0) {
                totalPage += 1;
            }
        }
        put(TOTAL_PAGE, totalPage);
    }

    /**
     * 设置开始记录数
     *
     * @param startRow startRow
     */
    public void setStartRow(long startRow) {
        put(START_ROW, startRow);
    }

    /**
     * 设置每页最大记录数
     *
     * @param maxRow maxRow
     */
    public void setMaxRow(long maxRow) {
        put(MAX_ROW, maxRow);
    }

    /**
     * 设置总记录数
     *
     * @param totalRow totalRow
     */
    public void setTotalRow(long totalRow) {
        put(TOTAL_ROW, totalRow);
    }

    /**
     * 计算第一页
     */
    private void setFirstPage() {
        if (getTotalPage() <= 0) {
            put(FIRST_PAGE, 0);
        } else {
            put(FIRST_PAGE, 1);
        }
    }

    /**
     * 计算最后一页
     */
    private void setLastPage() {
        put(LAST_PAGE, getTotalPage());
    }

    /**
     * 计算上一页
     */
    private void setPrevPage() {
        long prevPage = getNowPage() - 1;
        if (getTotalPage() > 0) {
            if (prevPage <= 0) {
                prevPage = 1;
            }
        }
        put(PREV_PAGE, prevPage);
    }

    /**
     * 计算下一页
     */
    private void setNextPage() {
        long nextPage = getNowPage() + 1;
        if (nextPage > getTotalPage()) {
            nextPage = getTotalPage();
        }
        put(NEXT_PAGE, nextPage);
    }

    /**
     * 获取最大行数
     *
     * @return long the maxRow
     */
    public long getMaxRow() {
        return getLong(MAX_ROW);
    }

    /**
     * 第一页
     *
     * @return long the firstPage
     */
    public long getFirstPage() {
        return getLong(FIRST_PAGE);
    }

    /**
     * 最后一页
     *
     * @return long the lastPage
     */
    public long getLastPage() {
        return getLong(LAST_PAGE);
    }

    /**
     * 下一页
     *
     * @return long the nextPage
     */
    public long getNextPage() {
        return getLong(NEXT_PAGE);
    }

    /**
     * 开始记录数
     *
     * @return long the offset
     */
    public long getStartRow() {
        return getLong(START_ROW);
    }

    /**
     * 下一页
     *
     * @return long the prevPage
     */
    public long getPrevPage() {
        return getLong(PREV_PAGE);
    }

    /**
     * 总页数
     *
     * @return long the totalPage
     */
    public long getTotalPage() {
        return getLong(TOTAL_PAGE);
    }

    /**
     * 总数据量
     *
     * @return long the totalRow
     */
    public long getTotalRow() {
        return getLong(TOTAL_ROW);
    }

    /**
     * 当前页
     *
     * @return long the nowPage
     */
    public long getNowPage() {
        return getLong(NOW_PAGE);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy