feat(EntityService): 实现 LIKE 和小于操作符的查询逻辑
添加 LIKE 操作符支持包含、开头和结尾匹配模式 实现小于操作符的比较逻辑,支持嵌套属性路径
This commit is contained in:
@@ -233,29 +233,95 @@ public abstract class EntityService<T extends Voable<VO>, VO, ID> {
|
||||
return buildBetweenSpecification(field, filterNode);
|
||||
}
|
||||
if (ParamConstant.KEY_like.equals(operatorStr)) {
|
||||
// TODO 实现 LIKE 操作符的逻辑
|
||||
return null;
|
||||
return buildLikeSpecification(field, filterNode);
|
||||
}
|
||||
if (ParamConstant.KEY_in.equals(operatorStr)) {
|
||||
// TODO 实现 IN 操作符的逻辑
|
||||
return null;
|
||||
return buildInSpecification(field, filterNode);
|
||||
}
|
||||
if (ParamConstant.KEY_notIn.equals(operatorStr)) {
|
||||
// TODO 实现 NOT IN 操作符的逻辑
|
||||
return null;
|
||||
return buildNotInSpecification(field, filterNode);
|
||||
}
|
||||
if (ParamConstant.KEY_greaterThan.equals(operatorStr)) {
|
||||
// TODO 实现大于操作符的逻辑
|
||||
return null;
|
||||
return buildGreaterThanSpecification(field, filterNode);
|
||||
}
|
||||
if (ParamConstant.KEY_lessThan.equals(operatorStr)) {
|
||||
// TODO 实现小于操作符的逻辑
|
||||
return null;
|
||||
return buildLessThanSpecification(field, filterNode);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private <X extends Comparable<? super X>> Specification<T> buildLessThanSpecification(String field,
|
||||
JsonNode filterNode) {
|
||||
JsonNode valueNode = filterNode.get(ParamConstant.KEY_VALUE);
|
||||
if (valueNode == null || valueNode.isNull()) {
|
||||
log.debug("lessThan 操作符需要 value 字段");
|
||||
return null;
|
||||
}
|
||||
return (root, query, cb) -> {
|
||||
// 支持嵌套属性路径,如 company.id
|
||||
String[] fieldPath = field.split("\\.");
|
||||
Path<?> path = root;
|
||||
for (String segment : fieldPath) {
|
||||
path = path.get(segment);
|
||||
}
|
||||
Class<X> clz = (Class<X>) path.getJavaType();
|
||||
ObjectMapper objectMapper = SpringApp.getBean(ObjectMapper.class);
|
||||
X value = objectMapper.convertValue(valueNode, clz);
|
||||
return cb.lessThan(path.as(clz), value);
|
||||
};
|
||||
}
|
||||
|
||||
private Specification<T> buildGreaterThanSpecification(String field, JsonNode filterNode) {
|
||||
// TODO 实现大于操作符的逻辑
|
||||
throw new UnsupportedOperationException("Unimplemented method 'buildGreaterThanSpecification'");
|
||||
}
|
||||
|
||||
private Specification<T> buildNotInSpecification(String field, JsonNode filterNode) {
|
||||
// TODO 实现 NOT IN 操作符的逻辑
|
||||
throw new UnsupportedOperationException("Unimplemented method 'buildNotInSpecification'");
|
||||
}
|
||||
|
||||
private Specification<T> buildInSpecification(String field, JsonNode filterNode) {
|
||||
// TODO 实现 IN 操作符的逻辑
|
||||
throw new UnsupportedOperationException("Unimplemented method 'buildInSpecification'");
|
||||
}
|
||||
|
||||
private Specification<T> buildLikeSpecification(String field, JsonNode filterNode) {
|
||||
JsonNode valueNode = filterNode.get(ParamConstant.KEY_VALUE);
|
||||
if (valueNode == null || !valueNode.isTextual()) {
|
||||
log.debug("LIKE 操作符需要 value 为字符串");
|
||||
return null;
|
||||
}
|
||||
|
||||
ParamConstant.Mode mode = ParamConstant.Mode.CONTAINS;
|
||||
if (filterNode.has(ParamConstant.KEY_MODE)) {
|
||||
mode = ParamConstant.Mode.valueOf(filterNode.get(ParamConstant.KEY_MODE).asText());
|
||||
}
|
||||
|
||||
String likeValue;
|
||||
switch (mode) {
|
||||
case STARTS_WITH:
|
||||
likeValue = valueNode.asText() + "%";
|
||||
break;
|
||||
case ENDS_WITH:
|
||||
likeValue = "%" + valueNode.asText();
|
||||
break;
|
||||
default:
|
||||
likeValue = "%" + valueNode.asText() + "%";
|
||||
break;
|
||||
}
|
||||
return (root, query, cb) -> {
|
||||
// 支持 company.name 这种嵌套属性路径
|
||||
String[] fieldPath = field.split("\\.");
|
||||
Path<?> path = root;
|
||||
for (String segment : fieldPath) {
|
||||
path = path.get(segment);
|
||||
}
|
||||
return cb.like(path.as(String.class), likeValue);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 等于操作符的逻辑
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user