cn.woodwhales.common.model.vo.PageRespVO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of woodwhales-common Show documentation
Show all versions of woodwhales-common Show documentation
https://github.com/woodwhales
package cn.woodwhales.common.model.vo;
import cn.woodwhales.common.model.enums.RespCodeEnum;
import cn.woodwhales.common.model.result.BaseRespResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
/**
* 分页查询响应视图
* @author woodwhales on 2020-08-25
*
*/
@Data
public class PageRespVO extends RespVO> {
/**
* 总记录数
*/
private Long count;
public static PageRespVO success(Long count, List data) {
return buildPageRespVO(RespCodeEnum.SUCCESS, count, data);
}
public static PageRespVO success(List data) {
return buildPageRespVO(RespCodeEnum.SUCCESS, Long.parseLong(data.size() + ""), data);
}
public static PageRespVO empty() {
return buildPageRespVO(RespCodeEnum.SUCCESS, 0L, null);
}
public static PageRespVO error(BaseRespResult baseRespResult) {
return buildPageRespVO(baseRespResult, 0L, null);
}
public static PageRespVO buildPageRespVO(IPage page) {
return buildPageRespVO(RespCodeEnum.SUCCESS, page.getTotal(), page.getRecords());
}
/**
* IPage 数据按照 mapper 规则转成 PageRespVO
* @param page page
* @param mapper mapper
* @param 原始数据泛型
* @param 目标数据泛型
* @return PageRespVO
*/
public static PageRespVO buildPageRespVO(IPage page,
Function super S, ? extends T> mapper) {
if(CollectionUtils.isEmpty(page.getRecords())) {
return buildPageRespVO(RespCodeEnum.SUCCESS, page.getTotal(), emptyList());
}
return buildPageRespVO(RespCodeEnum.SUCCESS, page.getTotal(), page.getRecords()
.stream()
.map(mapper)
.collect(toList()));
}
public static PageRespVO buildPageRespVO(IPage page,
Function super S, ? extends T> mapper,
Comparator comparator) {
if(CollectionUtils.isEmpty(page.getRecords())) {
return buildPageRespVO(RespCodeEnum.SUCCESS, page.getTotal(), emptyList());
}
return buildPageRespVO(RespCodeEnum.SUCCESS, page.getTotal(), page.getRecords()
.stream()
.map(mapper)
.sorted(comparator)
.collect(toList()));
}
public static PageRespVO buildPageRespVO(@NotNull BaseRespResult baseRespResult,
Long count,
List data) {
Objects.requireNonNull(baseRespResult, "baseRespResult不允许为空");
return build(baseRespResult.getCode(), baseRespResult.getMessage(), count, data);
}
private static PageRespVO build(Integer code,
String message,
Long count,
List data) {
Objects.requireNonNull(code, "code不允许为空");
PageRespVO pageRespVO = new PageRespVO<>();
pageRespVO.setCode(code);
pageRespVO.setMsg(message);
if(CollectionUtils.isEmpty(data)) {
pageRespVO.setCount(0L);
pageRespVO.setData(emptyList());
} else {
pageRespVO.setCount(count);
pageRespVO.setData(data);
}
return pageRespVO;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy