refactor(service): 重构多个服务类继承EntityService基类
重构多个服务类使其继承EntityService基类,统一实现基础CRUD操作 移除重复的findAll、getById等方法实现 添加缓存注解确保一致性 更新服务类文档说明继承关系
This commit is contained in:
@@ -1,4 +1,86 @@
|
||||
# 服务器端继承IEntityService接口的Service列表
|
||||
# 服务器端 Service 继承IEntityService接口任务
|
||||
|
||||
`IEntityService` 接口定义了若干基本 CRUD 操作(相对项目路径:src/main/java/com/contract/service/IEntityService.java),包括保存、删除、查询等方法。
|
||||
|
||||
## Service 继承 EntityService 类的任务
|
||||
|
||||
参考 `server\src\main\java\com\ecep\contract\cloud\rk\CloudRkService.java` 类
|
||||
|
||||
1. 实现 `server\src\main\java\com\ecep\contract\EntityService.java` 类的方法
|
||||
1.1 实现 `getRepository` 方法,用于获取实体的Repository
|
||||
1.1.1 确认的Repository是否为`server\src\main\java\com\ecep\contract\ds\MyRepository.java`的子接口
|
||||
1.2 实现 `createNewEntity` 方法,用于创建新实体
|
||||
1.3 实现 `buildSearchSpecification` 方法,根据搜索参数构建查询规范
|
||||
1.3.1 方法内容如下
|
||||
|
||||
```java
|
||||
@Override
|
||||
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 + "%"));
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
1.4 实现 `buildParameterSpecification` 方法,根据参数构建查询规范
|
||||
1.4.1 方法内容从 `findAll(JsonNode paramsNode, Pageable pageable)` 中提取,然后删除 findAll 方法
|
||||
比如 findAll 方法中定义如下
|
||||
```java
|
||||
@Override
|
||||
public Page<VendorApprovedItemVo> findAll(JsonNode paramsNode, Pageable pageable) {
|
||||
Specification<VendorApprovedItem> spec = null;
|
||||
if (paramsNode.has(ParamConstant.KEY_SEARCH_TEXT)) {
|
||||
spec = getSearchSpecification(paramsNode.get(ParamConstant.KEY_SEARCH_TEXT).asText());
|
||||
}
|
||||
// 添加额外的参数过滤
|
||||
if (paramsNode.has("type")) {
|
||||
String typeText = paramsNode.get("type").asText();
|
||||
VendorType type = VendorType.valueOf(typeText);
|
||||
spec = SpecificationUtils.and(spec, (root, query, builder) -> {
|
||||
return builder.equal(root.get("type"), type);
|
||||
});
|
||||
}
|
||||
spec = SpecificationUtils.andParam(spec, paramsNode, "vendor", "list");
|
||||
return findAll(spec, pageable).map(VendorApprovedItem::toVo);
|
||||
}
|
||||
```
|
||||
|
||||
则 `buildParameterSpecification` 方法的实现如下(删除 searchText 部分以及直接返回spec,不调用findAll方法)
|
||||
```java
|
||||
|
||||
@Override
|
||||
protected Specification<VendorApprovedItem> buildParameterSpecification(JsonNode paramsNode) {
|
||||
Specification<VendorApprovedItem> spec = null;
|
||||
// 添加额外的参数过滤
|
||||
if (paramsNode.has("type")) {
|
||||
String typeText = paramsNode.get("type").asText();
|
||||
VendorType type = VendorType.valueOf(typeText);
|
||||
spec = SpecificationUtils.and(spec, (root, query, builder) -> {
|
||||
return builder.equal(root.get("type"), type);
|
||||
});
|
||||
}
|
||||
spec = SpecificationUtils.andParam(spec, paramsNode, "vendor", "list");
|
||||
return spec;
|
||||
}
|
||||
```
|
||||
|
||||
1.5 检查`count`方法的功能实现是否与父类相同,相同则移除
|
||||
1.6 检查`findAll`方法的功能实现是否与父类相同,相同则移除
|
||||
1.7 检查`getById`方法的功能实现是否与父类相同,相同则移除
|
||||
|
||||
2. 缓存相关任务
|
||||
2.1 确认Service类是否有 @CacheConfig 注解,没有注解,则没有应用缓存功能,跳过后续缓存检查
|
||||
2.2 检查 `findById` 方法,注解 @Cacheable(key = "#p0")
|
||||
2.3 检查 `save` 方法,注解 @CacheEvict(key = "#p0.id") 或者 @Caching(evict = @CacheEvict(key = "#p0.id"))
|
||||
2.4 检查 `delete` 方法,注解 @CacheEvict(key = "#p0.id") 或者 @Caching(evict = @CacheEvict(key = "#p0.id"))
|
||||
|
||||
|
||||
3. 处理异常情况
|
||||
|
||||
|
||||
## Service 继承 IEntityService 接口的列表
|
||||
|
||||
| 类名 | 是否继承EntityService |
|
||||
|------|----------------------|
|
||||
@@ -65,13 +147,13 @@
|
||||
| ProjectFileService | 是 |
|
||||
| ProjectFileTypeService | 是 |
|
||||
| ProjectFundPlanService | 是 |
|
||||
| ProjectIndustryService | 否 |
|
||||
| ProjectQuotationService | 否 |
|
||||
| ProjectSaleTypeRequireFileTypeService | 否 |
|
||||
| ProjectSaleTypeService | 否 |
|
||||
| ProjectIndustryService | 是 |
|
||||
| ProjectQuotationService | 是 |
|
||||
| ProjectSaleTypeRequireFileTypeService | 是 |
|
||||
| ProjectSaleTypeService | 是 |
|
||||
| ProjectService | 是 |
|
||||
| ProjectTypeService | 否 |
|
||||
| VendorApprovedFileService | 否 |
|
||||
| ProjectTypeService | 是 |
|
||||
| VendorApprovedFileService | 是 |
|
||||
| VendorApprovedItemService | 否 |
|
||||
| VendorApprovedService | 否 |
|
||||
| VendorCatalogService | 否 |
|
||||
@@ -83,21 +165,3 @@
|
||||
| VendorService | 否 |
|
||||
| VendorTypeService | 否 |
|
||||
|
||||
## 分析结果
|
||||
|
||||
- 共找到81个实现了IEntityService接口的服务类
|
||||
- 其中有12个类同时继承了EntityService类
|
||||
- 大多数服务类(69个)仅实现了IEntityService接口,没有继承EntityService类
|
||||
|
||||
继承EntityService类的服务类包括:
|
||||
- CloudRkService
|
||||
- CloudTycService
|
||||
- YongYouU8Service
|
||||
- CompanyBankAccountService
|
||||
- CompanyService
|
||||
- ContractService
|
||||
- SaleOrdersService
|
||||
- SalesBillVoucherService
|
||||
- SalesOrderItemService
|
||||
- ProjectService
|
||||
- 以及其他几个核心业务服务类
|
||||
Reference in New Issue
Block a user