All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yes.tools.page.CommonPage Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.yes.tools.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.data.domain.Page;
import org.yes.tools.utils.BeanCopyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 公共分页对象
 */
@Data
public class CommonPage {

    private Integer page = 1;
    private Integer limit = 20;
    private Integer totalPage = 0;
    private Integer total = 0;
    private List list = new ArrayList<>();

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static  CommonPage restPage(List list) {
        CommonPage result = new CommonPage();
        PageInfo pageInfo = new PageInfo(list);
        result.setTotalPage(pageInfo.getPages());
        result.setPage(pageInfo.getPageNum());
        result.setLimit(pageInfo.getPageSize());
        result.setTotal((int) pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
    public static  CommonPage restPage(Page pageInfo) {
        CommonPage result = new CommonPage();
        result.setTotalPage(pageInfo.getTotalPages());
        result.setPage(pageInfo.getNumber());
        result.setLimit(pageInfo.getSize());
        result.setTotal((int) pageInfo.getTotalElements());
        result.setList(pageInfo.getContent());
        return result;
    }

    /**
     * 将Mybatis-plus分页后的 IPage 转为分页信息
     */
    public static  CommonPage restPage(IPage pageInfo) {
        CommonPage result = new CommonPage();
        result.setTotalPage(Math.toIntExact(pageInfo.getPages()));
        result.setPage(Math.toIntExact(pageInfo.getCurrent()));
        result.setLimit(Math.toIntExact(pageInfo.getSize()));
        result.setTotal((int) pageInfo.getTotal());
        result.setList(pageInfo.getRecords());
        return result;
    }

    public static  void copyIPage(IPage siPage, IPage tiPage, Class tClass) {
        if (siPage == null || tiPage == null) {
            return;
        }
        tiPage.setCurrent(siPage.getCurrent());
        tiPage.setPages(siPage.getPages());
        tiPage.setSize(siPage.getSize());
        tiPage.setTotal(siPage.getTotal());
        List sList = siPage.getRecords();
        List tList = BeanCopyUtils.copyListAs(sList, tClass);
        tiPage.setRecords(tList);
    }

    /**
     * 将PageHelper分页后的 PageInfo 转为分页信息
     *
     * @return
     */
    public static  CommonPage restPage(PageInfo pageInfo) {
        CommonPage result = new CommonPage();
        result.setTotalPage(pageInfo.getPages());
        result.setPage(pageInfo.getPageNum());
        result.setLimit(pageInfo.getPageSize());
        result.setTotal((int) pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 对象A复制对象B的分页信息 // TODO 多次数据查询导致分页数据异常解决办法
     */
    public static  PageInfo copyPageInfo(com.github.pagehelper.Page originPageInfo, List list) {
        PageInfo pageInfo = new PageInfo<>(list);
        BeanCopyUtils.copyProperties(originPageInfo, pageInfo, "list");
        return pageInfo;
    }

    /**
     * 对象A复制对象B的分页信息 //TODO 多次数据查询导致分页数据异常解决办法
     */
    public static  PageInfo copyPageInfo(PageInfo originPageInfo, List list) {
        PageInfo pageInfo = new PageInfo<>(list);
        pageInfo.setPages(originPageInfo.getPages());
        pageInfo.setPageNum(originPageInfo.getPageNum());
        pageInfo.setPageSize(originPageInfo.getPageSize());
        pageInfo.setTotal(originPageInfo.getTotal());
        pageInfo.setList(list);
        return pageInfo;
    }
}