//根据id集合查询
List<Long> idList = mainUserDTO.getIdList();
if (null != idList && !idList.isEmpty()) {
Path<Long> idSearchPath = root.get(MainUser_.id);
CriteriaBuilder.In<Long> in = criteriaBuilder.in(idSearchPath);
idList.stream().forEach(id -> in.value(id));
if (null != where) {
where = criteriaBuilder.and(where, criteriaBuilder.and(in));
} else {
where = criteriaBuilder.and(in);
}
}
//排除id集合查询
List<Long> excludeIdList = mainUserDTO.getExcludeIdList();
if (null != excludeIdList && !excludeIdList.isEmpty()) {
Path<Long> idSearchPath = root.get(MainUser_.id);
CriteriaBuilder.In<Long> in = criteriaBuilder.in(idSearchPath);
excludeIdList.stream().forEach(id -> in.value(id));
if (null != where) {
where = criteriaBuilder.and(where, criteriaBuilder.not(in));
} else {
where = criteriaBuilder.not(in);
}
}
//根据多个参数模糊查询
String likeQueryParam = mainUserDTO.getLikeQueryParam();
if (StrUtils.isNotBlank(likeQueryParam)) {
Path<String> userCodeSearchPath = root.get(MainUser_.userCode);
Path<String> phoneSearchPath = root.get(MainUser_.phone);
Path<String> userNameSearchPath = root.get(MainUser_.userName);
if (null != where) {
where = criteriaBuilder.and(where, criteriaBuilder.or(criteriaBuilder.like(userCodeSearchPath, "%" + likeQueryParam + "%"),
criteriaBuilder.or(criteriaBuilder.like(phoneSearchPath, "%" + likeQueryParam + "%")),
criteriaBuilder.or(criteriaBuilder.like(userNameSearchPath, "%" + likeQueryParam + "%"))));
} else {
where = criteriaBuilder.and(criteriaBuilder.like(userCodeSearchPath, "%" + likeQueryParam + "%"),
criteriaBuilder.or(criteriaBuilder.like(phoneSearchPath, "%" + likeQueryParam + "%")),
criteriaBuilder.or(criteriaBuilder.like(userNameSearchPath, "%" + likeQueryParam + "%")));
}
}
//关联表数据查询
String appNameSearch = userAppDTO.getAppName();
if (appNameSearch != null) {
List<Long> appIds = applicationService.findList(ApplicationDTO.builder().appName(appNameSearch).build())
.stream().map(Application::getId).collect(Collectors.toList());
if (Objects.nonNull(appIds) && !appIds.isEmpty()) {
Path<Long> appIdSearchPath = root.get(UserApp_.appId);
CriteriaBuilder.In<Long> in = criteriaBuilder.in(appIdSearchPath);
appIds.forEach(appId -> in.value(appId));
if (null != where) {
where = criteriaBuilder.and(where, criteriaBuilder.and(in));
} else {
where = criteriaBuilder.and(in);
}
}
}