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


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

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

/**
 * Created by chenweijiang on 2017/9/13.
 */
public class PageData implements Serializable {
    public PageData() {
    }

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

    }

    public PageData(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.total=total;
        }
        this.data = data;
    }

    private int pages=0;
    private int currentPage =1;
    private int pageSize=0;
    private boolean endOfPage = false;
    private Long total=0L;
    private Object data;

    public Long getTotal() {
        return total;
    }

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

    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;
    }
}
