package com.bonus.sgzb.common.core.utils;

import java.util.List;

/**
 * @Author：梁超
 * @date：2024/2/22 - 16:39
 */
public class ListPagingUtil {
    private Integer currentPage;//当前页
    private Integer pageSize;//每页显示记录条数
    private Integer totalPage;//总页数
    private Integer star;//开始数据
    private Integer total;//总条数
    private List<?> rows;//每页显示的数据


    public Integer getCurrentPage() {
        return currentPage;
    }

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

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalPage() {
        return totalPage;
    }

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

    public List<?> getRows() {
        return rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }

    public Integer getStar() {
        return star;
    }

    public void setStar(Integer star) {
        this.star = star;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "ListPagingUtil{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalPage=" + totalPage +
                ", rows=" + rows +
                ", star=" + star +
                ", total=" + total +
                '}';
    }

    public void pageStartInfo(Integer currentPage, Integer pageSize){
        //如果传入的pageNumber为null给pageNumber赋为1
        currentPage = currentPage == null ? 1 : currentPage;
        //如果传入的pageSize为null给pageSize赋为10
        pageSize = pageSize == null ? 10 : pageSize;
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
    }

    public static ListPagingUtil paging(Integer currentPage, Integer pageSize, List<?> list) {
        ListPagingUtil pagingUtil = new ListPagingUtil();
        //初始化
        pagingUtil.pageStartInfo(currentPage, pageSize);
        //设置起始数据
        pagingUtil.setStar((pagingUtil.getCurrentPage()-1)*pagingUtil.getPageSize());
        //设置总数
        pagingUtil.setTotal(list.size());
        //设置总页数
        pagingUtil.setTotalPage(pagingUtil.getTotal() % pagingUtil.getPageSize() == 0 ? pagingUtil.getTotal()/pagingUtil.getPageSize() :pagingUtil.getTotal()/pagingUtil.getPageSize()+1);
        //截取list
        pagingUtil.setRows(list.subList(pagingUtil.getStar(), pagingUtil.getTotal()-pagingUtil.getStar()>pagingUtil.getPageSize()?pagingUtil.getStar()+pagingUtil.getPageSize():pagingUtil.getTotal()));
        return pagingUtil;
    }
}
