分頁組件工具類


package com.mischen.common.core.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @類功能說明: 分頁組件.
 * @類修改者:
 * @修改日期:
 * @修改說明:
 * @作者:mischen
 * @創建時間:2020-02-09 上午09:13:59
 * @版本:V1.0
 */
public class PageBean<T> implements Serializable {
    /**
   * 
   */
    private static final long serialVersionUID = 84706979782548953214L;

    // 指定的或是頁面參數
    private int currentPage; // 當前頁
    private int numPerPage; // 每頁顯示多少條

    // 查詢數據庫
    private int totalCount; // 總記錄數
    private List<T> recordList =new  ArrayList<T>(0); // 本頁的數據列表

    // 計算
    private int totalPage; // 總頁數
    private int beginPageIndex; // 頁碼列表的開始索引(包含)
    private int endPageIndex; // 頁碼列表的結束索引(包含)

    private Map<String, Object> countResultMap; // 當前分頁條件下的統計結果

    public PageBean() {
    }

    /**
     * 計算總頁數 .
     * 
     * @param totalCount
     *            總記錄數.
     * @param numPerPage
     *            每頁記錄數.
     * @return totalPage 總頁數.
     */
    public static int countTotalPage(int totalCount, int numPerPage) {
        if (totalCount % numPerPage == 0) {
            // 剛好整除
            return totalCount / numPerPage;
        } else {
            // 不能整除則總頁數爲:商 + 1
            return totalCount / numPerPage + 1;
        }
    }

    /**
     * 校驗當前頁數currentPage.<br/>
     * 1、先根據總記錄數totalCount和每頁記錄數numPerPage,計算出總頁數totalPage.<br/>
     * 2、判斷頁面提交過來的當前頁數currentPage是否大於總頁數totalPage,大於則返回totalPage.<br/>
     * 3、判斷currentPage是否小於1,小於則返回1.<br/>
     * 4、其它則直接返回currentPage .
     * 
     * @param totalCount
     *            要分頁的總記錄數 .
     * @param numPerPage
     *            每頁記錄數大小 .
     * @param currentPage
     *            輸入的當前頁數 .
     * @return currentPage .
     */
    public static int checkCurrentPage(int totalCount, int numPerPage,
            int currentPage) {
        int totalPage = PageBean.countTotalPage(totalCount, numPerPage); // 最大頁數
        if (currentPage > totalPage) {
            // 如果頁面提交過來的頁數大於總頁數,則將當前頁設爲總頁數
            // 此時要求totalPage要大於獲等於1
            if (totalPage < 1) {
                return 1;
            }
            return totalPage;
        } else if (currentPage < 1) {
            return 1; // 當前頁不能小於1(避免頁面輸入不正確值)
        } else {
            return currentPage;
        }
    }

    /**
     * 校驗頁面輸入的每頁記錄數numPerPage是否合法 .<br/>
     * 1、當頁面輸入的每頁記錄數numPerPage大於允許的最大每頁記錄數MAX_PAGE_SIZE時,返回MAX_PAGE_SIZE.
     * 2、如果numPerPage小於1,則返回默認的每頁記錄數DEFAULT_PAGE_SIZE.
     * 
     * @param numPerPage
     *            頁面輸入的每頁記錄數 .
     * @return checkNumPerPage .
     */
    public static int checkNumPerPage(int numPerPage) {
        if (numPerPage > PageParam.MAX_PAGE_SIZE) {
            return PageParam.MAX_PAGE_SIZE;
        } else if (numPerPage < 1) {
            return PageParam.DEFAULT_NUM_PER_PAGE;
        } else {
            return numPerPage;
        }
    }

    /**
     * 只接受前4個必要的屬性,會自動的計算出其他3個屬生的值
     * 
     * @param currentPage
     * @param numPerPage
     * @param totalCount
     * @param recordList
     */
    public PageBean(int currentPage, int numPerPage, int totalCount,
            List<T> recordList) {
        this.currentPage = currentPage;
        this.numPerPage = numPerPage;
        this.totalCount = totalCount;
        this.recordList = recordList;

        // 計算總頁碼
        totalPage = (totalCount + numPerPage - 1) / numPerPage;

        // 計算 beginPageIndex 和 endPageIndex
        if (totalPage <= 10) {
            // 如果總頁數不多於10頁,則全部顯示
            beginPageIndex = 1;
            endPageIndex = totalPage;
        } else {
            // 如果總頁數多於10頁,則顯示當前頁附近的共10個頁碼
            // 當前頁附近的共10個頁碼(前4個 + 當前頁 + 後5個)
            beginPageIndex = currentPage - 4;
            endPageIndex = currentPage + 5;
            // 當前面的頁碼不足4個時,則顯示前10個頁碼
            if (beginPageIndex < 1) {
                beginPageIndex = 1;
                endPageIndex = 10;
            }
            // 當後面的頁碼不足5個時,則顯示後10個頁碼
            if (endPageIndex > totalPage) {
                endPageIndex = totalPage;
                beginPageIndex = totalPage - 10 + 1;
            }
        }
    }

    /**
     * 只接受前5個必要的屬性,會自動的計算出其他3個屬生的值
     * 
     * @param currentPage
     * @param numPerPage
     * @param totalCount
     * @param recordList
     */
    public PageBean(int currentPage, int numPerPage, int totalCount,
            List<T> recordList, Map<String, Object> countResultMap) {
        this.currentPage = currentPage;
        this.numPerPage = numPerPage;
        this.totalCount = totalCount;
        this.recordList = recordList;
        this.countResultMap = countResultMap;

        // 計算總頁碼
        totalPage = (totalCount + numPerPage - 1) / numPerPage;

        // 計算 beginPageIndex 和 endPageIndex
        if (totalPage <= 10) {
            // 如果總頁數不多於10頁,則全部顯示
            beginPageIndex = 1;
            endPageIndex = totalPage;
        } else {
            // 如果總頁數多於10頁,則顯示當前頁附近的共10個頁碼
            // 當前頁附近的共10個頁碼(前4個 + 當前頁 + 後5個)
            beginPageIndex = currentPage - 4;
            endPageIndex = currentPage + 5;
            // 當前面的頁碼不足4個時,則顯示前10個頁碼
            if (beginPageIndex < 1) {
                beginPageIndex = 1;
                endPageIndex = 10;
            }
            // 當後面的頁碼不足5個時,則顯示後10個頁碼
            if (endPageIndex > totalPage) {
                endPageIndex = totalPage;
                beginPageIndex = totalPage - 10 + 1;
            }
        }
    }

    public List<T> getRecordList() {
        return recordList;
    }

    public void setRecordList(List<T> recordList) {
        this.recordList = recordList;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getNumPerPage() {
        return numPerPage;
    }

    public void setNumPerPage(int numPerPage) {
        this.numPerPage = numPerPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getBeginPageIndex() {
        return beginPageIndex;
    }

    public void setBeginPageIndex(int beginPageIndex) {
        this.beginPageIndex = beginPageIndex;
    }

    public int getEndPageIndex() {
        return endPageIndex;
    }

    public void setEndPageIndex(int endPageIndex) {
        this.endPageIndex = endPageIndex;
    }

    public Map<String, Object> getCountResultMap() {
        return countResultMap;
    }

    public void setCountResultMap(Map<String, Object> countResultMap) {
        this.countResultMap = countResultMap;
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章