From cf73769ef2bdc166bad72642a5dd8cde39e877dc Mon Sep 17 00:00:00 2001 From: songqq Date: Thu, 28 Aug 2025 15:55:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=85=8D=E7=BD=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E9=85=8D=E7=BD=AE=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E4=B8=8E=E4=BF=9D=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将配置绑定逻辑抽象到StringConfig类中,统一处理配置的保存与控件绑定 提取配置键常量到对应服务类中,提高代码可维护性 使用LocalDateTime替代Instant处理时间字段 简化SysConfRepository继承结构,使用自定义基础仓库 优化SysConfWindowController的配置管理逻辑,减少重复代码 --- .../u8/YongYouU8ManagerWindowController.java | 3 - .../manager/cloud/u8/YongYouU8Service.java | 5 + .../manager/cloud/u8/ctx/CompanyCtx.java | 22 +-- .../service/CompanyCustomerService.java | 63 +++++---- .../ds/other/AbstractConfigBounder.java | 4 + .../manager/ds/other/StringConfig.java | 20 ++- .../controller/SysConfWindowController.java | 132 ++++++++---------- .../other/repository/SysConfRepository.java | 80 +---------- .../ds/other/service/SysConfService.java | 6 +- 9 files changed, 134 insertions(+), 201 deletions(-) diff --git a/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8ManagerWindowController.java b/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8ManagerWindowController.java index 74412c2..6a29ee0 100644 --- a/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8ManagerWindowController.java +++ b/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8ManagerWindowController.java @@ -1,7 +1,6 @@ package com.ecep.contract.manager.cloud.u8; import java.time.LocalDateTime; -import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,8 +9,6 @@ import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.ecep.contract.manager.Desktop; -import com.ecep.contract.manager.cloud.CloudBaseInfo; -import com.ecep.contract.manager.cloud.CloudInfo; import com.ecep.contract.manager.ds.company.model.Company; import com.ecep.contract.manager.ui.AbstManagerWindowController; import com.ecep.contract.manager.ui.BaseController; diff --git a/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8Service.java b/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8Service.java index 2336ffc..b90ac87 100644 --- a/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8Service.java +++ b/src/main/java/com/ecep/contract/manager/cloud/u8/YongYouU8Service.java @@ -40,6 +40,11 @@ public class YongYouU8Service implements ViewModelService { +public class CompanyCustomerService extends CompanyBasicService + implements ViewModelService { private static final Logger logger = LoggerFactory.getLogger(CompanyCustomerService.class); private static final String EVALUATION_FORM_NAME1 = "评定表"; public static final String EVALUATION_FORM_NAME2 = "评估表"; public static final String KEY_BASE_PATH = "customer.base.path"; + public static final String KEY_SALEBOOK_PATH = "customer.salebook.path"; @Lazy @Autowired @@ -78,20 +80,16 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM return companyCustomerRepository.findById(id).orElse(null); } - @Caching( - evict = { - @CacheEvict(key = "#p0.id") - } - ) + @Caching(evict = { + @CacheEvict(key = "#p0.id") + }) public CompanyCustomer save(CompanyCustomer companyCustomer) { return companyCustomerRepository.save(companyCustomer); } - @Caching( - evict = { - @CacheEvict(key = "#p0") - } - ) + @Caching(evict = { + @CacheEvict(key = "#p0") + }) @Override public void delete(CompanyCustomer entity) { companyCustomerRepository.delete(entity); @@ -164,7 +162,7 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM List retrieveFiles = new ArrayList<>(); List needMoveToCompanyPath = new ArrayList<>(); - //TODO 客户有曾用名,可能存在多个目录 + // TODO 客户有曾用名,可能存在多个目录 fetchFiles(companyCustomer.getPath(), needMoveToCompanyPath, retrieveFiles, map, status); // 移动文件到公司目录下 to company path @@ -173,7 +171,7 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM status.accept("导入 " + retrieveFiles.size() + " 个文件"); if (!retrieveFiles.isEmpty()) { - //update db + // update db retrieveFiles.forEach(v -> v.setCustomer(companyCustomer)); companyCustomerFileService.saveAll(retrieveFiles); modified = true; @@ -182,9 +180,9 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM return modified; } - @Override - protected > boolean fillFileAsDefaultType(F dbFile, File file, Consumer status) { + protected > boolean fillFileAsDefaultType(F dbFile, File file, + Consumer status) { dbFile.setType((T) CompanyCustomerFileType.General); fillFile(dbFile, file, null, status); companyCustomerFileService.save((CompanyCustomerFile) dbFile); @@ -192,7 +190,8 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM } @Override - protected > boolean fillFileAsEvaluationFile(F customerFile, File file, List fileList, Consumer status) { + protected > boolean fillFileAsEvaluationFile(F customerFile, File file, + List fileList, Consumer status) { boolean modified = super.fillFileAsEvaluationFile(customerFile, file, fileList, status); if (fileList != null) { @@ -225,7 +224,8 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM return modified; } - private > void updateEvaluationFileByJsonFile(F customerFile, File jsonFile, Consumer status) throws IOException { + private > void updateEvaluationFileByJsonFile(F customerFile, File jsonFile, + Consumer status) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); JsonNode root = objectMapper.readTree(jsonFile); if (!root.isObject()) { @@ -237,19 +237,21 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM data.remove("valid"); JsonNode type = data.remove("type"); - CompanyCustomerEvaluationFormFile formFile = companyCustomerFileService.findCustomerEvaluationFormFileByCustomerFile((CompanyCustomerFile) customerFile); + CompanyCustomerEvaluationFormFile formFile = companyCustomerFileService + .findCustomerEvaluationFormFileByCustomerFile((CompanyCustomerFile) customerFile); objectMapper.updateValue(formFile, data); logger.info("load json data from {}", jsonFile.getName()); formFile.setCatalog(type.asText()); companyCustomerEvaluationFormFileRepository.save(formFile); - // companyCustomerEvaluationFormFileRepository.save(formFile); + // companyCustomerEvaluationFormFileRepository.save(formFile); if (jsonFile.delete()) { logger.info("delete json file {}", jsonFile.getName()); } } @Override - protected > F fillFileType(File file, List fileList, Consumer status) { + protected > F fillFileType(File file, List fileList, + Consumer status) { CompanyCustomerFile customerFile = new CompanyCustomerFile(); customerFile.setType(CompanyCustomerFileType.General); if (fillFile(customerFile, file, fileList, status)) { @@ -271,7 +273,8 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM protected boolean isEvaluationFile(String fileName) { return (fileName.contains(EVALUATION_FORM_NAME1) || fileName.contains(EVALUATION_FORM_NAME2)) - && (CompanyFileUtils.withExtensions(fileName, CompanyFileUtils.JPG, CompanyFileUtils.JPEG, CompanyFileUtils.PDF)); + && (CompanyFileUtils.withExtensions(fileName, CompanyFileUtils.JPG, CompanyFileUtils.JPEG, + CompanyFileUtils.PDF)); } public boolean makePathAbsent(CompanyCustomer companyCustomer) { @@ -302,7 +305,8 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM } String companyName = company.getName(); - String fileName = formatCompanyVendorId(companyCustomer.getId()) + "-" + CompanyFileUtils.escapeFileName(companyName); + String fileName = formatCompanyVendorId(companyCustomer.getId()) + "-" + + CompanyFileUtils.escapeFileName(companyName); File dir = new File(basePath, fileName); if (!dir.exists()) { @@ -313,7 +317,6 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM return dir; } - public Page findAll(Specification spec, Pageable pageable) { return companyCustomerRepository.findAll(spec, pageable); } @@ -347,8 +350,9 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM /*** * 合并两个CompanyCustomer对象,并将fromCustomer的信息合并到toCustomer中,并保存到数据库中。 + * * @param from 源客户对象 - * @param to 目标客户对象 + * @param to 目标客户对象 */ public void resetTo(CompanyCustomer from, CompanyCustomer to) { // file @@ -359,7 +363,6 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM companyCustomerRepository.delete(from); } - /** * 删除 company 的 客户 */ @@ -386,12 +389,10 @@ public class CompanyCustomerService extends CompanyBasicService implements ViewM return customerCatalogRepository.findByCode(code).orElse(null); } - @Caching( - evict = { - @CacheEvict(key = "'catalog-'+#p0.id"), - @CacheEvict(key = "'catalog-code-'+#p0.code") - } - ) + @Caching(evict = { + @CacheEvict(key = "'catalog-'+#p0.id"), + @CacheEvict(key = "'catalog-code-'+#p0.code") + }) public CustomerCatalog save(CustomerCatalog catalog) { return customerCatalogRepository.save(catalog); } diff --git a/src/main/java/com/ecep/contract/manager/ds/other/AbstractConfigBounder.java b/src/main/java/com/ecep/contract/manager/ds/other/AbstractConfigBounder.java index 599dbde..42f98a6 100644 --- a/src/main/java/com/ecep/contract/manager/ds/other/AbstractConfigBounder.java +++ b/src/main/java/com/ecep/contract/manager/ds/other/AbstractConfigBounder.java @@ -116,6 +116,10 @@ public abstract class AbstractConfigBounder implements ConfigBounder { T newValue) { conf.setValue(getConverter().toString(newValue)); conf.setModified(LocalDateTime.now()); + save(); + } + + public void save() { SysConf saved = getConfService().save(conf); modified.set(saved.getModified()); created.set(saved.getCreated()); diff --git a/src/main/java/com/ecep/contract/manager/ds/other/StringConfig.java b/src/main/java/com/ecep/contract/manager/ds/other/StringConfig.java index bb0a5c6..3c0e8d1 100644 --- a/src/main/java/com/ecep/contract/manager/ds/other/StringConfig.java +++ b/src/main/java/com/ecep/contract/manager/ds/other/StringConfig.java @@ -1,6 +1,8 @@ package com.ecep.contract.manager.ds.other; import javafx.beans.property.Property; +import javafx.scene.control.Control; +import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.util.StringConverter; @@ -10,10 +12,10 @@ public class StringConfig extends AbstractConfigBounder { super(string); } - @Override - public TextField getControl() { - return (TextField) super.getControl(); - } + // @Override + // public TextField getControl() { + // return (TextField) super.getControl(); + // } @Override public StringConverter getConverter() { @@ -37,6 +39,14 @@ public class StringConfig extends AbstractConfigBounder { @Override void bindBidirectional(Property property) { - getControl().textProperty().bindBidirectional(property); + Control ctrl = getControl(); + if (ctrl == null) { + return; + } + if (ctrl instanceof TextField textField) { + textField.textProperty().bindBidirectional(property); + } else if (ctrl instanceof Label label) { + label.textProperty().bindBidirectional(property); + } } } diff --git a/src/main/java/com/ecep/contract/manager/ds/other/controller/SysConfWindowController.java b/src/main/java/com/ecep/contract/manager/ds/other/controller/SysConfWindowController.java index cf89dd0..105ba8e 100644 --- a/src/main/java/com/ecep/contract/manager/ds/other/controller/SysConfWindowController.java +++ b/src/main/java/com/ecep/contract/manager/ds/other/controller/SysConfWindowController.java @@ -1,11 +1,21 @@ package com.ecep.contract.manager.ds.other.controller; +import java.io.File; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import com.ecep.contract.manager.cloud.u8.YongYouU8Service; +import com.ecep.contract.manager.ds.contract.service.ContractService; import com.ecep.contract.manager.ds.customer.service.CompanyCustomerFileService; import com.ecep.contract.manager.ds.customer.service.CompanyCustomerService; +import com.ecep.contract.manager.ds.other.StringConfig; import com.ecep.contract.manager.ds.vendor.service.CompanyVendorService; -import com.ecep.contract.manager.ds.contract.service.ContractService; -import com.ecep.contract.manager.ds.other.repository.SysConfRepository; import com.ecep.contract.manager.ui.BaseController; + import jakarta.annotation.PreDestroy; import javafx.beans.property.SimpleStringProperty; import javafx.event.ActionEvent; @@ -14,14 +24,7 @@ import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Component; -import java.io.File; @Lazy @Scope("prototype") @Component @@ -42,90 +45,76 @@ public class SysConfWindowController extends BaseController { public Label customerEvaluationFormTemplateLabel; public Label customerSaleBookPathLabel; + StringConfig contractPathConfig = new StringConfig(ContractService.CONTRACT_BASE_PATH); + StringConfig vendorPathConfig = new StringConfig(CompanyVendorService.KEY_BASE_PATH); + StringConfig vendorEvaluationFormTemplateConfig = new StringConfig( + CompanyVendorService.KEY_EVALUATION_FORM_TEMPLATE); + StringConfig customerPathConfig = new StringConfig(CompanyCustomerService.KEY_BASE_PATH); + StringConfig customerEvaluationFormTemplateConfig = new StringConfig( + CompanyCustomerFileService.KEY_EVALUATION_FORM_TEMPLATE); + StringConfig customerSaleBookPathConfig = new StringConfig(CompanyCustomerService.KEY_SALEBOOK_PATH); - @Autowired - private SysConfRepository confRepository; - - private final SimpleStringProperty companyContractPath = new SimpleStringProperty(); - - private final SimpleStringProperty u8DataBaseServerHost = new SimpleStringProperty(); - private final SimpleStringProperty u8DataBaseCatalog = new SimpleStringProperty(); - private final SimpleStringProperty u8DataBaseUserName = new SimpleStringProperty(); - private final SimpleStringProperty u8DataBasePassword = new SimpleStringProperty(); - - - private final SimpleStringProperty vendorPath = new SimpleStringProperty(); - private final SimpleStringProperty vendorEvaluationFormTemplate = new SimpleStringProperty(); - - private final SimpleStringProperty customerPath = new SimpleStringProperty(); - private final SimpleStringProperty customerEvaluationFormTemplate = new SimpleStringProperty(); - private final SimpleStringProperty customerSaleBookPath = new SimpleStringProperty(); + StringConfig u8DataBaseServerHostConfig = new StringConfig(YongYouU8Service.KEY_HOST_IP); + StringConfig u8DataBaseCatalogConfig = new StringConfig(YongYouU8Service.KEY_DATABASE); + StringConfig u8DataBaseUserNameConfig = new StringConfig(YongYouU8Service.KEY_USER_NAME); + StringConfig u8DataBasePasswordConfig = new StringConfig(YongYouU8Service.KEY_PASSWORD); public void initialize() { + contractPathConfig.setControl(companyContractPathLabel); + contractPathConfig.initialize(); - initializeDirectory(companyContractPathLabel, companyContractPath, ContractService.CONTRACT_BASE_PATH); + vendorPathConfig.setControl(vendorPathLabel); + vendorPathConfig.initialize(); + vendorEvaluationFormTemplateConfig.setControl(vendorEvaluationFormTemplateLabel); + vendorEvaluationFormTemplateConfig.initialize(); - initializeDirectory(vendorPathLabel, vendorPath, CompanyVendorService.KEY_BASE_PATH); - initializeFile(vendorEvaluationFormTemplateLabel, vendorEvaluationFormTemplate, CompanyVendorService.KEY_EVALUATION_FORM_TEMPLATE); + customerPathConfig.setControl(customerPathLabel); + customerPathConfig.initialize(); + customerEvaluationFormTemplateConfig.setControl(customerEvaluationFormTemplateLabel); + customerEvaluationFormTemplateConfig.initialize(); + customerSaleBookPathConfig.setControl(customerSaleBookPathLabel); + customerSaleBookPathConfig.initialize(); - initializeDirectory(customerPathLabel, customerPath, CompanyCustomerService.KEY_BASE_PATH); - initializeFile(customerEvaluationFormTemplateLabel, customerEvaluationFormTemplate, CompanyCustomerFileService.KEY_EVALUATION_FORM_TEMPLATE); - initializeDirectory(customerSaleBookPathLabel, customerSaleBookPath, "customer.salebook.path"); - - initializeField(u8DataBaseServerHostField, u8DataBaseServerHost, "u8.db.server.ip"); - initializeField(u8DataBaseCatalogField, u8DataBaseCatalog, "u8.db.database"); - initializeField(u8DataBaseUserNameField, u8DataBaseUserName, "u8.db.server.name"); - initializeField(u8DataBasePasswordField, u8DataBasePassword, "u8.db.server.password"); + u8DataBaseServerHostConfig.setControl(u8DataBaseServerHostField); + u8DataBaseServerHostConfig.initialize(); + u8DataBaseCatalogConfig.setControl(u8DataBaseCatalogField); + u8DataBaseCatalogConfig.initialize(); + u8DataBaseUserNameConfig.setControl(u8DataBaseUserNameField); + u8DataBaseUserNameConfig.initialize(); + u8DataBasePasswordConfig.setControl(u8DataBasePasswordField); + u8DataBasePasswordConfig.initialize(); logger.debug("#initialize()"); } - private void initializeFile(Label label, SimpleStringProperty property, String key) { - label.textProperty().bind(property); - property.set(confRepository.get(key, "")); - } - - private void initializeDirectory(Label label, SimpleStringProperty property, String key) { - label.textProperty().bind(property); - property.set(confRepository.get(key, "")); - } - - private void initializeField(TextField field, SimpleStringProperty property, String key) { - field.textProperty().bindBidirectional(property); - property.set(confRepository.get(key, "")); - property.addListener((observable, oldValue, newValue) -> { - confRepository.set(key, newValue); - }); - } - - private void directoryChoose(SimpleStringProperty property, String title, String key, ActionEvent event) { + private void directoryChoose(StringConfig config, String title, String key, ActionEvent event) { DirectoryChooser chooser = new DirectoryChooser(); chooser.setTitle(title); - File value = new File(property.get()); + File value = new File(config.getProperty().getValue()); chooser.setInitialDirectory(value); Node node = (Node) event.getSource(); File selected = chooser.showDialog(node.getScene().getWindow()); if (selected != null) { - property.set(selected.getAbsolutePath()); - confRepository.set(key, property.get()); + config.getProperty().setValue(selected.getAbsolutePath()); + config.save(); } } public void changeCompanyContractPath(ActionEvent actionEvent) { - directoryChoose(companyContractPath, "请选择合同目录", ContractService.CONTRACT_BASE_PATH, actionEvent); + directoryChoose(contractPathConfig, "请选择合同目录", ContractService.CONTRACT_BASE_PATH, actionEvent); } public void changeVendorPath(ActionEvent actionEvent) { - directoryChoose(vendorPath, "请选择供应商目录", CompanyVendorService.KEY_BASE_PATH, actionEvent); + directoryChoose(vendorPathConfig, "请选择供应商目录", CompanyVendorService.KEY_BASE_PATH, actionEvent); } public void changeCustomerPath(ActionEvent actionEvent) { - directoryChoose(customerPath, "请选择客户目录", CompanyCustomerService.KEY_BASE_PATH, actionEvent); + directoryChoose(customerPathConfig, "请选择客户目录", CompanyCustomerService.KEY_BASE_PATH, actionEvent); } public void changeCustomerSaleBookPath(ActionEvent actionEvent) { - directoryChoose(customerSaleBookPath, "请选择销售台账目录", "customer.salebook.path", actionEvent); + directoryChoose(customerSaleBookPathConfig, "请选择销售台账目录", "customer.salebook.path", actionEvent); } // 模拟销毁方法 @@ -134,28 +123,29 @@ public class SysConfWindowController extends BaseController { } - private void fileChoose(SimpleStringProperty property, String title, String key, ActionEvent event) { + private void fileChoose(StringConfig config, String title, String key, ActionEvent event) { FileChooser chooser = new FileChooser(); chooser.setTitle(title); - File value = new File(property.get()); + File value = new File(config.getProperty().getValue()); chooser.setInitialDirectory(value.getParentFile()); chooser.setInitialFileName(value.getName()); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(" 模板文件(*.xlsx)", "*.xlsx")); Node node = (Node) event.getSource(); File selected = chooser.showOpenDialog(node.getScene().getWindow()); if (selected != null) { - property.set(selected.getAbsolutePath()); - confRepository.set(key, property.get()); + config.getProperty().setValue(selected.getAbsolutePath()); + config.save(); } } - public void changeCustomerEvaluationFormTemplate(ActionEvent actionEvent) { - fileChoose(customerEvaluationFormTemplate, "请选择客户资信评估表模板", CompanyCustomerFileService.KEY_EVALUATION_FORM_TEMPLATE, actionEvent); + fileChoose(customerEvaluationFormTemplateConfig, "请选择客户资信评估表模板", + CompanyCustomerFileService.KEY_EVALUATION_FORM_TEMPLATE, actionEvent); } - public void changeVendorEvaluationFormTemplate(ActionEvent actionEvent) { - fileChoose(vendorEvaluationFormTemplate, "请选择供方调查评价表模板", CompanyVendorService.KEY_EVALUATION_FORM_TEMPLATE, actionEvent); + fileChoose(vendorEvaluationFormTemplateConfig, "请选择供方调查评价表模板", + CompanyVendorService.KEY_EVALUATION_FORM_TEMPLATE, + actionEvent); } } diff --git a/src/main/java/com/ecep/contract/manager/ds/other/repository/SysConfRepository.java b/src/main/java/com/ecep/contract/manager/ds/other/repository/SysConfRepository.java index 87012ae..857aa55 100644 --- a/src/main/java/com/ecep/contract/manager/ds/other/repository/SysConfRepository.java +++ b/src/main/java/com/ecep/contract/manager/ds/other/repository/SysConfRepository.java @@ -1,85 +1,11 @@ package com.ecep.contract.manager.ds.other.repository; -import com.ecep.contract.manager.ds.other.model.SysConf; -import org.springframework.data.repository.CrudRepository; -import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; -import java.time.Instant; -import java.util.Optional; -import java.util.concurrent.CompletableFuture; +import com.ecep.contract.manager.ds.MyRepository; +import com.ecep.contract.manager.ds.other.model.SysConf; @Repository -public interface SysConfRepository extends CrudRepository, PagingAndSortingRepository { - default String get(String key) { - return get(key, null); - } +public interface SysConfRepository extends MyRepository { - /** - * 读取配置值 - * - * @param key 配置项 - * @param defaultValue 没有配置时的默认值 - * @return 配置值 - */ - default String get(String key, String defaultValue) { - Optional optional = findById(key); - if (optional.isPresent()) { - return optional.get().getValue(); - } else { - return defaultValue; - } - } - - default long get(String key, long defaultValue) { - Optional optional = findById(key); - return optional.map(sysConf -> Long.parseLong(sysConf.getValue())).orElse(defaultValue); - } - - default int get(String key, int defaultValue) { - Optional optional = findById(key); - return optional.map(sysConf -> Integer.parseInt(sysConf.getValue())).orElse(defaultValue); - } - - default boolean get(String key, boolean defaultValue) { - Optional optional = findById(key); - return optional.map(sysConf -> { - String value = sysConf.getValue(); - if (Boolean.parseBoolean(value)) { - return true; - } - return "1".equalsIgnoreCase(value); - }).orElse(defaultValue); - } - - /** - * 保存配置值 - * - * @param key 配置项 - * @param value 配置值 - * @return 配置值对象 - */ - default SysConf set(String key, String value) { - SysConf conf; - Optional optional = findById(key); - if (optional.isPresent()) { - conf = optional.get(); - } else { - conf = new SysConf(); - conf.setId(key); - conf.setCreated(Instant.now()); - } - conf.setValue(value); - conf.setModified(Instant.now()); - save(conf); - return conf; - } - - default CompletableFuture> value(String key) { - return CompletableFuture.supplyAsync(() -> findById(key)); - } - - default CompletableFuture value(String key, String value) { - return CompletableFuture.supplyAsync(() -> set(key, value)); - } } diff --git a/src/main/java/com/ecep/contract/manager/ds/other/service/SysConfService.java b/src/main/java/com/ecep/contract/manager/ds/other/service/SysConfService.java index 2b8b516..cd12ea8 100644 --- a/src/main/java/com/ecep/contract/manager/ds/other/service/SysConfService.java +++ b/src/main/java/com/ecep/contract/manager/ds/other/service/SysConfService.java @@ -1,6 +1,6 @@ package com.ecep.contract.manager.ds.other.service; -import java.time.Instant; +import java.time.LocalDateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; @@ -35,10 +35,10 @@ public class SysConfService { if (conf == null) { conf = new SysConf(); conf.setId(key); - conf.setCreated(Instant.now()); + conf.setCreated(LocalDateTime.now()); } conf.setValue(value); - conf.setModified(Instant.now()); + conf.setModified(LocalDateTime.now()); repository.save(conf); return conf; }