package cn.com.countrygarden.bi.main.util.wcx;

import cn.com.countrygarden.bi.main.dto.wcx.WcxPrizeDto;
import cn.com.countrygarden.bi.main.model.PageData;
import com.github.pagehelper.PageRowBounds;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 */
public class PageUtil {

    /**
     * 获取数据库分页起始页面
     * @author ferren
     * @param pageIndex 页面页码
     * @param pageSize  页面容量
     * @return          用于数据库分页的起始点
     */
    public static int getPageOffset(int pageIndex, int pageSize) {
        return pageSize*(pageIndex-1);
    }

    public static PageData spiltPage(List t, int pageIndex, int pageSize){
        // todo
        if (pageSize == 0) {
            pageSize = 10;
        }
        // 处理数据为空
        if (t == null || t.isEmpty()) {
            PageRowBounds pageRowBounds = new PageRowBounds(pageIndex, pageSize);
            pageRowBounds.setTotal(0L);
            pageRowBounds.setCount(true);
            return new PageData(new ArrayList<WcxPrizeDto>(), pageRowBounds);
        }

        // 假分页
        int total = t.size();

        PageRowBounds pageRowBounds = new PageRowBounds(pageIndex, pageSize);
        pageRowBounds.setTotal((long) total);
        pageRowBounds.setCount(true);
        PageData pageData = null;
        if (!(1 <= pageIndex && pageIndex <= total)) {
            pageData = new PageData(new ArrayList<WcxPrizeDto>(), pageRowBounds);
        } else {
            int formIndex = cn.com.countrygarden.bi.main.util.PageUtil.getPageOffset(pageIndex, pageSize);
            int toIndex = formIndex + pageSize > total ? total : formIndex + pageSize;
            if (formIndex > toIndex) {
                pageData = new PageData(new ArrayList<WcxPrizeDto>(), pageRowBounds);
            } else {
                List subWcxPrizeDtos = t.subList(formIndex, toIndex);
                pageData = new PageData(subWcxPrizeDtos, pageRowBounds);
            }
        }
        return pageData;
    }
}
