package com.brilliance.isc.common.taskdistributor.util;

import com.brilliance.isc.common.vo.PageVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.function.Function;

public class PageUtils {

    private PageUtils() {}

    public static <T> PageVo<T> toPageVo(List<T> list) {
        if (list instanceof Page) {
            PageInfo<T> pageInfo = new PageInfo<>(list);
            PageVo<T> pageVo = new PageVo<>();
            pageVo.setTotal((int) pageInfo.getTotal());
            pageVo.setPageNo(pageInfo.getPageNum());
            pageVo.setPageSize(pageInfo.getPageSize());
            pageVo.setRecords(pageInfo.getList());
            return pageVo;
        }
        throw new RuntimeException("不允许接受非Page类型的List作为参数");
    }

    public static <T, R> PageVo<R> toPageVo(List<T> list, Function<List<T>, List<R>> converter) {
        if (list instanceof Page) {
            PageInfo<T> pageInfo = new PageInfo<>(list);
            PageVo<R> pageVo = new PageVo<>();
            pageVo.setTotal((int) pageInfo.getTotal());
            pageVo.setPageNo(pageInfo.getPageNum());
            pageVo.setPageSize(pageInfo.getPageSize());
            pageVo.setRecords(converter.apply(pageInfo.getList()));
            return pageVo;
        }
        throw new RuntimeException("不允许接受非Page类型的List作为参数");
    }

}
