[alert,manager] bugfix-多条件查询有条件未生效问题
This commit is contained in:
@@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
@@ -49,19 +50,20 @@ public class AlertDefinesController {
|
||||
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
||||
|
||||
Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
|
||||
Predicate predicate = criteriaBuilder.conjunction();
|
||||
List<Predicate> andList = new ArrayList<>();
|
||||
if (ids != null && !ids.isEmpty()) {
|
||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||
for (long id : ids) {
|
||||
inPredicate.value(id);
|
||||
}
|
||||
predicate = criteriaBuilder.and(inPredicate);
|
||||
andList.add(inPredicate);
|
||||
}
|
||||
if (priority != null) {
|
||||
Predicate predicateApp = criteriaBuilder.equal(root.get("priority"), priority);
|
||||
predicate = criteriaBuilder.and(predicateApp);
|
||||
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
|
||||
andList.add(predicate);
|
||||
}
|
||||
return predicate;
|
||||
Predicate[] predicates = new Predicate[andList.size()];
|
||||
return criteriaBuilder.and(andList.toArray(predicates));
|
||||
};
|
||||
// 分页是必须的
|
||||
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
@@ -54,31 +55,33 @@ public class AlertsController {
|
||||
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
||||
|
||||
Specification<Alert> specification = (root, query, criteriaBuilder) -> {
|
||||
Predicate predicate = criteriaBuilder.conjunction();
|
||||
List<Predicate> andList = new ArrayList<>();
|
||||
|
||||
if (ids != null && !ids.isEmpty()) {
|
||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||
for (long id : ids) {
|
||||
inPredicate.value(id);
|
||||
}
|
||||
predicate = criteriaBuilder.and(inPredicate);
|
||||
andList.add(inPredicate);
|
||||
}
|
||||
if (monitorId != null) {
|
||||
Predicate predicateApp = criteriaBuilder.equal(root.get("monitorId"), monitorId);
|
||||
predicate = criteriaBuilder.and(predicateApp);
|
||||
Predicate predicate = criteriaBuilder.equal(root.get("monitorId"), monitorId);
|
||||
andList.add(predicate);
|
||||
}
|
||||
if (priority != null) {
|
||||
Predicate predicateApp = criteriaBuilder.equal(root.get("priority"), priority);
|
||||
predicate = criteriaBuilder.and(predicateApp);
|
||||
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
|
||||
andList.add(predicate);
|
||||
}
|
||||
if (status != null) {
|
||||
Predicate predicateApp = criteriaBuilder.equal(root.get("status"), status);
|
||||
predicate = criteriaBuilder.and(predicateApp);
|
||||
Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
|
||||
andList.add(predicate);
|
||||
}
|
||||
if (content != null && !"".equals(content)) {
|
||||
Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%");
|
||||
predicate = criteriaBuilder.and(predicateContent);
|
||||
andList.add(predicateContent);
|
||||
}
|
||||
return predicate;
|
||||
Predicate[] predicates = new Predicate[andList.size()];
|
||||
return criteriaBuilder.and(andList.toArray(predicates));
|
||||
};
|
||||
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
|
||||
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
@@ -53,33 +54,34 @@ public class MonitorsController {
|
||||
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
||||
|
||||
Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
|
||||
Predicate predicate = criteriaBuilder.conjunction();
|
||||
List<Predicate> andList = new ArrayList<>();
|
||||
if (ids != null && !ids.isEmpty()) {
|
||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||
for (long id : ids) {
|
||||
inPredicate.value(id);
|
||||
}
|
||||
predicate = criteriaBuilder.and(inPredicate);
|
||||
andList.add(inPredicate);
|
||||
}
|
||||
if (app != null && !"".equals(app)) {
|
||||
Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app);
|
||||
predicate = criteriaBuilder.and(predicateApp);
|
||||
andList.add(predicateApp);
|
||||
}
|
||||
if (name != null && !"".equals(name) && host != null && !"".equals(host)) {
|
||||
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
||||
Predicate[] andPredicates = new Predicate[andList.size()];
|
||||
Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates));
|
||||
|
||||
List<Predicate> orList = new ArrayList<>();
|
||||
if (host != null && !"".equals(host)) {
|
||||
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
|
||||
predicate = criteriaBuilder.or(predicateName, predicateHost);
|
||||
} else {
|
||||
if (host != null && !"".equals(host)) {
|
||||
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
|
||||
predicate = criteriaBuilder.and(predicateHost);
|
||||
}
|
||||
if (name != null && !"".equals(name)) {
|
||||
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
||||
predicate = criteriaBuilder.and(predicateName);
|
||||
}
|
||||
orList.add(predicateHost);
|
||||
}
|
||||
return predicate;
|
||||
if (name != null && !"".equals(name)) {
|
||||
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
||||
orList.add(predicateName);
|
||||
}
|
||||
Predicate[] orPredicates = new Predicate[orList.size()];
|
||||
Predicate orPredicate = criteriaBuilder.and(orList.toArray(orPredicates));
|
||||
|
||||
return query.where(andPredicate,orPredicate).getRestriction();
|
||||
};
|
||||
// 分页是必须的
|
||||
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
|
||||
|
||||
Reference in New Issue
Block a user