package com.ruoyi.web.util;

import com.github.pagehelper.Page;

import java.util.List;

public class PageUtil {

    public static <V, R> List<R> getDtoListWithPageable(List<V> list, List<R> dtoList) {
        if (list instanceof Page) {
            Page page = (Page) list;
            Page<R> dtoPage = new Page(page.getPageNum(), page.getPageSize());
            dtoPage.setTotal(page.getTotal());
            dtoPage.addAll(dtoList);
            return dtoPage;
        }
        return dtoList;
    }

    public static <V> List<V> buildListForPage(List<V> list, Integer pageNo, Integer pageSize) {
        Page<V> dtoPage = new Page(pageNo, pageSize);
        dtoPage.setTotal(list.size());
        Integer begin = (pageNo - 1) * pageSize;
        Integer end = Math.min(begin + pageSize, list.size());
        dtoPage.addAll(list.subList(begin, end));
        return dtoPage;
    }
}
