feat(service): 实现国际化支持并优化Service层
重构文件类型相关Service以支持国际化查询 添加findOneByLang辅助方法统一查询逻辑 实现StringConverter支持UI控件显示 优化缓存配置和查询性能 新增UnitStringConverter和CustomerCatalogStringConverter 完善文档和测试用例
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ecep.contract.ds.customer.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.ecep.contract.ds.MyRepository;
|
||||
import org.springframework.data.repository.ListCrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@@ -9,8 +10,6 @@ import org.springframework.stereotype.Repository;
|
||||
import com.ecep.contract.model.CustomerCatalog;
|
||||
|
||||
@Repository
|
||||
public interface CustomerCatalogRepository extends
|
||||
// JDBC interfaces
|
||||
ListCrudRepository<CustomerCatalog, Integer>, PagingAndSortingRepository<CustomerCatalog, Integer> {
|
||||
public interface CustomerCatalogRepository extends MyRepository<CustomerCatalog, Integer> {
|
||||
Optional<CustomerCatalog> findByCode(String code);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package com.ecep.contract.ds.customer.service;
|
||||
|
||||
import com.ecep.contract.IEntityService;
|
||||
import com.ecep.contract.QueryService;
|
||||
import com.ecep.contract.constant.ServiceConstant;
|
||||
import com.ecep.contract.ds.customer.repository.CustomerCatalogRepository;
|
||||
import com.ecep.contract.model.CustomerCatalog;
|
||||
import com.ecep.contract.util.SpecificationUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 客户分类服务
|
||||
*/
|
||||
@Lazy
|
||||
@Service
|
||||
@CacheConfig(cacheNames = "customer-catalog")
|
||||
public class CustomerCatalogService implements IEntityService<CustomerCatalog>, QueryService<CustomerCatalog> {
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CustomerCatalogRepository repository;
|
||||
|
||||
/**
|
||||
* 根据 id 查找 CustomerCatalog
|
||||
*/
|
||||
@Cacheable(key = "#p0")
|
||||
@Override
|
||||
public CustomerCatalog findById(Integer id) {
|
||||
return repository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据 code 查找 CustomerCatalog
|
||||
*/
|
||||
@Cacheable(key = "'code-'+#p0")
|
||||
public CustomerCatalog findByCode(String code) {
|
||||
if (!StringUtils.hasText(code)) {
|
||||
return null;
|
||||
}
|
||||
return repository.findByCode(code).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 name 查找 CustomerCatalog
|
||||
*/
|
||||
@Cacheable(key = "'name-'+#p0")
|
||||
public CustomerCatalog findByName(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
return null;
|
||||
}
|
||||
Specification<CustomerCatalog> spec = (root, query, builder) -> {
|
||||
return builder.equal(root.get("name"), name);
|
||||
};
|
||||
List<CustomerCatalog> list = repository.findAll(spec);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找所有 CustomerCatalog
|
||||
*/
|
||||
@Cacheable(key = "'all'")
|
||||
public List<CustomerCatalog> findAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 CustomerCatalog
|
||||
*/
|
||||
@Caching(evict = {
|
||||
@CacheEvict(key = "#p0.id"),
|
||||
@CacheEvict(key = "'code-'+#p0.code"),
|
||||
@CacheEvict(key = "'name-'+#p0.name"),
|
||||
@CacheEvict(key = "'all'")
|
||||
})
|
||||
@Override
|
||||
public CustomerCatalog save(CustomerCatalog catalog) {
|
||||
return repository.save(catalog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 CustomerCatalog
|
||||
*/
|
||||
@Caching(evict = {
|
||||
@CacheEvict(key = "#p0.id"),
|
||||
@CacheEvict(key = "'code-'+#p0.code"),
|
||||
@CacheEvict(key = "'name-'+#p0.name"),
|
||||
@CacheEvict(key = "'all'")
|
||||
})
|
||||
@Override
|
||||
public void delete(CustomerCatalog catalog) {
|
||||
repository.delete(catalog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询 CustomerCatalog
|
||||
*/
|
||||
@Override
|
||||
public Page<CustomerCatalog> findAll(JsonNode paramsNode, Pageable pageable) {
|
||||
Specification<CustomerCatalog> spec = null;
|
||||
if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) {
|
||||
spec = getSpecification(paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText());
|
||||
}
|
||||
|
||||
// 字段等值查询
|
||||
spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name", "description");
|
||||
return repository.findAll(spec, pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据搜索文本创建查询条件
|
||||
*/
|
||||
public Specification<CustomerCatalog> getSpecification(String searchText) {
|
||||
if (!StringUtils.hasText(searchText)) {
|
||||
return null;
|
||||
}
|
||||
String likeText = "%" + searchText + "%";
|
||||
return (root, query, builder) -> {
|
||||
return builder.or(
|
||||
builder.like(root.get("code"), likeText),
|
||||
builder.like(root.get("name"), likeText),
|
||||
builder.like(root.get("description"), likeText)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索 CustomerCatalog
|
||||
*/
|
||||
public List<CustomerCatalog> search(String searchText) {
|
||||
return repository.findAll(getSpecification(searchText), Pageable.ofSize(10)).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<CustomerCatalog> findAll(Specification<CustomerCatalog> spec, Pageable pageable) {
|
||||
return repository.findAll(spec, pageable);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user