package com.jwipc.page;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.query.BasePageQuery;

import java.util.List;
import java.util.Objects;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
public class PageUtil {
    /**
     * Page 转 PageView，用户easyui分页
     *
     * @param page Mybatis-plus中Page对象
     * @return PageView
     */
    public static PageView getPV(Page page) {
        final PageView pageView = new PageView();
        pageView.setRowCount(page.getTotal());
        pageView.setRows(page.getRecords());
        pageView.setPageNow(page.getCurrent());
        return pageView;
    }


    public static <T> PageView getPV(List<T> record, Long total) {
        final PageView pageView = new PageView();
        pageView.setRowCount(total);
        pageView.setRows(record);
        return pageView;
    }

    public static <T> Page<T> toPage(BasePageQuery query) {
        if (Objects.isNull(query.getPage())) {
            return null;
        }
        return new Page<>(query.getPage(), query.getRows());
    }

    public static <T> PageView getPV(
            List<T> record, int total)
    {
        final PageView pageView = new PageView();
        pageView.setRowCount(total);
        pageView.setRows(record);
        return pageView;
    }

    public static <T> PageView getPV(
            List<T> record)
    {
        final PageView pageView = new PageView();
        pageView.setRowCount(record.size());
        pageView.setRows(record);
        return pageView;
    }
}
