top.doudou.common.tool.datebase.jpa.PageUtil Maven / Gradle / Ivy
package top.doudou.common.tool.datebase.jpa;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import top.doudou.base.page.PageInfo;
/**
* @Description 分页工具类
* @Author 傻男人 <[email protected]>
* @Date 2020-10-09 16:20
* @Version V1.0
*/
public class PageUtil {
/**
* 封装PageRequest
* @param pageInfo
* @return
*/
public static PageRequest getPageRequest(PageInfo pageInfo){
return PageRequest.of((pageInfo.getPageIndex() - 1), pageInfo.getPageSize());
}
/**
*
* @param pageInfo
* @param desc 排序规则 false 正序 true 倒序
* @param properties 排序的字段
* @return
*/
public static PageRequest getPageRequest(PageInfo pageInfo,boolean desc,String... properties){
if(!desc){
return PageRequest.of((pageInfo.getPageIndex() - 1), pageInfo.getPageSize(), Sort.by(Sort.Direction.ASC, properties));
}
return PageRequest.of((pageInfo.getPageIndex() - 1), pageInfo.getPageSize(), Sort.by(Sort.Direction.DESC, properties));
}
// public static Specification getSpecification(ConditionQueryDto conditionQueryDto){
// return (root, criteriaQuery, criteriaBuilder) -> {
// List list = Lists.newArrayList();
// if (StringUtils.isNotBlank(conditionQueryDto.getQueryType())) {
// List orList = Lists.newArrayList();
// if(StrategyStateEnum.OVERDUE.getCode().equals(conditionQueryDto.getQueryType())){
// orList.add(criteriaBuilder.equal(root.get("orderType").as(String.class),StrategyStateEnum.OVERDUE.getCode()));
// }
// orList.add(criteriaBuilder.equal(root.get("orderType").as(String.class),StrategyStateEnum.WITHHOLD.getCode()));
// orList.add(criteriaBuilder.equal(root.get("orderType").as(String.class),StrategyStateEnum.FIRST_WITHHOLD.getCode()));
// list.add(criteriaBuilder.or(orList.toArray(new Predicate[orList.size()])));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getNameOrPhone())) {
// List orList = Lists.newArrayList();
// orList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + conditionQueryDto.getNameOrPhone() + "%"));
// orList.add(criteriaBuilder.like(root.get("phone").as(String.class), "%" + conditionQueryDto.getNameOrPhone() + "%"));
// list.add(criteriaBuilder.or(orList.toArray(new Predicate[orList.size()])));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getOrderNo())) {
// list.add(criteriaBuilder.equal(root.get("orderNo").as(String.class), conditionQueryDto.getOrderNo()));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getOverdueStage())) {
// list.add(criteriaBuilder.equal(root.get("overdueStage").as(String.class), conditionQueryDto.getOverdueStage()));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getStrategyState())) {
// CollectionStateEnum.getCollectionStateEnum(conditionQueryDto.getStrategyState());
// list.add(criteriaBuilder.equal(root.get("strategyState").as(String.class), conditionQueryDto.getStrategyState()));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getMoneyBackState())) {
// MoneyBackEnum.getMoneyBackEnum(conditionQueryDto.getMoneyBackState());
// list.add(criteriaBuilder.equal(root.get("repaymentState").as(String.class), conditionQueryDto.getMoneyBackState()));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getBeginTime())) {
// list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createDate").as(String.class), conditionQueryDto.getBeginTime()));
// }
// if (StringUtils.isNotBlank(conditionQueryDto.getEndTime())) {
// list.add(criteriaBuilder.lessThanOrEqualTo(root.get("createDate").as(String.class), conditionQueryDto.getEndTime()));
// }
// criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
// criteriaQuery.where(list.toArray(new Predicate[list.size()])).distinct(true);
// criteriaQuery.orderBy(criteriaBuilder.desc(root.get("periodsIndex")));
// criteriaQuery.groupBy(root.get("orderId"));
// return criteriaQuery.getRestriction();
//// criteriaBuilder.and(list.toArray(new Predicate[list.size()]))
// };
// }
}