refactor(service): 重构服务类继承EntityService以统一实现

将BankService、EmployeeService、CustomerCatalogService、InventoryService和DepartmentService重构为继承EntityService基类,统一实现通用CRUD操作和查询逻辑。移除重复代码,提高代码复用性和可维护性。
This commit is contained in:
2025-12-13 18:17:53 +08:00
parent e3661630fe
commit 0c26d329c6
6 changed files with 134 additions and 51 deletions

View File

@@ -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<CustomerCatalog>, QueryService<CustomerCatalogVo>,
public class CustomerCatalogService extends EntityService<CustomerCatalog, CustomerCatalogVo, Integer> implements IEntityService<CustomerCatalog>, QueryService<CustomerCatalogVo>,
VoableService<CustomerCatalog, CustomerCatalogVo> {
@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<CustomerCatalog> buildParameterSpecification(JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<CustomerCatalog> spec = null;
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name", "description");
return spec;
}
@Override
protected org.springframework.data.jpa.domain.Specification<CustomerCatalog> buildSearchSpecification(String searchText) {
return getSearchSpecification(searchText);
}
/**
* 根据 id 查找 CustomerCatalog
*/
@@ -166,4 +190,4 @@ public class CustomerCatalogService implements IEntityService<CustomerCatalog>,
model.setName(vo.getName());
model.setDescription(vo.getDescription());
}
}
}

View File

@@ -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<Inventory, Integer>, JpaSpecificationExecutor<Inventory> {
public interface InventoryRepository extends MyRepository<Inventory, Integer> {
Optional<Inventory> findByCode(String code);
}
}

View File

@@ -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<Bank>, QueryService<BankVo>, VoableService<Bank, BankVo> {
public class BankService extends EntityService<Bank, BankVo, Integer> implements IEntityService<Bank>, QueryService<BankVo>, VoableService<Bank, BankVo> {
@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<Bank> buildParameterSpecification(JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<Bank> spec = null;
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name");
return spec;
}
@Override
protected org.springframework.data.jpa.domain.Specification<Bank> buildSearchSpecification(String searchText) {
return getSearchSpecification(searchText);
}
@Cacheable(key = "#id")
public BankVo findById(Integer id) {
return bankRepository.findById(id).map(Bank::toVo).orElse(null);

View File

@@ -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<Department>, QueryService<DepartmentVo>, VoableService<Department, DepartmentVo> {
public class DepartmentService extends EntityService<Department, DepartmentVo, Integer>
implements IEntityService<Department>, QueryService<DepartmentVo>, VoableService<Department, DepartmentVo> {
@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<Department> buildParameterSpecification(
JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<Department> spec = null;
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name");
return spec;
}
@Override
protected org.springframework.data.jpa.domain.Specification<Department> 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<Department>, QueryServi
return repository.findAll(spec, pageable);
}
@Override
public Page<DepartmentVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<Department> spec = null;
if (paramsNode.has("searchText")) {
spec = getSearchSpecification(paramsNode.get("searchText").asText());
}
// 可以根据需要添加更多参数处理
return findAll(spec, pageable).map(Department::toVo);
}
@Override
public Specification<Department> 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<Department> search(String searchText) {
Specification<Department> spec = getSearchSpecification(searchText);
return repository.findAll(spec, Pageable.ofSize(10)).getContent();

View File

@@ -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<Employee, EmployeeVo, Integer>
implements IEntityService<Employee>, QueryService<EmployeeVo>, VoableService<Employee, EmployeeVo> {
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<Employee> buildParameterSpecification(JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<Employee> spec = null;
spec = SpecificationUtils.andParam(spec, paramsNode, "department");
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "isActive");
return spec;
}
@Override
protected org.springframework.data.jpa.domain.Specification<Employee> buildSearchSpecification(String searchText) {
return getSearchSpecification(searchText);
}
/**
* 根据员工ID查找员工信息并使用缓存机制提高查询效率。
* <p>

View File

@@ -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<Inventory, InventoryVo, Integer>
implements IEntityService<Inventory>, QueryService<InventoryVo>, VoableService<Inventory, InventoryVo> {
@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<Inventory> buildParameterSpecification(
JsonNode paramsNode) {
org.springframework.data.jpa.domain.Specification<Inventory> 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<InventoryVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<Inventory> spec = null;
if (paramsNode.has("searchText")) {
spec = getSearchSpecification(paramsNode.get("searchText").asText());
}
// 可以根据需要添加更多参数处理
return findAll(spec, pageable).map(Inventory::toVo);
}
@Override
public Specification<Inventory> getSearchSpecification(String searchText) {
if (!StringUtils.hasText(searchText)) {
return null;
}
return SpecificationUtils.andWith(searchText, this::buildSearchSpecification);
}
protected Specification<Inventory> buildSearchSpecification(String searchText) {
return (root, query, builder) -> {
Join<Object, Object> catalog = root.join("catalog", JoinType.LEFT);