package com.finance.common.basic; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fasterxml.jackson.annotation.JsonIgnore; import com.finance.common.utils.BeanUtils; import com.finance.common.utils.CollUtils; import com.finance.common.utils.Convert; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; @Data @NoArgsConstructor @AllArgsConstructor @ApiModel(description = "分页结果") public class PageDTO { @ApiModelProperty("总条数") protected Long total; @ApiModelProperty("总页码数") protected Long pages; @ApiModelProperty("当前页数据") protected List list; public static PageDTO empty(Long total, Long pages) { return new PageDTO<>(total, pages, CollUtils.emptyList()); } public static PageDTO empty(Page page) { return new PageDTO<>(page.getTotal(), page.getPages(), CollUtils.emptyList()); } public static PageDTO of(Page page) { if(page == null){ return new PageDTO<>(); } if (CollUtils.isEmpty(page.getRecords())) { return empty(page); } return new PageDTO<>(page.getTotal(), page.getPages(), page.getRecords()); } public static PageDTO of(Page page, Function mapper) { if(page == null){ return new PageDTO<>(); } if (CollUtils.isEmpty(page.getRecords())) { return empty(page); } return new PageDTO<>(page.getTotal(), page.getPages(), page.getRecords().stream().map(mapper).collect(Collectors.toList())); } public static PageDTO of(Page page, List list) { return new PageDTO<>(page.getTotal(), page.getPages(), list); } public static PageDTO of(Page page, Class clazz) { return new PageDTO<>(page.getTotal(), page.getPages(), BeanUtils.copyList(page.getRecords(), clazz)); } public static PageDTO of(Page page, Class clazz, Convert convert) { return new PageDTO<>(page.getTotal(), page.getPages(), BeanUtils.copyList(page.getRecords(), clazz, convert)); } @ApiModelProperty(hidden = true) @JsonIgnore public boolean isEmpty(){ return list == null || list.size() == 0; } }