package com.mosquito.project.service; import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** * 系统配置服务 */ @Service public class SystemService { private final Map configs = new ConcurrentHashMap<>(); private final Map> caches = new ConcurrentHashMap<>(); public SystemService() { // 初始化默认配置 configs.put("system.name", "蚊子系统"); configs.put("system.version", "1.0.0"); configs.put("system.timezone", "Asia/Shanghai"); configs.put("reward.points.enabled", "true"); configs.put("reward.coupon.enabled", "true"); configs.put("risk.alert.enabled", "true"); configs.put("risk.auto.block", "false"); } /** * 获取配置列表 */ public Map getConfigs(String category, String keyword) { Map result = new HashMap<>(); List> configList = new ArrayList<>(); for (Map.Entry entry : configs.entrySet()) { if (category != null && !category.isEmpty()) { String configCategory = entry.getKey().split("\\.")[0]; if (!category.equals(configCategory)) continue; } if (keyword != null && !keyword.isEmpty()) { if (!entry.getKey().contains(keyword) && !entry.getValue().contains(keyword)) { continue; } } Map config = new HashMap<>(); config.put("key", entry.getKey()); config.put("value", entry.getValue()); configList.add(config); } result.put("data", configList); result.put("total", configList.size()); return result; } /** * 获取单个配置 */ public Map getConfigByKey(String key) { String value = configs.get(key); if (value == null) return null; Map result = new HashMap<>(); result.put("key", key); result.put("value", value); return result; } /** * 更新配置 */ public boolean updateConfig(String key, String value) { configs.put(key, value); return true; } /** * 批量更新配置 */ public int batchUpdateConfigs(Map configsToUpdate) { int count = 0; for (Map.Entry entry : configsToUpdate.entrySet()) { if (entry.getValue() != null) { configs.put(entry.getKey(), entry.getValue().toString()); count++; } } return count; } /** * 重置配置 */ public boolean resetConfig(String key) { // 重置为默认值 configs.remove(key); return true; } /** * 清除缓存 */ public boolean clearCache(String type) { if (type != null && !type.isEmpty()) { caches.remove(type); } else { caches.clear(); } return true; } /** * 获取缓存列表 */ public List> getCacheList() { List> result = new ArrayList<>(); for (Map.Entry> entry : caches.entrySet()) { Map cache = new HashMap<>(); cache.put("name", entry.getKey()); cache.put("size", entry.getValue().size()); result.add(cache); } return result; } /** * 获取系统信息 */ public Map getSystemInfo() { Map info = new HashMap<>(); info.put("version", "1.0.0"); info.put("uptime", System.currentTimeMillis()); info.put("memory", Map.of( "total", Runtime.getRuntime().totalMemory(), "used", Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(), "free", Runtime.getRuntime().freeMemory() )); info.put("cpu", Runtime.getRuntime().availableProcessors()); info.put("threads", Thread.activeCount()); return info; } }