package com.fdkankan.db.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Hb_zzZ on 2021/2/18.
 */
public class PageInfoUtils {

    public static <T> Page<T> list2PageInfo(List<T> arrayList, Integer pageNum, Integer pageSize) {
        //实现list分页
        int pageStart = pageNum == 1 ? 0 : (pageNum - 1) * pageSize;
        int pageEnd = arrayList.size() < pageSize * pageNum ? arrayList.size() : pageSize * pageNum;
        List<T> pageResult = new LinkedList<T>();
        if (arrayList.size() > pageStart) {
            pageResult = arrayList.subList(pageStart, pageEnd);
        }
        Page<T> pageInfo = new Page<T>();
        pageInfo.setRecords(pageResult);
        //获取PageInfo其他参数
        pageInfo.setTotal(arrayList.size());
        int pages = arrayList.size() % pageSize == 0 ? arrayList.size() / pageSize : (arrayList.size() / pageSize) + 1;
        pageInfo.setPages(pages);
        pageInfo.setCurrent(pageNum);
        pageInfo.setSize(pageSize);
//        pageInfo.setPages(pages);
        return pageInfo;
    }
}
