package com.ho.common.tools.util;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * @描述 分页工具类
 * @创建人 fancl
 * @创建时间 2021/2/6
 * @修改人
 */
@Getter
@Setter
public class PageUtils {


    /**
     * 循环截取某页列表进行分页
     *
     * @param dataList    分页数据
     * @param pageSize    页面大小
     * @param currentPage 当前页面
     */
    public static List page(List dataList, int pageSize, int currentPage) {
        List currentPageList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {
                Object data = dataList.get(currIdx + i);
                currentPageList.add(data);
            }
        }
        return currentPageList;
    }

    /**
     * 将分页信息封装到统一的接口
     *
     * @param pageInfo
     * @return
     */
    public static PageResult getPageResult(PageInfo<?> pageInfo) {
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotalRows(pageInfo.getTotal());
        pageResult.setTotalPages(pageInfo.getPages());
        pageResult.setList(pageInfo.getList());
        return pageResult;
    }

    //使用Java计算分页对象
    public static PageResult getPageResultByJava(List origList, Integer pageNum, Integer pageSize) {
        //空对象处理
        if (CollectionUtil.isEmpty(origList)) {
            return new PageResult(pageNum, pageSize, 0, 0, origList);
        }
        //计算总大小totalSize ,总页数totalPages
        int totalSize = origList.size();
        int totalPages = (totalSize + pageSize - 1) / pageSize;
        //根据pageNum, pageSize对list进行截取
        List list = dealList(origList, pageNum, pageSize);
        return new PageResult(pageNum, pageSize, totalSize, totalPages, list);

    }

    /**
     * 对list进行截取
     *
     * @param list     传入的全量list
     * @param pageNum  页码
     * @param pageSize 每页多少条数据
     * @return
     */
    public static List dealList(List list, Integer pageNum,
                                Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }

        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (!pageNum.equals(pageCount)) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }

        List pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }

}
