diff --git a/server/src/main/java/com/ecep/contract/EntityService.java b/server/src/main/java/com/ecep/contract/EntityService.java index aa9dd10..af23b7d 100644 --- a/server/src/main/java/com/ecep/contract/EntityService.java +++ b/server/src/main/java/com/ecep/contract/EntityService.java @@ -233,29 +233,95 @@ public abstract class EntityService, 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 > Specification 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 clz = (Class) path.getJavaType(); + ObjectMapper objectMapper = SpringApp.getBean(ObjectMapper.class); + X value = objectMapper.convertValue(valueNode, clz); + return cb.lessThan(path.as(clz), value); + }; + } + + private Specification buildGreaterThanSpecification(String field, JsonNode filterNode) { + // TODO 实现大于操作符的逻辑 + throw new UnsupportedOperationException("Unimplemented method 'buildGreaterThanSpecification'"); + } + + private Specification buildNotInSpecification(String field, JsonNode filterNode) { + // TODO 实现 NOT IN 操作符的逻辑 + throw new UnsupportedOperationException("Unimplemented method 'buildNotInSpecification'"); + } + + private Specification buildInSpecification(String field, JsonNode filterNode) { + // TODO 实现 IN 操作符的逻辑 + throw new UnsupportedOperationException("Unimplemented method 'buildInSpecification'"); + } + + private Specification 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); + }; + } + /** * 等于操作符的逻辑 *