package com.zksy.common.utils.page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import java.util.List;
import java.util.concurrent.Callable;

import com.zksy.common.utils.bean.PageResult;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Intercepts({@Signature(
    type = Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
    type = Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class PageManager extends PageInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(PageManager.class);
    public static final String __PARANAMER_DATA = "getPage java.util.List list \ngetPage int,int,java.util.concurrent.Callable pageNo,pageSize,call \nsetPageInfo int,int pageNo,pageSize \n";

    public PageManager() {
    }

    public static void setPageInfo(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
    }

    public static PageResult getPage(List<?> list) {
        PageResult var3;
        try {
            PageInfo info = new PageInfo(list);
            PageResult page = new PageResult(info.getPageNum(), info.getPageSize());
            page.setResult(list);
            page.setTotalPage(info.getPages());
            page.setTotalSize(info.getTotal());
            var3 = page;
        } finally {
            PageHelper.clearPage();
        }

        return var3;
    }

    public static PageResult getPage(int pageNo, int pageSize, Callable<List> call) {
        setPageInfo(pageNo, pageSize);

        try {
            return getPage((List)call.call());
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }
}
