package com.ssm.util.page;

public class Helper{

	/**
	 * 设置分页参数初始化
	 */
	public static void setPageInfo(Integer pageNo,Integer pageSize){
		pageNo = pageNo == null?1:pageNo;  
	    pageSize = pageSize == null?10:pageSize;
	}

	public static String formatHtml(long total,long pageNo,String url) {
		        if(total <= 1) {
		            return "";
		        } else {
		            int prePage = (int) (pageNo - 1);
		            if(prePage <= 0) {
		                prePage = 1;
		            }

		            int nextPage = (int) (pageNo + 1);
		            if(nextPage > total) {
		                nextPage = (int) total;
		            }

		           /* String null = "";
		            if(this.hasStie) {
		                null = "#specialsite";
		            }*/

		            String html = "<div class=\'page\'>";
		            if(total == 1) {
		                html = html + "<a href=\'javascript:;\' class=\'current\'>< 上一页</a>";
		            } else {
		                html = html + "<a href=\'" + url + prePage + null + "\'>< 上一页</a>";
		            }

		            int i;
		            if(total > 7) {
		                if(pageNo <= 4) {
		                    for(i = 1; i <= 5; ++i) {
		                        if(pageNo == i) {
		                            html = html + " <span class=\'current\'>" + i + null + "</span>";
		                        } else {
		                            html = html + "<a href=\'" + url + i + null + "\'>" + i + "</a>";
		                        }
		                    }

		                    html = html + "<span>...</span>";
		                    html = html + " <a href=\'" + url + total + null + "\'>" + total + "</a>";
		                } else if(pageNo >= total - 3) {
		                    html = html + " <a href=\'" + url + "1" + null + "\'>1</a>";
		                    html = html + "<span>...</span>";

		                    for(i = (int) (total - 4); i <= total; ++i) {
		                        if(pageNo == i) {
		                            html = html + " <span class=\'current\'>" + i + "</span>";
		                        } else {
		                            html = html + "<a href=\'" + url + i + null + "\'>" + i + "</a>";
		                        }
		                    }
		                } else {
		                    html = html + " <a href=\'" + url + "1" + null + "\'>1</a>";
		                    html = html + "<span>...</span>";

		                    for(i = (int) (pageNo - 2); i <= pageNo + 2; ++i) {
		                        if(pageNo == i) {
		                            html = html + " <span class=\'current\'>" + i + "</span>";
		                        } else {
		                            html = html + "<a href=\'" + url + i + null + "\'>" + i + "</a>";
		                        }
		                    }

		                    html = html + "<span>...</span>";
		                    html = html + " <a href=\'" + url + total + null + "\'>" + total + "</a>";
		                }
		            } else {
		                for(i = 1; i <= total; ++i) {
		                    if(pageNo == i) {
		                        html = html + " <span class=\'current\'>" + i + "</span>";
		                    } else {
		                        html = html + "<a href=\'" + url + i + null + "\'>" + i + "</a>";
		                    }
		                }
		            }

		            if(pageNo != total) {
		                html = html + "<a href=\'" + url+"?nextPage="+ nextPage + /*null + */"\'>下一页 ></a>";
		            }

		         //   html = html + "共" + total + "页 到第<input class=\'page_text\' type=\'text\' url=\'" + url + null + "\' name=\'pageJump\' id=\'pageJump\' page=\'" + total + "\' />页<input type=\'button\' class=\'page_btn\' value=\'确定\' id=\'btnJump\'></a>";
		            html = html + "</div>";
		            return html;
		        }
	}
	
	public static void countStartPageAndEndPage(PagedResult<?> pagedResult){
		long totalCount = pagedResult.getTotal();//总条目数量
		long pageSize = pagedResult.getPageSize();
		long totalPage = pagedResult.getPages();
		long page = pagedResult.getPageNo();
		long startPage = pagedResult.getStartPage();
		long endPage = pagedResult.getEndPage();
		if(totalCount%pageSize==0){
			totalPage = totalCount/pageSize;
		}else{
			totalPage = totalCount/pageSize+1;
		}
		if((page-2)>0){
			startPage = page-2;
		}else if((page-1>0)){
			startPage = page-1;
		}else{
			startPage = page;
		}
		if(totalPage<=5){
			endPage = totalPage;
			startPage = 1;
		}else{
			endPage = startPage+4;
			if(endPage>totalPage){//最大页码不能超过总页码
				endPage = totalPage;
				startPage = totalPage -4;
			}
		}
		pagedResult.setStartPage(startPage);
		pagedResult.setEndPage(endPage);
	}
}

	