package cn.com.qmth.stmms.biz.common;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class BaseQuery<T> implements Pageable {

    public static final int DEFAULT_PAGE_SIZE = 10;

    private int pageNumber = 1;

    private int pageSize = 10;

    private Sort sort;

    private List<T> result;

    private long currentCount;

    private int totalPage;

    private long totalCount;

    private int first;// 首页索引

    private int last;// 尾页索引

    private int prev;// 上一页索引

    private int next;// 下一页索引

    private boolean firstPage;// 是否是第一页

    private boolean lastPage;// 是否是最后一页

    private int length = 8;// 显示页面长度

    private int slider = 1;// 前后显示页面长度

    private String funcName = "page"; // 设置点击页码调用的js函数名称，默认为page，在一页有多个分页对象时使用。

    private String message = ""; // 设置提示消息，显示在“共n条”之后

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    @Override
    public int getOffset() {
        return (pageNumber - 1) * pageSize;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

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

    @Override
    public int getPageSize() {
        return pageSize;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    @Override
    public Sort getSort() {
        return sort;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public long getCurrentCount() {
        return currentCount;
    }

    public void setCurrentCount(long currentCount) {
        this.currentCount = currentCount;
    }

    /**
     * 初始化参数
     */
    public void initialize() {

        // 1
        this.first = 1;

        this.last = (int) (totalCount / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1);

        if (this.totalCount % this.pageSize != 0 || this.last == 0) {
            this.last++;
        }

        if (this.last < this.first) {
            this.last = this.first;
        }

        if (this.pageNumber <= 1) {
            this.pageNumber = this.first;
            this.firstPage = true;
        }

        if (this.pageNumber >= this.last) {
            this.pageNumber = this.last;
            this.lastPage = true;
        }

        if (this.pageNumber < this.last - 1) {
            this.next = this.pageNumber + 1;
        } else {
            this.next = this.last;
        }

        if (this.pageNumber > 1) {
            this.prev = this.pageNumber - 1;
        } else {
            this.prev = this.first;
        }

        // 2
        if (this.pageNumber < this.first) {// 如果当前页小于首页
            this.pageNumber = this.first;
        }

        if (this.pageNumber > this.last) {// 如果当前页大于尾页
            this.pageNumber = this.last;
        }

    }

    /**
     * 默认输出当前分页标签 <div class="page">${page}</div>
     */
    @Override
    public String toString() {

        initialize();

        StringBuilder sb = new StringBuilder();

        if (pageNumber == first) {// 如果是首页
            sb.append("<li class=\"disabled\"><a href=\"javascript:\">&#171; 上一页</a></li>\n");
        } else {
            sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + prev + "," + pageSize
                    + ");\">&#171; 上一页</a></li>\n");
        }

        int begin = pageNumber - (length / 2);

        if (begin < first) {
            begin = first;
        }

        int end = begin + length - 1;

        if (end >= last) {
            end = last;
            begin = end - length + 1;
            if (begin < first) {
                begin = first;
            }
        }

        if (begin > first) {
            int i = 0;
            for (i = first; i < first + slider && i < begin; i++) {
                sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + i + "," + pageSize + ");\">"
                        + (i + 1 - first) + "</a></li>\n");
            }
            if (i < begin) {
                sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
            }
        }

        for (int i = begin; i <= end; i++) {
            if (i == pageNumber) {
                sb.append("<li class=\"active\"><a href=\"javascript:\">" + (i + 1 - first) + "</a></li>\n");
            } else {
                sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + i + "," + pageSize + ");\">"
                        + (i + 1 - first) + "</a></li>\n");
            }
        }

        if (last - end > slider) {
            sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
            end = last - slider;
        }

        for (int i = end + 1; i <= last; i++) {
            sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + i + "," + pageSize + ");\">"
                    + (i + 1 - first) + "</a></li>\n");
        }

        if (pageNumber == last) {
            sb.append("<li class=\"disabled\"><a href=\"javascript:\">下一页 &#187;</a></li>\n");
        } else {
            sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + next + "," + pageSize + ");\">"
                    + "下一页 &#187;</a></li>\n");
        }

        sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">当前 ");
        sb.append("<input type=\"text\" value=\"" + pageNumber
                + "\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
        sb.append(funcName + "(this.value," + pageSize + ");\" onclick=\"this.select();\"/> / ");
        sb.append("<input type=\"text\" value=\"" + pageSize
                + "\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
        sb.append(funcName + "(" + pageNumber + ",this.value);\" onclick=\"this.select();\"/> 页，");
        sb.append("共 " + totalCount + " 条" + (message != null ? message : "") + "</a><li>\n");

        sb.insert(0, "<ul>\n").append("</ul>\n");

        sb.append("<div style=\"clear:both;\"></div>");

        // sb.insert(0,"<div class=\"page\">\n").append("</div>\n");

        return sb.toString();
    }

    /**
     * 获取分页HTML代码
     * 
     * @return
     */
    public String getHtml() {
        return toString();
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

}
