package com.zg.common.utils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zg.common.utils.sql.SqlUtil;
import com.zg.framework.web.page.PageDomain;
import com.zg.framework.web.page.TableDataInfo;
import com.zg.framework.web.page.TableSupport;

import java.util.List;

/**
 * 分页工具类
 * 
 * @author zg
 */
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();
    }

    // 封装分页结果
    public static <T> TableDataInfo getDataTable(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list); // 使用 PageInfo 封装分页数据
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(pageInfo.getList());
        tableDataInfo.setTotal(pageInfo.getTotal());
        return tableDataInfo;
    }
}
