package com.fjhx.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

public class PageUtil {

    /**
     * 获取页数
     */
    public static int getCurrent(Map<String, ?> condition) {
        Object currentObj = condition.get("pageNum");
        return ObjectUtil.isEmpty(currentObj) ? 1 : Integer.parseInt(currentObj.toString());
    }

    /**
     * 获取每页条数
     */
    public static int getSize(Map<String, ?> condition) {
        Object sizeObj = condition.get("pageSize");
        return ObjectUtil.isEmpty(sizeObj) ? 10 : Integer.parseInt(sizeObj.toString());
    }

    /**
     * 获取分页
     */
    public static Page<Map<String, Object>> createPageMap(Map<String, ?> condition) {
        return new Page<>(getCurrent(condition), getSize(condition));
    }

    public static <T> Page<T> createPage(Map<String, ?> condition) {
        return new Page<>(getCurrent(condition), getSize(condition));
    }

    public static <T> Page<T> copyPage(IPage<?> page, Class<T> cls) {
        Page<T> result = new Page<>();
        result.setRecords(BeanUtil.copyToList(page.getRecords(), cls));
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        result.setPages(page.getPages());
        return result;
    }

}
