package com.ruoyi.web.v2.v1.utils;

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

public class PaginationUtil {
    
    public static <T> PageResult<T> paginate(List<T> list, int pageNumber, int pageSize) {
        if (list == null || list.isEmpty()) {
            return new PageResult<>(new ArrayList<>(), 0, 0);
        }

        int totalItems = list.size();
        int totalPages = (int) Math.ceil((double) totalItems / pageSize);
        
        // 假设pageNumber从1开始
        if (pageNumber < 1 || pageNumber > totalPages) {
            return new PageResult<>(new ArrayList<>(), totalItems, totalPages);
        }

        int startIndex = (pageNumber - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, totalItems);

        List<T> pagedList = list.subList(startIndex, endIndex);
        
        return new PageResult<>(pagedList, totalItems, totalPages);
    }

    public static class PageResult<T> {
        private List<T> data;
        private int totalItems;
        private int totalPages;

        public PageResult(List<T> data, int totalItems, int totalPages) {
            this.data = data;
            this.totalItems = totalItems;
            this.totalPages = totalPages;
        }

        public List<T> getData() {
            return data;
        }

        public int getTotalItems() {
            return totalItems;
        }

        public int getTotalPages() {
            return totalPages;
        }
    }

}