package cn.com.countrygarden.bi.main.dto.extension;


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

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

/**
 * @Author Sjiang
 * @Description  为适应前端 修改 currentPage 为 pageIndex
 * @Date 14:57 2019/1/22
 * @Param
 * @return
 **/

public class ExtensionPageData implements Serializable {
    public ExtensionPageData() {
    }
    public ExtensionPageData(Object data) {
        this.data = data;
    }
    public ExtensionPageData(Page page){
        this.pages = page.getPages();
        this.pageIndex = page.getPageNum();
        this.pageSize = page.getPageSize();
        if(this.pages<=this.pageIndex){
            this.endOfPage =true;
        } else {
            this.endOfPage = false;
        }
    }
    public ExtensionPageData(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.pageIndex = currentPage;
            this.pageSize = (int)pageSize;
            this.endOfPage = isEndOfPage;
        }
        this.data = data;
    }

    private int pages=0;
    private int pageIndex =1;
    private int pageSize=0;
    private String AccessToken;
    private boolean endOfPage = false;
    private Object data;

    public String getAccessToken() {
        return AccessToken;
    }

    public void setAccessToken(String accessToken) {
        AccessToken = accessToken;
    }

    public int getPages() {
        return pages;
    }

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

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

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