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

love.keeping.starter.web.utils.PageResultUtil Maven / Gradle / Ivy

The newest version!
package love.keeping.starter.web.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import love.keeping.starter.common.utils.BeanUtil;
import love.keeping.starter.common.utils.CollectionUtil;
import love.keeping.starter.common.utils.ObjectUtil;
import love.keeping.starter.web.resp.PageResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
import lombok.NonNull;

/**
 * 分页数据Util
 *
 * @author Keeping
 */
public class PageResultUtil {

  public static  PageResult convert(@NonNull IPage page) {

    return convert(page, null);
  }

  public static  PageResult convert(@NonNull PageInfo pageInfo) {

    return convert(pageInfo, null);
  }

  public static  PageResult convert(@NotNull IPage page,
      Map extra) {
    PageResult pageResult = new PageResult<>();
    List datas = new ArrayList(page.getRecords());
    pageResult.setDatas(datas);
    pageResult.setHasNext(page.getCurrent() > 1);
    pageResult.setHasPrev(page.getCurrent() < page.getPages());
    pageResult.setPageIndex(page.getCurrent());
    pageResult.setPageSize(page.getSize());
    pageResult.setTotalCount(page.getTotal());
    pageResult.setTotalPage((int) page.getPages());
    if (!ObjectUtil.isEmpty(extra)) {
      pageResult.setExtra(extra);
    }

    return pageResult;
  }

  public static  PageResult convert(@NonNull PageInfo pageInfo,
      Map extra) {

    PageResult pageResult = new PageResult<>();
    List datas = new ArrayList(pageInfo.getList());
    pageResult.setDatas(datas);
    pageResult.setHasNext(pageInfo.isHasNextPage());
    pageResult.setHasPrev(pageInfo.isHasPreviousPage());
    pageResult.setPageIndex(pageInfo.getPageNum());
    pageResult.setPageSize(pageInfo.getPageSize());
    pageResult.setTotalCount(pageInfo.getTotal());
    pageResult.setTotalPage(pageInfo.getPages());
    if (!ObjectUtil.isEmpty(extra)) {
      pageResult.setExtra(extra);
    }

    return pageResult;
  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  public static  PageResult rebuild(@SuppressWarnings("rawtypes") PageResult pageResult,
      List datas) {

    PageResult result = new PageResult<>();
    BeanUtil.copyProperties(pageResult, result, "datas");

    result.setDatas(datas == null ? CollectionUtil.emptyList() : datas);

    pageResult.setDatas(datas);

    return result;
  }

  public static  PageResult newInstance(long pageIndex, long pageSize, long totalCount,
      List datas) {

    return newInstance(pageIndex, pageSize, totalCount, datas, null);
  }

  public static  PageResult newInstance(long pageIndex, long pageSize, long totalCount,
      List datas, Map extra) {
    PageResult pageResult = new PageResult<>();
    pageResult.setTotalCount(totalCount);
    pageResult.setPageSize(pageSize);
    pageResult.setPageIndex(pageIndex);
    pageResult.setTotalPage((int) (totalCount / pageSize) + (totalCount % pageSize > 0 ? 1 : 0));
    pageResult.setDatas(datas);
    pageResult.setExtra(extra);
    pageResult.setHasNext(pageResult.getPageIndex() < pageResult.getTotalPage());
    pageResult.setHasPrev(pageResult.getPageIndex() > 1);

    return pageResult;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy