package com.ozs.appshow.util;

import java.util.List;
import java.util.Map;

public class PageUtil {
    
    /**
     * 
     * @param page
     *            当前页
     * @param pageSize
     *            每页条数
     * @param totalNumber
     *            总条数
     * @param result
     *            返回数据
     * @param total
     *            合计数据
     * @return
     */
    public static R<List<Map<String, Object>>> getResultAndTotal(Integer page, 
            Integer pageSize, Integer totalNumber,
            List<Map<String, Object>> result, Map<String, Object> total) {
        if(page != 0 && pageSize != 0 && total != null && total.size() != 0) {
            totalNumber++;
        }
        int totalPageCount = 0;
        if(pageSize != 0) {
            totalPageCount = (totalNumber - 1) / pageSize + 1;
        }
        // 当前页为最后一页，加入合计数据
        if (page != 0 && page >= totalPageCount) {
            if (result.size() == pageSize) {
                result.removeAll(result);
            }
            if (total != null && total.size() != 0) {
                result.add(total);
            }
            page = totalPageCount;
        }
        R<List<Map<String, Object>>> r = new R<>(result);
        r.setPage(page);
        r.setPageSize(pageSize);
        r.setTotalNumber(totalNumber);
        r.setTotalPageCount(totalPageCount);
        return r;
    }
}
