package org.wfc.common.core.utils;

import com.github.pagehelper.PageHelper;
import org.wfc.common.core.constant.HttpStatus;
import org.wfc.common.core.utils.sql.SqlUtil;
import org.wfc.common.core.web.page.PageDomain;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.common.core.web.page.TableSupport;

import java.util.List;

/**
 * 分页工具类
 *
 * @author wfc
 */
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 TableDataInfo getDataTable(List<?> list, Long total) {
        TableDataInfo rspData = new TableDataInfo(list, total);
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("Query successful");

        return rspData;
    }
}
