From 0c26d329c60d16b97158dd0fc8e74ede09dc3c63 Mon Sep 17 00:00:00 2001 From: songqq Date: Sat, 13 Dec 2025 18:17:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(service):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=B1=BB=E7=BB=A7=E6=89=BFEntityService?= =?UTF-8?q?=E4=BB=A5=E7=BB=9F=E4=B8=80=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将BankService、EmployeeService、CustomerCatalogService、InventoryService和DepartmentService重构为继承EntityService基类,统一实现通用CRUD操作和查询逻辑。移除重复代码,提高代码复用性和可维护性。 --- .../service/CustomerCatalogService.java | 28 ++++++++- .../other/repository/InventoryRepository.java | 7 +-- .../ds/other/service/BankService.java | 26 ++++++++- .../ds/other/service/DepartmentService.java | 57 +++++++++++-------- .../ds/other/service/EmployeeService.java | 27 ++++++++- .../ds/other/service/InventoryService.java | 40 ++++++------- 6 files changed, 134 insertions(+), 51 deletions(-) diff --git a/server/src/main/java/com/ecep/contract/ds/customer/service/CustomerCatalogService.java b/server/src/main/java/com/ecep/contract/ds/customer/service/CustomerCatalogService.java index 389a265..757a077 100644 --- a/server/src/main/java/com/ecep/contract/ds/customer/service/CustomerCatalogService.java +++ b/server/src/main/java/com/ecep/contract/ds/customer/service/CustomerCatalogService.java @@ -1,5 +1,6 @@ package com.ecep.contract.ds.customer.service; +import com.ecep.contract.EntityService; import com.ecep.contract.IEntityService; import com.ecep.contract.QueryService; import com.ecep.contract.constant.ParamConstant; @@ -10,6 +11,7 @@ import com.ecep.contract.service.VoableService; import com.ecep.contract.util.SpecificationUtils; import com.ecep.contract.vo.CustomerCatalogVo; import com.fasterxml.jackson.databind.JsonNode; +import com.ecep.contract.util.SpecificationUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; @@ -30,12 +32,34 @@ import java.util.List; @Lazy @Service @CacheConfig(cacheNames = "customer-catalog") -public class CustomerCatalogService implements IEntityService, QueryService, +public class CustomerCatalogService extends EntityService implements IEntityService, QueryService, VoableService { @Lazy @Autowired private CustomerCatalogRepository repository; + @Override + protected CustomerCatalogRepository getRepository() { + return repository; + } + + @Override + public CustomerCatalog createNewEntity() { + return new CustomerCatalog(); + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildParameterSpecification(JsonNode paramsNode) { + org.springframework.data.jpa.domain.Specification spec = null; + spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name", "description"); + return spec; + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildSearchSpecification(String searchText) { + return getSearchSpecification(searchText); + } + /** * 根据 id 查找 CustomerCatalog */ @@ -166,4 +190,4 @@ public class CustomerCatalogService implements IEntityService, model.setName(vo.getName()); model.setDescription(vo.getDescription()); } -} \ No newline at end of file +} diff --git a/server/src/main/java/com/ecep/contract/ds/other/repository/InventoryRepository.java b/server/src/main/java/com/ecep/contract/ds/other/repository/InventoryRepository.java index 0cc62e9..364b5f8 100644 --- a/server/src/main/java/com/ecep/contract/ds/other/repository/InventoryRepository.java +++ b/server/src/main/java/com/ecep/contract/ds/other/repository/InventoryRepository.java @@ -2,13 +2,12 @@ package com.ecep.contract.ds.other.repository; import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import com.ecep.contract.ds.MyRepository; import org.springframework.stereotype.Repository; import com.ecep.contract.model.Inventory; @Repository -public interface InventoryRepository extends JpaRepository, JpaSpecificationExecutor { +public interface InventoryRepository extends MyRepository { Optional findByCode(String code); -} \ No newline at end of file +} diff --git a/server/src/main/java/com/ecep/contract/ds/other/service/BankService.java b/server/src/main/java/com/ecep/contract/ds/other/service/BankService.java index 521d27f..2239628 100644 --- a/server/src/main/java/com/ecep/contract/ds/other/service/BankService.java +++ b/server/src/main/java/com/ecep/contract/ds/other/service/BankService.java @@ -15,6 +15,7 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import com.ecep.contract.EntityService; import com.ecep.contract.IEntityService; import com.ecep.contract.QueryService; import com.ecep.contract.ds.other.repository.BankRepository; @@ -22,15 +23,38 @@ import com.ecep.contract.model.Bank; import com.ecep.contract.service.VoableService; import com.ecep.contract.vo.BankVo; import com.fasterxml.jackson.databind.JsonNode; +import com.ecep.contract.util.SpecificationUtils; @Lazy @Service @CacheConfig(cacheNames = "bank") -public class BankService implements IEntityService, QueryService, VoableService { +public class BankService extends EntityService implements IEntityService, QueryService, VoableService { @Lazy @Autowired private BankRepository bankRepository; + @Override + protected BankRepository getRepository() { + return bankRepository; + } + + @Override + public Bank createNewEntity() { + return new Bank(); + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildParameterSpecification(JsonNode paramsNode) { + org.springframework.data.jpa.domain.Specification spec = null; + spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name"); + return spec; + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildSearchSpecification(String searchText) { + return getSearchSpecification(searchText); + } + @Cacheable(key = "#id") public BankVo findById(Integer id) { return bankRepository.findById(id).map(Bank::toVo).orElse(null); diff --git a/server/src/main/java/com/ecep/contract/ds/other/service/DepartmentService.java b/server/src/main/java/com/ecep/contract/ds/other/service/DepartmentService.java index 2eb2713..614bf3a 100644 --- a/server/src/main/java/com/ecep/contract/ds/other/service/DepartmentService.java +++ b/server/src/main/java/com/ecep/contract/ds/other/service/DepartmentService.java @@ -14,6 +14,7 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import com.ecep.contract.EntityService; import com.ecep.contract.IEntityService; import com.ecep.contract.QueryService; import com.ecep.contract.SpringApp; @@ -23,6 +24,7 @@ import com.ecep.contract.model.Employee; import com.ecep.contract.service.VoableService; import com.ecep.contract.vo.DepartmentVo; import com.fasterxml.jackson.databind.JsonNode; +import com.ecep.contract.util.SpecificationUtils; /** * 部门 @@ -30,11 +32,41 @@ import com.fasterxml.jackson.databind.JsonNode; @Lazy @Service @CacheConfig(cacheNames = "department") -public class DepartmentService implements IEntityService, QueryService, VoableService { +public class DepartmentService extends EntityService + implements IEntityService, QueryService, VoableService { @Lazy @Autowired private DepartmentRepository repository; + @Override + protected DepartmentRepository getRepository() { + return repository; + } + + @Override + public Department createNewEntity() { + return new Department(); + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildParameterSpecification( + JsonNode paramsNode) { + org.springframework.data.jpa.domain.Specification spec = null; + spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name"); + return spec; + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildSearchSpecification( + String searchText) { + return (root, query, builder) -> { + // 使用或条件组合查询,以实现对员工代码或名称的模糊搜索 + return builder.or( + builder.like(root.get("code"), "%" + searchText + "%"), + builder.like(root.get("name"), "%" + searchText + "%")); + }; + } + @Cacheable(key = "#p0") @Override public DepartmentVo findById(Integer id) { @@ -55,29 +87,6 @@ public class DepartmentService implements IEntityService, QueryServi return repository.findAll(spec, pageable); } - @Override - public Page findAll(JsonNode paramsNode, Pageable pageable) { - Specification spec = null; - if (paramsNode.has("searchText")) { - spec = getSearchSpecification(paramsNode.get("searchText").asText()); - } - // 可以根据需要添加更多参数处理 - return findAll(spec, pageable).map(Department::toVo); - } - - @Override - public Specification getSearchSpecification(String searchText) { - if (!StringUtils.hasText(searchText)) { - return null; - } - return (root, query, builder) -> { - // 使用或条件组合查询,以实现对员工代码或名称的模糊搜索 - return builder.or( - builder.like(root.get("code"), "%" + searchText + "%"), - builder.like(root.get("name"), "%" + searchText + "%")); - }; - } - public List search(String searchText) { Specification spec = getSearchSpecification(searchText); return repository.findAll(spec, Pageable.ofSize(10)).getContent(); diff --git a/server/src/main/java/com/ecep/contract/ds/other/service/EmployeeService.java b/server/src/main/java/com/ecep/contract/ds/other/service/EmployeeService.java index 7c41200..16a9db1 100644 --- a/server/src/main/java/com/ecep/contract/ds/other/service/EmployeeService.java +++ b/server/src/main/java/com/ecep/contract/ds/other/service/EmployeeService.java @@ -17,6 +17,7 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import com.ecep.contract.EntityService; import com.ecep.contract.IEntityService; import com.ecep.contract.QueryService; import com.ecep.contract.SpringApp; @@ -28,6 +29,7 @@ import com.ecep.contract.service.VoableService; import com.ecep.contract.util.SpecificationUtils; import com.ecep.contract.vo.EmployeeVo; import com.fasterxml.jackson.databind.JsonNode; +import com.ecep.contract.util.SpecificationUtils; import jakarta.transaction.Transactional; @@ -37,7 +39,7 @@ import jakarta.transaction.Transactional; @Lazy @Service @CacheConfig(cacheNames = "employee") -public class EmployeeService +public class EmployeeService extends EntityService implements IEntityService, QueryService, VoableService { private static final Logger logger = LoggerFactory.getLogger(EmployeeService.class); public static final int DEFAULT_SYSTEM_EMPLOYEE_ID = 26; @@ -45,6 +47,29 @@ public class EmployeeService @Autowired private EmployeeRepository employeeRepository; + @Override + protected EmployeeRepository getRepository() { + return employeeRepository; + } + + @Override + public Employee createNewEntity() { + return new Employee(); + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildParameterSpecification(JsonNode paramsNode) { + org.springframework.data.jpa.domain.Specification spec = null; + spec = SpecificationUtils.andParam(spec, paramsNode, "department"); + spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "isActive"); + return spec; + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildSearchSpecification(String searchText) { + return getSearchSpecification(searchText); + } + /** * 根据员工ID查找员工信息,并使用缓存机制提高查询效率。 *

diff --git a/server/src/main/java/com/ecep/contract/ds/other/service/InventoryService.java b/server/src/main/java/com/ecep/contract/ds/other/service/InventoryService.java index 8cd9386..4c42207 100644 --- a/server/src/main/java/com/ecep/contract/ds/other/service/InventoryService.java +++ b/server/src/main/java/com/ecep/contract/ds/other/service/InventoryService.java @@ -16,6 +16,7 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import com.ecep.contract.EntityService; import com.ecep.contract.IEntityService; import com.ecep.contract.QueryService; import com.ecep.contract.SpringApp; @@ -36,7 +37,7 @@ import lombok.Setter; @Lazy @Service @CacheConfig(cacheNames = "inventory") -public class InventoryService +public class InventoryService extends EntityService implements IEntityService, QueryService, VoableService { @Lazy @Autowired @@ -45,6 +46,25 @@ public class InventoryService @Setter private int searchPageSize = 10; + @Override + protected InventoryRepository getRepository() { + return inventoryRepository; + } + + @Override + public Inventory createNewEntity() { + return createNewInstance(); + } + + @Override + protected org.springframework.data.jpa.domain.Specification buildParameterSpecification( + JsonNode paramsNode) { + org.springframework.data.jpa.domain.Specification spec = null; + spec = SpecificationUtils.andParam(spec, paramsNode, "catalog", "creator", "updater"); + spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name", "specification"); + return spec; + } + @Cacheable(key = "#p0") public InventoryVo findById(Integer id) { return inventoryRepository.findById(id).map(Inventory::toVo).orElse(null); @@ -59,24 +79,6 @@ public class InventoryService return inventoryRepository.findAll(spec, pageable); } - @Override - public Page findAll(JsonNode paramsNode, Pageable pageable) { - Specification spec = null; - if (paramsNode.has("searchText")) { - spec = getSearchSpecification(paramsNode.get("searchText").asText()); - } - // 可以根据需要添加更多参数处理 - return findAll(spec, pageable).map(Inventory::toVo); - } - - @Override - public Specification getSearchSpecification(String searchText) { - if (!StringUtils.hasText(searchText)) { - return null; - } - return SpecificationUtils.andWith(searchText, this::buildSearchSpecification); - } - protected Specification buildSearchSpecification(String searchText) { return (root, query, builder) -> { Join catalog = root.join("catalog", JoinType.LEFT);