Files
contract-manager/server/src/main/java/com/ecep/contract/EntityService.java
songqq 42a8f9ab30 refactor(service): 实现VoableService接口以统一VO与实体映射逻辑
refactor(model): 重构实体类与VO类的字段映射关系
style: 调整代码格式与注释
fix: 修复部分字段映射错误
2025-09-26 12:31:08 +08:00

67 lines
2.2 KiB
Java

package com.ecep.contract;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.ds.MyRepository;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;
public abstract class EntityService<T, ID> {
protected abstract MyRepository<T, ID> getRepository();
public abstract T createNewEntity();
public long count() {
return getRepository().count();
}
public long count(Specification<T> spec) {
return getRepository().count(spec);
}
public long count(JsonNode paramsNode) {
return getRepository().count(buildParameterSpecification(paramsNode));
}
protected abstract Specification<T> buildParameterSpecification(JsonNode paramsNode);
public Page<T> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<T> spec = null;
if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) {
spec = getSpecification(paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText());
}
spec = SpecificationUtils.and(spec, buildParameterSpecification(paramsNode));
return findAll(spec, pageable);
}
public Page<T> findAll(Specification<T> spec, Pageable pageable) {
return getRepository().findAll(spec, pageable);
}
public List<T> findAll(Specification<T> spec, Sort sort) {
return getRepository().findAll(spec, sort);
}
protected abstract Specification<T> buildSearchSpecification(String searchText);
public Specification<T> getSpecification(String searchText) {
if (!StringUtils.hasText(searchText)) {
return null;
}
return SpecificationUtils.andWith(searchText, this::buildSearchSpecification);
}
public List<T> search(String searchText) {
Specification<T> spec = getSpecification(searchText);
return getRepository().findAll(spec, Pageable.ofSize(10)).getContent();
}
}