package com.zbf.common.core.controller;

import com.zbf.common.constant.HttpStatus;
import com.zbf.common.core.page.PageDomain;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.core.page.TableSupport;

import java.util.List;
import java.util.stream.Collectors;

public class ManualPagination {
    public static TableDataInfo pagination(List<?> list) {
        // 数据手动分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(
                list.stream().skip((long) (pageNum - 1) * pageSize)
                        .limit(pageSize)
                        .collect(Collectors.toList()));
        rspData.setTotal(list.size());
        return rspData;
    }
}