package com.trade.common.core.dto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.trade.common.core.page.GridPage;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;

public class PageMapper<T, M> {
    public GridPage<M> getGridPage(IPage<T> page, Function<T, M> convertFunction) {
        GridPage<M> gridPage = new GridPage<>();

        if (!CollectionUtils.isEmpty(page.getRecords())) {
            gridPage.setRows(page.getRecords().stream().map(convertFunction).collect(Collectors.toList()));
        } else {
            gridPage.setRows(new ArrayList<>());
        }
        gridPage.setRecords((int)page.getTotal());
        gridPage.setPage((int)page.getCurrent());
        gridPage.setPageNum((int)page.getSize());
        gridPage.setTotal((int)page.getPages());
        return gridPage;
    }

}
