From d0b25091a0c2dbef45299e152078f858d6d4039e Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期三, 28 一月 2026 15:07:36 +0800
Subject: [PATCH] #
---
zy-acs-manager/src/main/java/com/zy/acs/manager/system/service/impl/ConfigServiceImpl.java | 85 +++++++++++++++++++++++++++++++-----------
1 files changed, 62 insertions(+), 23 deletions(-)
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/system/service/impl/ConfigServiceImpl.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/system/service/impl/ConfigServiceImpl.java
index d20cd4c..68de310 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/system/service/impl/ConfigServiceImpl.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/system/service/impl/ConfigServiceImpl.java
@@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.common.utils.GsonUtils;
+import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.DateUtils;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.system.entity.Config;
@@ -12,7 +13,10 @@
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
-import java.util.*;
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -25,45 +29,71 @@
@PostConstruct
public void init() {
+ CONFIG_CACHE.clear();
+
List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getStatus, StatusType.ENABLE.val));
for (Config config : list) {
- CONFIG_CACHE.put(config.getFlag(), config);
+ if (null != config && !Cools.isEmpty(config.getFlag())) {
+ CONFIG_CACHE.put(config.getFlag(), config);
+ }
}
}
@Override
@SuppressWarnings("unchecked")
public <T> T getVal(String key, Class<T> clazz) {
+ if (Cools.isEmpty(key.trim(), clazz)) {
+ return null;
+ }
+
Config config = CONFIG_CACHE.get(key);
if (config == null) {
- List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
- config = list.stream().findFirst().orElse(null);
+ config = getOne(new LambdaQueryWrapper<Config>()
+ .eq(Config::getFlag, key)
+ .eq(Config::getStatus, StatusType.ENABLE.val), false);
+
if (null == config) {
return null;
}
+
+ if (!Cools.isEmpty(config.getFlag())) {
+ CONFIG_CACHE.put(config.getFlag(), config);
+ }
}
- String val = config.getVal();
- switch (ConfigType.query(config.getType())) {
- case BOOLEAN:
- if (val.equals("1") || val.trim().equalsIgnoreCase("TRUE")) {
+
+ String raw = config.getVal();
+ String val = Cools.isEmpty(raw) ? "" : raw.trim();
+ ConfigType type = ConfigType.query(config.getType());
+ if (null == type) {
+ return null;
+ }
+ switch (type) {
+ case BOOLEAN: {
+ if ("1".equals(val) || "true".equalsIgnoreCase(val) || "yes".equalsIgnoreCase(val)) {
return (T) Boolean.TRUE;
}
return (T) Boolean.FALSE;
- case NUMBER:
- if (clazz == Integer.class) {
+ }
+ case NUMBER: {
+ if (clazz == Integer.class || clazz == int.class) {
return (T) Integer.valueOf(val);
- } else if (clazz == Short.class) {
+ } else if (clazz == Short.class || clazz == short.class) {
return (T) Short.valueOf(val);
- } else if (clazz == Long.class) {
+ } else if (clazz == Long.class || clazz == long.class) {
return (T) Long.valueOf(val);
- } else if (clazz == Double.class) {
+ } else if (clazz == Double.class || clazz == double.class) {
return (T) Double.valueOf(val);
+ } else if (clazz == Float.class || clazz == float.class) {
+ return (T) Float.valueOf(val);
+ } else if (clazz == BigDecimal.class) {
+ return (T) new BigDecimal(val);
}
- throw new UnsupportedOperationException("Unsupported type: " + clazz.getName());
+ throw new UnsupportedOperationException("Unsupported number type: " + clazz.getName());
+ }
case STRING:
- return (T) val;
+ return (T) raw;
case JSON:
- return GsonUtils.fromJson(val, clazz);
+ return GsonUtils.fromJson(raw, clazz);
case DATE:
return (T) DateUtils.convert(val);
default:
@@ -73,11 +103,20 @@
@Override
public <T> boolean setVal(String key, T val) {
+ if (key == null || key.trim().isEmpty()) {
+ return false;
+ }
+
Config config = CONFIG_CACHE.get(key);
if (config == null) {
- List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
- config = list.stream().findFirst().orElse(null);
- if (null == config) {
+ config = getOne(new LambdaQueryWrapper<Config>()
+ .eq(Config::getFlag, key)
+ .eq(Config::getStatus, StatusType.ENABLE.val), false);
+ if (config == null) {
+ return false;
+ }
+ } else {
+ if (config.getStatus() == null || config.getStatus() != StatusType.ENABLE.val) {
return false;
}
}
@@ -88,10 +127,11 @@
if (!(val instanceof Boolean)) {
throw new IllegalArgumentException("Expected Boolean value for key: " + key);
}
- config.setVal((Boolean) val ? "TRUE" : "FALSE");
+ config.setVal(((Boolean) val) ? "TRUE" : "FALSE");
break;
case NUMBER:
- if (val instanceof Integer || val instanceof Short || val instanceof Long || val instanceof Double) {
+ if (val instanceof Integer || val instanceof Short || val instanceof Long
+ || val instanceof Double || val instanceof Float || val instanceof BigDecimal) {
config.setVal(String.valueOf(val));
} else {
throw new IllegalArgumentException("Expected a numeric value for key: " + key);
@@ -118,5 +158,4 @@
return this.updateById(config);
}
-
-}
+}
\ No newline at end of file
--
Gitblit v1.9.1