refactor(vendor): 重构服务类继承EntityService基类

将多个vendor相关的服务类重构为继承自EntityService基类,统一实现通用CRUD操作
移除重复代码,简化各服务类的实现
添加必要的重写方法如getRepository和createNewEntity
This commit is contained in:
2025-12-13 18:54:15 +08:00
parent 0c26d329c6
commit 9dc90507cb
6 changed files with 115 additions and 86 deletions

View File

@@ -1,19 +1,13 @@
package com.ecep.contract.ds.vendor.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import com.ecep.contract.ds.MyRepository;
import com.ecep.contract.model.VendorApproved;
/**
* 合格供方名录
*/
@Repository
public interface VendorApprovedRepository extends
JpaRepository<VendorApproved, Integer>, JpaSpecificationExecutor<VendorApproved>
{
public interface VendorApprovedRepository extends MyRepository<VendorApproved, Integer> {
}

View File

@@ -1,6 +1,7 @@
package com.ecep.contract.ds.vendor.service;
import com.ecep.contract.IEntityService;
import com.ecep.contract.EntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.constant.ParamConstant;
@@ -26,12 +27,22 @@ import java.util.List;
@Lazy
@Service
@CacheConfig(cacheNames = "vendor-approved-file")
public class VendorApprovedFileService
public class VendorApprovedFileService extends EntityService<VendorApprovedFile, VendorApprovedFileVo, Integer>
implements IEntityService<VendorApprovedFile>, QueryService<VendorApprovedFileVo>, VoableService<VendorApprovedFile, VendorApprovedFileVo> {
@Lazy
@Autowired
private VendorApprovedFileRepository repository;
@Override
protected VendorApprovedFileRepository getRepository() {
return repository;
}
@Override
public VendorApprovedFile createNewEntity() {
return new VendorApprovedFile();
}
/**
* 根据ID查找供应商已批准文件
*
@@ -44,41 +55,29 @@ public class VendorApprovedFileService
}
@Override
public Page<VendorApprovedFileVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<VendorApprovedFile> spec = null;
if (paramsNode.has(ParamConstant.KEY_SEARCH_TEXT)) {
spec = getSearchSpecification(paramsNode.get(ParamConstant.KEY_SEARCH_TEXT).asText());
}
spec = SpecificationUtils.andParam(spec, paramsNode, "list");
return findAll(spec, pageable).map(VendorApprovedFile::toVo);
protected org.springframework.data.jpa.domain.Specification<VendorApprovedFile> buildSearchSpecification(String searchText) {
return (root, query, builder) -> {
return builder.or(
builder.like(root.get("fileName"), "%" + searchText + "%"),
builder.like(root.get("description"), "%" + searchText + "%"));
};
}
@Override
public long count(JsonNode paramsNode) {
return repository.count();
protected org.springframework.data.jpa.domain.Specification<VendorApprovedFile> buildParameterSpecification(JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<VendorApprovedFile> spec = null;
spec = SpecificationUtils.andParam(spec, paramsNode, "list");
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "fileName", "description");
return spec;
}
@Override
public VendorApprovedFile getById(Integer id) {
return repository.findById(id).orElse(null);
}
/**
* 获取供应商已批准文件的查询规格
* 根据搜索文本构建文件名和描述的模糊查询条件
*
* @param searchText 搜索文本
* @return 构建的查询规格
*/
@Override
public Specification<VendorApprovedFile> getSearchSpecification(String searchText) {
return (root, query, builder) -> {
return builder.or(
builder.like(root.get("fileName"), "%" + searchText + "%"),
builder.like(root.get("description"), "%" + searchText + "%"));
};
}
/**
* 根据查询规格和分页参数获取供应商已批准文件列表
@@ -159,4 +158,4 @@ public class VendorApprovedFileService
entity.setList(null);
}
}
}
}

View File

@@ -24,6 +24,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.ecep.contract.IEntityService;
import com.ecep.contract.EntityService;
import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.QueryService;
import com.ecep.contract.constant.CompanyVendorConstant;
@@ -40,7 +41,8 @@ import com.fasterxml.jackson.databind.JsonNode;
@Lazy
@Service
public class VendorApprovedService implements IEntityService<VendorApproved>, QueryService<VendorApprovedVo>,
public class VendorApprovedService extends EntityService<VendorApproved, VendorApprovedVo, Integer>
implements IEntityService<VendorApproved>, QueryService<VendorApprovedVo>,
VoableService<VendorApproved, VendorApprovedVo> {
private static final Logger logger = LoggerFactory.getLogger(VendorApprovedService.class);
@Lazy
@@ -57,6 +59,16 @@ public class VendorApprovedService implements IEntityService<VendorApproved>, Qu
@Autowired
private SysConfService confService;
@Override
protected VendorApprovedRepository getRepository() {
return repository;
}
@Override
public VendorApproved createNewEntity() {
return new VendorApproved();
}
public File getBasePath() {
return new File(confService.getString(CompanyVendorConstant.KEY_APPROVED_LIST_BASE_PATH));
}
@@ -72,26 +84,15 @@ public class VendorApprovedService implements IEntityService<VendorApproved>, Qu
}
@Override
public Page<VendorApproved> findAll(Specification<VendorApproved> spec, Pageable pageable) {
return repository.findAll(spec, pageable);
}
@Override
public Page<VendorApprovedVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<VendorApproved> spec = null;
if (paramsNode.has(ParamConstant.KEY_SEARCH_TEXT)) {
spec = getSearchSpecification(paramsNode.get(ParamConstant.KEY_SEARCH_TEXT).asText());
}
// 添加额外的参数过滤
protected org.springframework.data.jpa.domain.Specification<VendorApproved> buildParameterSpecification(
JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<VendorApproved> spec = null;
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "title");
return findAll(spec, pageable).map(VendorApproved::toVo);
return spec;
}
@Override
public Specification<VendorApproved> getSearchSpecification(String searchText) {
if (!StringUtils.hasText(searchText)) {
return null;
}
protected Specification<VendorApproved> buildSearchSpecification(String searchText) {
return (root, query, builder) -> {
return builder.like(root.get("title"), "%" + searchText + "%");
};

View File

@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.ecep.contract.IEntityService;
import com.ecep.contract.EntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.ds.vendor.repository.VendorClassRepository;
import com.ecep.contract.model.VendorCatalog;
@@ -30,12 +31,23 @@ import com.fasterxml.jackson.databind.JsonNode;
@Lazy
@Service
@CacheConfig(cacheNames = "vendor-catalog")
public class VendorCatalogService implements IEntityService<VendorCatalog>, QueryService<VendorCatalogVo>,
public class VendorCatalogService extends EntityService<VendorCatalog, VendorCatalogVo, Integer>
implements IEntityService<VendorCatalog>, QueryService<VendorCatalogVo>,
VoableService<VendorCatalog, VendorCatalogVo> {
@Lazy
@Autowired
private VendorClassRepository repository;
@Override
protected VendorClassRepository getRepository() {
return repository;
}
@Override
public VendorCatalog createNewEntity() {
return new VendorCatalog();
}
@Override
public Page<VendorCatalogVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<VendorCatalog> spec = null;
@@ -65,10 +77,15 @@ public class VendorCatalogService implements IEntityService<VendorCatalog>, Quer
}
@Override
public Specification<VendorCatalog> getSearchSpecification(String searchText) {
if (!StringUtils.hasText(searchText)) {
return null;
}
protected org.springframework.data.jpa.domain.Specification<VendorCatalog> buildParameterSpecification(
JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<VendorCatalog> spec = null;
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "name", "code", "parentId");
return spec;
}
@Override
protected Specification<VendorCatalog> buildSearchSpecification(String searchText) {
return (root, query, builder) -> {
return builder.or(
builder.like(root.get("name"), "%" + searchText + "%"),

View File

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.ecep.contract.IEntityService;
import com.ecep.contract.EntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.other.service.EmployeeService;
@@ -29,12 +30,22 @@ import com.fasterxml.jackson.databind.JsonNode;
@Lazy
@Service
@CacheConfig(cacheNames = "company-vendor-entity")
public class VendorEntityService implements IEntityService<VendorEntity>, QueryService<VendorEntityVo>,
public class VendorEntityService extends EntityService<VendorEntity, VendorEntityVo, Integer> implements IEntityService<VendorEntity>, QueryService<VendorEntityVo>,
VoableService<VendorEntity, VendorEntityVo> {
@Lazy
@Autowired
private VendorEntityRepository repository;
@Override
protected VendorEntityRepository getRepository() {
return repository;
}
@Override
public VendorEntity createNewEntity() {
return new VendorEntity();
}
@Override
public VendorEntity getById(Integer id) {
return repository.findById(id).orElse(null);
@@ -74,6 +85,14 @@ public class VendorEntityService implements IEntityService<VendorEntity>, QueryS
};
}
@Override
protected org.springframework.data.jpa.domain.Specification<VendorEntity> buildParameterSpecification(JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<VendorEntity> spec = null;
spec = SpecificationUtils.andParam(spec, paramsNode, "vendor");
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "name", "abbName", "code");
return spec;
}
@Override
public List<VendorEntity> search(String searchText) {
Specification<VendorEntity> spec = getSearchSpecification(searchText);
@@ -154,4 +173,4 @@ public class VendorEntityService implements IEntityService<VendorEntity>, QueryS
model.setModifier(employeeService.getById(vo.getModifierId()));
}
}
}
}

View File

@@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.ecep.contract.IEntityService;
import com.ecep.contract.EntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.VendorType;
import com.ecep.contract.ds.vendor.repository.VendorTypeLocalRepository;
@@ -30,27 +31,44 @@ import com.fasterxml.jackson.databind.JsonNode;
@Lazy
@Service
@CacheConfig(cacheNames = "vendor-type")
public class VendorTypeService implements IEntityService<VendorTypeLocal>, QueryService<VendorTypeLocalVo>,
public class VendorTypeService extends EntityService<VendorTypeLocal, VendorTypeLocalVo, Integer>
implements IEntityService<VendorTypeLocal>, QueryService<VendorTypeLocalVo>,
VoableService<VendorTypeLocal, VendorTypeLocalVo> {
@Lazy
@Autowired
private VendorTypeLocalRepository repository;
@Override
public Page<VendorTypeLocalVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<VendorTypeLocal> spec = null;
if (paramsNode.has(ParamConstant.KEY_SEARCH_TEXT)) {
spec = getSearchSpecification(paramsNode.get(ParamConstant.KEY_SEARCH_TEXT).asText());
}
protected VendorTypeLocalRepository getRepository() {
return repository;
}
@Override
public VendorTypeLocal createNewEntity() {
return new VendorTypeLocal();
}
@Override
protected Specification<VendorTypeLocal> buildParameterSpecification(JsonNode paramsNode) {
Specification<VendorTypeLocal> spec = null;
if (paramsNode.has("type")) {
spec = SpecificationUtils.and(spec, (root, query, builder) -> builder.equal(root.get("type"),
VendorType.valueOf(paramsNode.get("type").asText())));
}
// field
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "lang", "value");
return findAll(spec, pageable).map(VendorTypeLocal::toVo);
return spec;
}
@Override
protected Specification<VendorTypeLocal> buildSearchSpecification(String searchText) {
return (root, query, builder) -> {
return
// builder.or(
builder.like(root.get("type"), "%" + searchText + "%")
// )
;
};
}
@Cacheable(key = "#p0")
@@ -64,30 +82,11 @@ public class VendorTypeService implements IEntityService<VendorTypeLocal>, Query
return repository.findById(id).orElse(null);
}
@Override
public Page<VendorTypeLocal> findAll(Specification<VendorTypeLocal> spec, Pageable pageable) {
return repository.findAll(spec, pageable);
}
@Cacheable(key = "'all-'+#p0.toLanguageTag()")
public Map<VendorType, VendorTypeLocal> findAll(Locale locale) {
return repository.getCompleteMapByLocal(locale.toLanguageTag());
}
@Override
public Specification<VendorTypeLocal> getSearchSpecification(String searchText) {
if (!StringUtils.hasText(searchText)) {
return null;
}
return (root, query, builder) -> {
return
// builder.or(
builder.like(root.get("type"), "%" + searchText + "%")
// )
;
};
}
@Caching(evict = {
@CacheEvict(key = "#p0.id"),
@CacheEvict(key = "'all-'+#p0.getLang()")