package com.wok.supportbot.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.wok.supportbot.dao.SystemConfigMapper; import com.wok.supportbot.entity.SystemConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; /** * 系统配置 CRUD 服务 *

* 使用 key-value 模式存储系统级配置项(如 SDK 保密声明等), * 便于管理后台灵活编辑、SDK 端动态拉取。 */ @Slf4j @Service public class SystemConfigService { @Autowired private SystemConfigMapper systemConfigMapper; /** * 根据 key 查询配置值(仅返回 configValue 字符串) * * @param key 配置键 * @return configValue,不存在返回 null */ public String getValueByKey(String key) { SystemConfig config = getByKey(key); return config != null ? config.getConfigValue() : null; } /** * 根据 key 查询完整配置实体 * * @param key 配置键 * @return 配置实体,不存在返回 null */ public SystemConfig getByKey(String key) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SystemConfig::getConfigKey, key); return systemConfigMapper.selectOne(wrapper); } /** * 保存或更新配置(upsert 语义) * * @param key 配置键 * @param value 配置值 * @param description 描述说明 */ public void saveOrUpdate(String key, String value, String description) { SystemConfig existing = getByKey(key); if (existing != null) { // 更新 configValue、description 和 updateTime LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(SystemConfig::getId, existing.getId()) .set(SystemConfig::getConfigValue, value) .set(SystemConfig::getDescription, description) .set(SystemConfig::getUpdateTime, new Date()); systemConfigMapper.update(null, wrapper); log.info("更新系统配置:{}", key); } else { // 新建 SystemConfig config = SystemConfig.builder() .configKey(key) .configValue(value) .description(description) .build(); systemConfigMapper.insert(config); log.info("新建系统配置:{}", key); } } /** * 查询所有未删除的配置列表 * * @return 配置实体列表 */ public List listAll() { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.orderByAsc(SystemConfig::getConfigKey); return systemConfigMapper.selectList(wrapper); } /** * 根据 key 删除配置(逻辑删除) * * @param key 配置键 * @return true 删除成功,false 配置不存在 */ public boolean deleteByKey(String key) { SystemConfig existing = getByKey(key); if (existing == null) { return false; } systemConfigMapper.deleteById(existing.getId()); log.info("删除系统配置:{}", key); return true; } }