Page.java 8.22 KB
package com.qgutech.model;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xxx
 * @date 2024/12/26 15:21
 * @description
 */


public class Page<T> {
    public static final String ASC = "asc";
    public static final String DESC = "desc";
    protected String id;
    protected int pageNo = 1;
    protected int pageSize = 10;
    protected int pages = 1;
    protected boolean autoCount = true;
    protected boolean autoPaging = true;
    private String sortName;
    private String sortOrder;
    protected List<T> rows = new ArrayList();
    protected long total = 0L;

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Page() {
    }

    public Page(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return this.pageNo;
    }

    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;
        if (pageNo < 1) {
            this.pageNo = 1;
        }

    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
    }

    public int getFirst() {
        return (this.pageNo - 1) * this.pageSize + 1;
    }

    public boolean isAutoCount() {
        return this.autoCount;
    }

    public void setAutoCount(final boolean autoCount) {
        this.autoCount = autoCount;
    }

    public Page<T> autoCount(final boolean theAutoCount) {
        this.setAutoCount(theAutoCount);
        return this;
    }

    public List<T> getRows() {
        return this.rows;
    }

    public void setRows(final List<T> result) {
        this.rows = result;
    }

    public long getTotal() {
        return this.total;
    }

    public void setTotal(final long totalCount) {
        this.total = totalCount;
    }

    public long getTotalPages() {
        long count = this.total / (long) this.pageSize;
        if (this.total % (long) this.pageSize > 0L) {
            ++count;
        }

        return count;
    }

    public boolean isHasNext() {
        return (long) (this.pageNo + 1) <= this.getTotalPages();
    }

    public int getNextPage() {
        return this.isHasNext() ? this.pageNo + 1 : this.pageNo;
    }

    public boolean isHasPre() {
        return this.pageNo - 1 >= 1;
    }

    public int getPrePage() {
        return this.isHasPre() ? this.pageNo - 1 : this.pageNo;
    }

    public String getSortName() {
        return this.sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        return this.sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public boolean isAutoPaging() {
        return this.autoPaging;
    }

    public void setAutoPaging(boolean autoPaging) {
        this.autoPaging = autoPaging;
    }

    public String toString() {
        return "Page{total=" + this.total + ", pageNo=" + this.pageNo + ", pageSize=" + this.pageSize + ", autoCount=" + this.autoCount + ", sortName='" + this.sortName + '\'' + ", sortOrder='" + this.sortOrder + '\'' + '}';
    }

    public String toTwitterStylePager() {
        Object redisI18nService = null;

        try {
            ApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext();
            redisI18nService = applicationContext.getBean("redisI18nService");
        } catch (Exception var14) {
        }

        String firstPage = "首页";
        String pageUp = "上一页";
        String pageDown = "下一页";
        String lastPage = "末页";
        if (redisI18nService != null) {
            try {
                Method method = redisI18nService.getClass().getMethod("getI18nValue", String.class);
                method.setAccessible(true);
                firstPage = method.invoke(redisI18nService, "common.firstPage").toString();
                pageUp = method.invoke(redisI18nService, "common.pageUp").toString();
                pageDown = method.invoke(redisI18nService, "common.pageDown").toString();
                lastPage = method.invoke(redisI18nService, "common.lastPage").toString();
            } catch (Exception var13) {
            }
        }

        StringBuilder pager = new StringBuilder();
        boolean isFirstRightDot = false;
        boolean isFirstLefttDot = false;
        boolean isShow = false;
        long totalPageNum = this.getTotalPages();
        pager.append("<ul>");
        pager.append("<li");
        if (this.pageNo == 1) {
            pager.append(" class=\"active\"");
        }

        pager.append("><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\"");
        if (this.pageNo != 1) {
            pager.append(" onclick=\"T.jump('").append(this.id).append("',1)\"");
        }

        pager.append(">").append(firstPage).append("</a></li>");
        pager.append("<li");
        if (this.pageNo == 1) {
            pager.append(" class=\"active\"");
        }

        pager.append("><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\"");
        if (this.pageNo != 1) {
            pager.append(" onclick=\"T.jump('").append(this.id).append("','").append(this.pageNo - 1).append("')\"");
        }

        pager.append(">").append(pageUp).append("</a></li>");

        for (int i = 1; (long) i <= totalPageNum; ++i) {
            if (i == 1 || i == 2 || (long) i == totalPageNum || (long) i == totalPageNum - 1L || i == this.pageNo - 1 || i == this.pageNo - 2 || i == this.pageNo + 1 || i == this.pageNo + 2) {
                isShow = true;
            }

            if (this.pageNo == i) {
                pager.append("<li class=\"active\"><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\">");
                pager.append(this.pageNo).append("</a></li>");
            } else if (isShow) {
                pager.append("<li><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\" onclick=\"T.jump('").append(this.id).append("','").append(i).append("')\">");
                pager.append(i).append("</a></li>");
            } else {
                if (!isFirstLefttDot && i == this.pageNo - 3) {
                    pager.append("<li class=\"disabled\"><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\">");
                    pager.append("...").append("</a></li>");
                    isFirstLefttDot = true;
                }

                if (!isFirstRightDot && i > this.pageNo) {
                    pager.append("<li class=\"disabled\"><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\">");
                    pager.append("...").append("</a></li>");
                    isFirstRightDot = true;
                }
            }

            isShow = false;
        }

        pager.append("<li");
        if ((long) this.pageNo == this.getTotalPages() || this.getTotalPages() == 0L) {
            pager.append(" class=\"active\"");
        }

        pager.append("><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\"");
        if ((long) this.pageNo != this.getTotalPages() && this.getTotalPages() > 0L) {
            pager.append(" onclick=\"T.jump('").append(this.id).append("','").append(this.pageNo + 1).append("')\"");
        }

        pager.append(">").append(pageDown).append("</a></li>");
        pager.append("<li");
        if ((long) this.pageNo == this.getTotalPages() || this.getTotalPages() == 0L) {
            pager.append(" class=\"active\"");
        }

        pager.append("><a style=\"padding: 0 11px;line-height: 26px;\" href=\"javascript:void(0);\"");
        if ((long) this.pageNo != this.getTotalPages() && this.getTotalPages() > 0L) {
            pager.append(" onclick=\"T.jump('").append(this.id).append("','").append(this.getTotalPages()).append("')\"");
        }

        pager.append(">").append(lastPage).append("</a></li>");
        pager.append("</ul>");
        return pager.toString();
    }
}