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

import com.github.pagehelper.Page;
import com.github.pagehelper.PageRowBounds;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class PageDataCkCleaner implements Serializable {

    public PageDataCkCleaner() {
    }

    public PageDataCkCleaner(Page page){
        this.pages = page.getPages();
        this.currentPage = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.total=page.getTotal();
        if(this.pages<=this.currentPage){
            this.endOfPage =true;
        } else {
            this.endOfPage = false;
        }

    }

    public PageDataCkCleaner(Object data, PageRowBounds pageRowBounds) {
        if(pageRowBounds!=null){
            long total = pageRowBounds.getTotal();
            long pageNum = pageRowBounds.getOffset();
            long pageSize = pageRowBounds.getLimit();
            int pages = 0;
            int currentPage = (int)pageNum;
            if(total > 0 && pageSize>0){
                BigDecimal totalSize = new BigDecimal(Long.toString(total));
                BigDecimal pageSizeDc = new BigDecimal(Long.toString(pageSize));
                BigDecimal pagesDc = totalSize.divide(pageSizeDc,0, RoundingMode.UP);
                pages = pagesDc.intValue();
            }
            boolean isEndOfPage = false;
            if(pages<=currentPage){
                isEndOfPage = true;
            }

            this.pages = pages;
            this.currentPage = currentPage;
            this.pageSize = (int)pageSize;
            this.endOfPage = isEndOfPage;
        }
        this.data = data;
    }

    private int pages=0;
    //当前页码
    private int currentPage =1;
    private int pageSize=0;
    private boolean endOfPage = false;
    private Long total;
    private Object data;

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public boolean isEndOfPage() {
        return endOfPage;
    }

    public void setEndOfPage(boolean endOfPage) {
        this.endOfPage = endOfPage;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}
