From 39dbce013f974a65d8189926a6b64180b25c3138 Mon Sep 17 00:00:00 2001 From: songqq Date: Mon, 22 Sep 2025 23:54:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(converter):=20=E5=AE=9E=E7=8E=B0=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E6=9E=9A=E4=B8=BE=E8=BD=AC=E6=8D=A2=E5=99=A8=E5=92=8C?= =?UTF-8?q?=E4=BE=9B=E5=BA=94=E5=95=86=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加EnumEntityStringConverter作为通用枚举转换基类 实现VendorTypeStringConverter用于供应商类型本地化转换 在VendorTypeService中添加findByLocaleAndValue方法支持转换器 优化ComboBoxUtils的绑定逻辑使其支持可选属性 新增VendorCatalogService提供供应商目录CRUD功能 --- .../contract/controller/ComboBoxUtils.java | 49 ++++++----- .../converter/EnumEntityStringConverter.java | 16 +++- .../converter/VendorTypeStringConverter.java | 22 ++++- .../contract/service/VendorTypeService.java | 34 +++++--- .../vendor/service/VendorCatalogService.java | 83 ++++++++++++++++++- 5 files changed, 167 insertions(+), 37 deletions(-) diff --git a/client/src/main/java/com/ecep/contract/controller/ComboBoxUtils.java b/client/src/main/java/com/ecep/contract/controller/ComboBoxUtils.java index 948e13c..0286850 100644 --- a/client/src/main/java/com/ecep/contract/controller/ComboBoxUtils.java +++ b/client/src/main/java/com/ecep/contract/controller/ComboBoxUtils.java @@ -124,29 +124,34 @@ public class ComboBoxUtils { } list.addAll(queryService.findAll()); comboBox.setItems(list); - // 从ComboBox选择到property的单向绑定 - comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { - property.setValue(newValue != null ? newValue.getId() : null); - }); - // 从property到ComboBox的单向绑定 - property.addListener((observable, oldValue, newValue) -> { - if (newValue == null) { - comboBox.getSelectionModel().clearSelection(); - return; - } - list.stream() - .filter(item -> item != null && newValue.equals(item.getId())) - .findFirst() - .ifPresent(comboBox.getSelectionModel()::select); - }); + EntityStringConverter converter = new EntityStringConverter<>(list); comboBox.setConverter(converter); - // 初始化ComboBox的值 - if (property.getValue() != null) { - list.stream() - .filter(item -> item != null && property.getValue().equals(item.getId())) - .findFirst() - .ifPresent(comboBox.getSelectionModel()::select); + + if (property != null) { + // 从ComboBox选择到property的单向绑定 + comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { + property.setValue(newValue != null ? newValue.getId() : null); + }); + // 从property到ComboBox的单向绑定 + property.addListener((observable, oldValue, newValue) -> { + if (newValue == null) { + comboBox.getSelectionModel().clearSelection(); + return; + } + list.stream() + .filter(item -> item != null && newValue.equals(item.getId())) + .findFirst() + .ifPresent(comboBox.getSelectionModel()::select); + }); + + // 初始化ComboBox的值 + if (property.getValue() != null) { + list.stream() + .filter(item -> item != null && property.getValue().equals(item.getId())) + .findFirst() + .ifPresent(comboBox.getSelectionModel()::select); + } } } @@ -183,7 +188,7 @@ public class ComboBoxUtils { } public static , T extends BaseEnumEntity> void bindComboBox(ComboBox comboBox, - Property property, List dataSet) { + Property property, List dataSet) { property.addListener((observable, oldValue, newValue) -> { dataSet.stream().filter(l -> l.getType() == newValue).findFirst().ifPresent(comboBox::setValue); }); diff --git a/client/src/main/java/com/ecep/contract/converter/EnumEntityStringConverter.java b/client/src/main/java/com/ecep/contract/converter/EnumEntityStringConverter.java index 2f8b932..ce8d9ad 100644 --- a/client/src/main/java/com/ecep/contract/converter/EnumEntityStringConverter.java +++ b/client/src/main/java/com/ecep/contract/converter/EnumEntityStringConverter.java @@ -1,4 +1,18 @@ package com.ecep.contract.converter; -public class EnumEntityStringConverter { +import com.ecep.contract.model.BaseEnumEntity; +import javafx.util.StringConverter; + +public class EnumEntityStringConverter, T extends BaseEnumEntity> extends StringConverter { + + @Override + public String toString(T object) { + return object == null ? "" : object.getValue(); + } + + @Override + public T fromString(String string) { + return null; + // return service.findByName(string); + } } diff --git a/client/src/main/java/com/ecep/contract/converter/VendorTypeStringConverter.java b/client/src/main/java/com/ecep/contract/converter/VendorTypeStringConverter.java index abdc4d8..820866a 100644 --- a/client/src/main/java/com/ecep/contract/converter/VendorTypeStringConverter.java +++ b/client/src/main/java/com/ecep/contract/converter/VendorTypeStringConverter.java @@ -1,4 +1,24 @@ package com.ecep.contract.converter; -public class VendorTypeStringConverter { +import com.ecep.contract.Desktop; +import com.ecep.contract.service.VendorTypeService; +import com.ecep.contract.vo.VendorTypeLocalVo; +import javafx.util.StringConverter; + +public class VendorTypeStringConverter extends StringConverter { + private final VendorTypeService service; + + public VendorTypeStringConverter(VendorTypeService service) { + this.service = service; + } + + @Override + public String toString(VendorTypeLocalVo object) { + return object.getValue(); + } + + @Override + public VendorTypeLocalVo fromString(String string) { + return service.findByLocaleAndValue(Desktop.instance.getActiveEmployee().localeProperty().get(), string); + } } diff --git a/client/src/main/java/com/ecep/contract/service/VendorTypeService.java b/client/src/main/java/com/ecep/contract/service/VendorTypeService.java index c301410..271ac86 100644 --- a/client/src/main/java/com/ecep/contract/service/VendorTypeService.java +++ b/client/src/main/java/com/ecep/contract/service/VendorTypeService.java @@ -1,19 +1,17 @@ package com.ecep.contract.service; -import java.util.List; - -import org.springframework.cache.annotation.CacheConfig; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.CachePut; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.cache.annotation.Caching; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - import com.ecep.contract.VendorType; +import com.ecep.contract.converter.VendorTypeStringConverter; import com.ecep.contract.util.ParamUtils; import com.ecep.contract.vm.VendorTypeLocalViewModel; import com.ecep.contract.vo.VendorTypeLocalVo; +import javafx.util.StringConverter; +import org.springframework.cache.annotation.*; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Locale; @Service @CacheConfig(cacheNames = "vendor-type") @@ -36,15 +34,27 @@ public class VendorTypeService extends QueryService stringConverter = new VendorTypeStringConverter(this); + + @Override + public StringConverter getStringConverter() { + return stringConverter; + } + + public VendorTypeLocalVo findByLocaleAndValue(Locale locale, String string) { + return findAll(ParamUtils.builder().equals("lang", locale.toLanguageTag()).equals("value", string).build(), Pageable.ofSize(1)) + .stream().findFirst().orElse(null); + } } diff --git a/server/src/main/java/com/ecep/contract/ds/vendor/service/VendorCatalogService.java b/server/src/main/java/com/ecep/contract/ds/vendor/service/VendorCatalogService.java index b1c841c..466d3ee 100644 --- a/server/src/main/java/com/ecep/contract/ds/vendor/service/VendorCatalogService.java +++ b/server/src/main/java/com/ecep/contract/ds/vendor/service/VendorCatalogService.java @@ -1,4 +1,85 @@ package com.ecep.contract.ds.vendor.service; -public class VendorCatalogService { +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 com.ecep.contract.IEntityService; +import com.ecep.contract.QueryService; +import com.ecep.contract.constant.ServiceConstant; +import com.ecep.contract.ds.vendor.repository.VendorClassRepository; +import com.ecep.contract.model.VendorCatalog; +import com.ecep.contract.util.SpecificationUtils; +import com.fasterxml.jackson.databind.JsonNode; + +/** + * 供应商目录服务类 + * 提供对供应商目录的查询、创建、更新和删除操作 + */ +@Lazy +@Service +@CacheConfig(cacheNames = "vendor-catalog") +public class VendorCatalogService implements IEntityService, QueryService { + @Lazy + @Autowired + private VendorClassRepository repository; + + @Override + public Page findAll(JsonNode paramsNode, Pageable pageable) { + Specification spec = null; + if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) { + spec = getSpecification(paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText()); + } + + // field + spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "name", "code", "parentId"); + return findAll(spec, pageable); + } + + @Cacheable(key = "#p0") + @Override + public VendorCatalog findById(Integer id) { + return repository.findById(id).orElse(null); + } + + @Override + public Page findAll(Specification spec, Pageable pageable) { + return repository.findAll(spec, pageable); + } + + @Override + public Specification getSpecification(String searchText) { + if (!StringUtils.hasText(searchText)) { + return null; + } + return (root, query, builder) -> { + return builder.or( + builder.like(root.get("name"), "%" + searchText + "%"), + builder.like(root.get("code"), "%" + searchText + "%")); + }; + } + + @Caching(evict = { + @CacheEvict(key = "#p0.id") + }) + @Override + public void delete(VendorCatalog entity) { + repository.delete(entity); + } + + @Caching(evict = { + @CacheEvict(key = "#p0.id") + }) + @Override + public VendorCatalog save(VendorCatalog entity) { + return repository.save(entity); + } }