package com.tongran.common.core.utils;

import com.github.pagehelper.PageHelper;
import com.tongran.common.core.utils.sql.SqlUtil;
import com.tongran.common.core.web.page.PageDomain;
import com.tongran.common.core.web.page.TableSupport;

/**
 * 分页工具类
 * 
 * @author tongran
 */
public class PageUtils extends PageHelper
{
    /**
     * 设置请求分页数据
     */
    public static void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }

    /**
     * post传参分页
     * @param pageDomain
     */
    public static void startPage(PageDomain pageDomain)
    {
        if(pageDomain.getPageNum() == null) {
            pageDomain.setPageNum(1);
        }
        if(pageDomain.getPageSize() == null) {
            pageDomain.setPageSize(10);
        }
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }
}
