|
|
@@ -0,0 +1,79 @@
|
|
|
+package com.zxyj.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.zxyj.common.Result;
|
|
|
+import com.zxyj.dto.SysConfigDTO;
|
|
|
+import com.zxyj.entity.SysConfig;
|
|
|
+import com.zxyj.mapper.SysConfigMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class SysConfigService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SysConfigMapper configMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有配置(key-value 形式)
|
|
|
+ */
|
|
|
+ public Result<Map<String, String>> getAllAsMap() {
|
|
|
+ List<SysConfig> list = configMapper.selectList(null);
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ for (SysConfig c : list) {
|
|
|
+ map.put(c.getConfigKey(), c.getConfigValue());
|
|
|
+ }
|
|
|
+ return Result.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有配置(详细列表)
|
|
|
+ */
|
|
|
+ public Result<List<SysConfigDTO>> listAll() {
|
|
|
+ List<SysConfig> list = configMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<SysConfig>().orderByAsc(SysConfig::getConfigKey));
|
|
|
+ return Result.success(list.stream().map(SysConfigDTO::from).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单个配置
|
|
|
+ */
|
|
|
+ public Result<SysConfigDTO> get(String key) {
|
|
|
+ SysConfig config = configMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getConfigKey, key));
|
|
|
+ if (config == null) {
|
|
|
+ return Result.error("配置不存在");
|
|
|
+ }
|
|
|
+ return Result.success(SysConfigDTO.from(config));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新配置
|
|
|
+ */
|
|
|
+ public Result<String> update(String key, String value) {
|
|
|
+ SysConfig config = configMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getConfigKey, key));
|
|
|
+ if (config == null) {
|
|
|
+ return Result.error("配置不存在");
|
|
|
+ }
|
|
|
+ config.setConfigValue(value);
|
|
|
+ config.setUpdatedAt(new Date());
|
|
|
+ configMapper.updateById(config);
|
|
|
+ return Result.success("更新成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 内部方法:获取配置值(供其他 Service 调用)
|
|
|
+ */
|
|
|
+ public String getValue(String key) {
|
|
|
+ SysConfig config = configMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getConfigKey, key));
|
|
|
+ return config != null ? config.getConfigValue() : null;
|
|
|
+ }
|
|
|
+}
|