[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.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -49,19 +50,20 @@ public class AlertDefinesController {
|
|||||||
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
||||||
|
|
||||||
Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
|
Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
|
||||||
Predicate predicate = criteriaBuilder.conjunction();
|
List<Predicate> andList = new ArrayList<>();
|
||||||
if (ids != null && !ids.isEmpty()) {
|
if (ids != null && !ids.isEmpty()) {
|
||||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||||
for (long id : ids) {
|
for (long id : ids) {
|
||||||
inPredicate.value(id);
|
inPredicate.value(id);
|
||||||
}
|
}
|
||||||
predicate = criteriaBuilder.and(inPredicate);
|
andList.add(inPredicate);
|
||||||
}
|
}
|
||||||
if (priority != null) {
|
if (priority != null) {
|
||||||
Predicate predicateApp = criteriaBuilder.equal(root.get("priority"), priority);
|
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
|
||||||
predicate = criteriaBuilder.and(predicateApp);
|
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));
|
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.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -54,31 +55,33 @@ public class AlertsController {
|
|||||||
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
||||||
|
|
||||||
Specification<Alert> specification = (root, query, criteriaBuilder) -> {
|
Specification<Alert> specification = (root, query, criteriaBuilder) -> {
|
||||||
Predicate predicate = criteriaBuilder.conjunction();
|
List<Predicate> andList = new ArrayList<>();
|
||||||
|
|
||||||
if (ids != null && !ids.isEmpty()) {
|
if (ids != null && !ids.isEmpty()) {
|
||||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||||
for (long id : ids) {
|
for (long id : ids) {
|
||||||
inPredicate.value(id);
|
inPredicate.value(id);
|
||||||
}
|
}
|
||||||
predicate = criteriaBuilder.and(inPredicate);
|
andList.add(inPredicate);
|
||||||
}
|
}
|
||||||
if (monitorId != null) {
|
if (monitorId != null) {
|
||||||
Predicate predicateApp = criteriaBuilder.equal(root.get("monitorId"), monitorId);
|
Predicate predicate = criteriaBuilder.equal(root.get("monitorId"), monitorId);
|
||||||
predicate = criteriaBuilder.and(predicateApp);
|
andList.add(predicate);
|
||||||
}
|
}
|
||||||
if (priority != null) {
|
if (priority != null) {
|
||||||
Predicate predicateApp = criteriaBuilder.equal(root.get("priority"), priority);
|
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
|
||||||
predicate = criteriaBuilder.and(predicateApp);
|
andList.add(predicate);
|
||||||
}
|
}
|
||||||
if (status != null) {
|
if (status != null) {
|
||||||
Predicate predicateApp = criteriaBuilder.equal(root.get("status"), status);
|
Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
|
||||||
predicate = criteriaBuilder.and(predicateApp);
|
andList.add(predicate);
|
||||||
}
|
}
|
||||||
if (content != null && !"".equals(content)) {
|
if (content != null && !"".equals(content)) {
|
||||||
Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + 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));
|
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
|
||||||
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
|
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.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -53,33 +54,34 @@ public class MonitorsController {
|
|||||||
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
|
||||||
|
|
||||||
Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
|
Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
|
||||||
Predicate predicate = criteriaBuilder.conjunction();
|
List<Predicate> andList = new ArrayList<>();
|
||||||
if (ids != null && !ids.isEmpty()) {
|
if (ids != null && !ids.isEmpty()) {
|
||||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||||
for (long id : ids) {
|
for (long id : ids) {
|
||||||
inPredicate.value(id);
|
inPredicate.value(id);
|
||||||
}
|
}
|
||||||
predicate = criteriaBuilder.and(inPredicate);
|
andList.add(inPredicate);
|
||||||
}
|
}
|
||||||
if (app != null && !"".equals(app)) {
|
if (app != null && !"".equals(app)) {
|
||||||
Predicate predicateApp = criteriaBuilder.equal(root.get("app"), 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[] andPredicates = new Predicate[andList.size()];
|
||||||
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates));
|
||||||
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
|
|
||||||
predicate = criteriaBuilder.or(predicateName, predicateHost);
|
List<Predicate> orList = new ArrayList<>();
|
||||||
} else {
|
|
||||||
if (host != null && !"".equals(host)) {
|
if (host != null && !"".equals(host)) {
|
||||||
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
|
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
|
||||||
predicate = criteriaBuilder.and(predicateHost);
|
orList.add(predicateHost);
|
||||||
}
|
}
|
||||||
if (name != null && !"".equals(name)) {
|
if (name != null && !"".equals(name)) {
|
||||||
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
||||||
predicate = criteriaBuilder.and(predicateName);
|
orList.add(predicateName);
|
||||||
}
|
}
|
||||||
}
|
Predicate[] orPredicates = new Predicate[orList.size()];
|
||||||
return predicate;
|
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));
|
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
|
||||||
|
|||||||
Reference in New Issue
Block a user