package cn.com.capcloud.core.noentitydb;

import cn.com.capcloud.api.action.service.util.TextUtil;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PageTool {
	private Logger logger = LogManager.getLogger("LogFileAppSql");
	// 一页显示的记录数
	private int numPerPage;
	// 记录总数
	private int totalRows;
	// 总页数
	private int totalPages;
	// 当前页码
	private int currentPage;
	// 起始行数
	private int startIndex;
	// 结束行数
	private int lastIndex;

	// 结果集存放List
	private List<Map<String, Object>> resultList;

	/**
	 * 分页构造函数
	 */
	public PageTool(String sql, int currentPage, int numPerPage, JdbcTemplate jTemplate) {
		if (jTemplate == null) {
			throw new IllegalArgumentException("Page.jTemplate is null");
		} else if (sql == null || sql.equals("")) {
			throw new IllegalArgumentException("Page.sql is empty");
		}
		// 设置每页显示记录数
		setNumPerPage(numPerPage);
		// 设置要显示的页数
		setCurrentPage(currentPage);
		// 计算总记录数
		StringBuffer totalSQL = new StringBuffer(" SELECT count(*) as countnum FROM ( ");
		totalSQL.append(sql);
		totalSQL.append(" ) totalTable ");
		// 总记录数
		List<Map<String, Object>> mapList = jTemplate.queryForList(totalSQL.toString());

		setTotalRows(Integer.parseInt(String.valueOf(mapList.get(0).get("countnum"))));
		// 计算总页数
		setTotalPages();
		// 计算起始行数
		setStartIndex();
		System.out.println(this.getStartIndex() + "start");
		// 计算结束行数
		setLastIndex();
		System.out.println(this.getLastIndex() + "last");
		// 使用mysql时直接使用limits
		StringBuffer paginationSQL = new StringBuffer();
		paginationSQL.append(sql);
		paginationSQL.append(" limit " + startIndex + "," + lastIndex);
		// 装入结果集
		System.out.println(paginationSQL);
		setResultList(jTemplate.queryForList(paginationSQL.toString()));

	}

	public PageTool(String sql, int currentPage, int numPerPage, JdbcTemplate jTemplate,String sqlCount) {
		if (jTemplate == null) {
			throw new IllegalArgumentException("Page.jTemplate is null");
		} else if (sql == null || sql.equals("")) {
			throw new IllegalArgumentException("Page.sql is empty");
		}
		// 设置每页显示记录数
		setNumPerPage(numPerPage);
		// 设置要显示的页数
		setCurrentPage(currentPage);
		// 计算总记录数
		StringBuffer totalSQL = new StringBuffer(" SELECT count(*) as countnum FROM ( ");
		totalSQL.append(sqlCount);
		totalSQL.append(" ) totalTable ");
		logger.debug(totalSQL.toString());
		// 总记录数
		List<Map<String, Object>> mapList = jTemplate.queryForList(totalSQL.toString());

		setTotalRows(Integer.parseInt(String.valueOf(mapList.get(0).get("countnum"))));
		// 计算总页数
		setTotalPages();
		// 计算起始行数
		setStartIndex();
		// 计算结束行数
		setLastIndex();
		// 使用mysql时直接使用limits
		StringBuffer paginationSQL = new StringBuffer();
		paginationSQL.append(sql);
		paginationSQL.append(" limit " + startIndex + "," + lastIndex);
		// 装入结果集
		logger.debug(paginationSQL);
		setResultList(jTemplate.queryForList(paginationSQL.toString()));

	}

	public int getCurrentPage() {
		return currentPage;
	}

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

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public List<Map<String, Object>> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map<String, Object>> resultList) {
		this.resultList = JackJSON.parseObject(TextUtil.nullToStrFilter(resultList), ArrayList.class);
	}

	public JtPage getJtPage() {
		JtPage j = new JtPage();
		j.setNumber(currentPage - 1);
		j.setNumberOfElements(this.getResultList().size());
		j.setContent(this.resultList);
		j.setSize(this.numPerPage);
		j.setTotalElements(this.totalRows);
		j.setTotalPages(this.totalPages);
		return j;
	}

	public int getTotalPages() {
		return totalPages;
	}

	// 计算总页数
	public void setTotalPages() {
		if (totalRows % numPerPage == 0) {
			this.totalPages = totalRows / numPerPage;
		} else {
			this.totalPages = (totalRows / numPerPage) + 1;
		}
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex() {
		this.startIndex = (currentPage - 1) * numPerPage;
	}

	public int getLastIndex() {
		return lastIndex;
	}

	// 计算结束时候的索引
	public void setLastIndex() {
		this.lastIndex = numPerPage;
	}
}
