176 lines
6.6 KiB
Markdown
176 lines
6.6 KiB
Markdown
# 服务器端 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`方法的功能实现是否与父类相同,相同则移除
|
||
1.8 检查`findById`f方法的返回值类型,须 Vo 类型,如未实现,则添加如下代码
|
||
|
||
```java
|
||
@Override
|
||
public VendorGroupRequireFileTypeVo findById(Integer id) {
|
||
return repository.findById(id).map(VendorGroupRequireFileType::toVo).orElse(null);
|
||
}
|
||
```
|
||
|
||
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 |
|
||
|------|----------------------|
|
||
| CloudRkService | 是 |
|
||
| CloudTycService | 是 |
|
||
| YongYouU8Service | 是 |
|
||
| CompanyBankAccountService | 是 |
|
||
| CompanyBlackReasonService | 是 |
|
||
| CompanyContactService | 是 |
|
||
| CompanyExtendInfoService | 是 |
|
||
| CompanyFileService | 是 |
|
||
| CompanyFileTypeService | 是 |
|
||
| CompanyInvoiceInfoService | 是 |
|
||
| CompanyOldNameService | 是 |
|
||
| CompanyService | 是 |
|
||
| HolidayService | 是 |
|
||
| InvoiceService | 是 |
|
||
| ContractBalanceService | 是 |
|
||
| ContractBidVendorService | 是 |
|
||
| ContractCatalogService | 是 |
|
||
| ContractFileService | 是 |
|
||
| ContractFileTypeService | 是 |
|
||
| ContractGroupService | 是 |
|
||
| ContractInvoiceService | 是 |
|
||
| ContractItemService | 是 |
|
||
| ContractKindService | 是 |
|
||
| ContractPayPlanService | 是 |
|
||
| ContractService | 是 |
|
||
| ContractTypeService | 是 |
|
||
| ExtendVendorInfoService | 是 |
|
||
| PurchaseBillVoucherItemService | 是 |
|
||
| PurchaseBillVoucherService | 是 |
|
||
| PurchaseOrderItemService | 是 |
|
||
| PurchaseOrdersService | 是 |
|
||
| SaleOrdersService | 是 |
|
||
| SalesBillVoucherItemService | 是 |
|
||
| SalesBillVoucherService | 是 |
|
||
| SalesOrderItemService | 是 |
|
||
| CompanyCustomerEntityService | 是 |
|
||
| CompanyCustomerEvaluationFormFileService | 是 |
|
||
| CompanyCustomerFileService | 是 |
|
||
| CompanyCustomerFileTypeService | 是 |
|
||
| CustomerCatalogService | 是 |
|
||
| CustomerFileTypeService | 是 |
|
||
| CustomerService | 是 |
|
||
| BankService | 是 |
|
||
| DepartmentService | 是 |
|
||
| EmployeeAuthBindService | 是 |
|
||
| EmployeeLoginHistoryService | 是 |
|
||
| EmployeeRoleService | 是 |
|
||
| EmployeeService | 是 |
|
||
| FunctionService | 是 |
|
||
| InventoryCatalogService | 是 |
|
||
| InventoryHistoryPriceService | 是 |
|
||
| InventoryService | 是 |
|
||
| PermissionService | 是 |
|
||
| CustomerSatisfactionSurveyService | 是 |
|
||
| DeliverySignMethodService | 是 |
|
||
| ProductTypeService | 是 |
|
||
| ProductUsageService | 是 |
|
||
| ProjectBidService | 是 |
|
||
| ProjectCostItemService | 是 |
|
||
| ProjectCostService | 是 |
|
||
| ProjectFileService | 是 |
|
||
| ProjectFileTypeService | 是 |
|
||
| ProjectFundPlanService | 是 |
|
||
| ProjectIndustryService | 是 |
|
||
| ProjectQuotationService | 是 |
|
||
| ProjectSaleTypeRequireFileTypeService | 是 |
|
||
| ProjectSaleTypeService | 是 |
|
||
| ProjectService | 是 |
|
||
| ProjectTypeService | 是 |
|
||
| VendorApprovedFileService | 是 |
|
||
| VendorApprovedItemService | 是 |
|
||
| VendorApprovedService | 是 |
|
||
| VendorCatalogService | 是 |
|
||
| VendorEntityService | 是 |
|
||
| VendorFileService | 是 |
|
||
| VendorFileTypeService | 是 |
|
||
| VendorGroupRequireFileTypeService | 是 |
|
||
| VendorGroupService | 是 |
|
||
| VendorService | 是 |
|
||
| VendorTypeService | 是 |
|
||
|