package com.ssyf.plugs.mysql.helper;

import cn.hutool.core.lang.func.Func1;
import com.github.pagehelper.PageInfo;
import com.ssyf.common.core.util.EntityUtil;

import java.util.List;

/**
 * 分页结果帮助类
 * @author huwei
 * @date 2023/1/29 14:45
 */
public class PageEnhanceHelper {

    public static <T ,R> PageInfo<R> switchPageInfoIgnore(PageInfo<T> pageInfo , Class<R> cls , Func1<R ,?>... ignoreFunc1s){
        List<R> list = EntityUtil.copyProperties(pageInfo.getList() ,cls ,ignoreFunc1s);
        PageInfo<R> result = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        return result;
    }

    /**
     * 将分页数据转换为指定类型的数据
     * @param pageInfo
     * @param cls
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T ,R> PageInfo<R> switchPageInfo(PageInfo<T> pageInfo , Class<R> cls){
        return switchPageInfoIgnore(pageInfo, cls);
    }

    /**
     * 获取开始下标
     * @param pageNum
     * @param pageSize
     * @return
     */
    public static long getStartIndex(long pageNum ,long pageSize){
        return (pageNum - 1 ) * pageSize;
    }

}
