package com.hzs.retail.member.utils;

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

public class MemoryPageUtils {
    public static <T> PageResult<T> paginate(List<T> dataList, int pageNum, int pageSize) {
        // 校验参数有效性
        if (pageNum <= 0) pageNum = 1;
        if (pageSize <= 0) pageSize = 10; // 默认每页10条

        int total = dataList.size();
        int start = (pageNum - 1) * pageSize;
        if (start >= total) {
            return new PageResult<>(total, new ArrayList<>());
        }

        // 计算结束索引（避免超出列表长度）
        int end = Math.min(start + pageSize, total);
        List<T> pageData = dataList.subList(start, end);

        return new PageResult<>(total, pageData);
    }
}
