package com.iamberry.wechat.service;

import com.github.pagehelper.Page;
import com.iamberry.wechat.core.entity.page.PagedResult;

import java.util.List;

/**
 * 分页工具类
 * @author 献
 * @company 深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @tel 18271840547
 * @date 2017/5/18
 */
public class PageUtil {

    /**
     * 根据 List 获取分页数据
     * @param datas
     * @param <T>
     * @return
     */
    public static <T> PagedResult<T> getPage(List<T> datas) {
        PagedResult<T> result = new PagedResult<T>();
        if (datas instanceof Page) {
            Page page = (Page) datas;
            result.setPageNO(page.getPageNum());
            result.setPageSize(page.getPageSize());
            result.setDataList(page.getResult());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
        }
        else {
            result.setPageNO(1);
            result.setPageSize(datas.size());
            result.setDataList(datas);
            result.setTotal(datas.size());
        }
        return result;
    }

    /**
     * 根据Page 对象分页
     * @param page
     * @param <T>
     * @return
     */
    public static <T> PagedResult<T> getPage(Page page) {
        PagedResult<T> result = new PagedResult<T>();
        result.setPageNO(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setDataList(page.getResult());
        result.setTotal(page.getTotal());
        result.setPages(page.getPages());

        return result;
    }

}
