🔨 系统设置模块优化.

This commit is contained in:
lijiahang
2025-01-10 10:44:45 +08:00
parent 72579c7e83
commit 2a5bda3d00
15 changed files with 362 additions and 147 deletions

View File

@@ -32,16 +32,18 @@ import org.dromara.visor.framework.log.core.enums.IgnoreLogMode;
import org.dromara.visor.framework.web.core.annotation.DemoDisableApi;
import org.dromara.visor.framework.web.core.annotation.RestWrapper;
import org.dromara.visor.module.infra.define.operator.SystemSettingOperatorType;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdatePartialRequest;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateBatchRequest;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest;
import org.dromara.visor.module.infra.entity.vo.AppInfoVO;
import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO;
import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel;
import org.dromara.visor.module.infra.service.SystemSettingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
import javax.annotation.security.PermitAll;
/**
* 系统设置服务
@@ -68,30 +70,49 @@ public class SystemSettingController {
return systemSettingService.getAppInfo();
}
@PermitAll
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/setting")
@Operation(summary = "获取系统聚合设置")
public SystemSettingAggregateVO getSystemAggregateSetting() {
return systemSettingService.getSystemAggregateSetting();
}
@DemoDisableApi
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/generator-keypair")
@Operation(summary = "生成密钥对")
@PreAuthorize("@ss.hasPermission('infra:system-setting:update')")
public EncryptSystemSettingModel generatorKeypair() {
return systemSettingService.generatorKeypair();
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/get")
@Operation(summary = "查询系统设置")
@Parameter(name = "type", description = "type", required = true)
public Map<String, Object> getSystemSettingByType(@RequestParam("type") String type) {
@PreAuthorize("@ss.hasPermission('infra:system-setting:update')")
public Object getSystemSettingByType(@RequestParam("type") String type) {
return systemSettingService.getSystemSettingByType(type);
}
@DemoDisableApi
@OperatorLog(SystemSettingOperatorType.UPDATE)
@PutMapping("/update")
@Operation(summary = "更新系统设置")
@Operation(summary = "更新系统设置-单个")
@PreAuthorize("@ss.hasPermission('infra:system-setting:update')")
public Integer updateSystemSetting(@Validated @RequestBody SystemSettingUpdateRequest request) {
return systemSettingService.updateSystemSetting(request);
public Boolean updateSystemSetting(@Validated @RequestBody SystemSettingUpdateRequest request) {
systemSettingService.updateSystemSetting(request);
return true;
}
@DemoDisableApi
@OperatorLog(SystemSettingOperatorType.UPDATE)
@PutMapping("/update-partial")
@Operation(summary = "更新部分系统设置")
@PutMapping("/update-batch")
@Operation(summary = "更新系统设置-多个")
@PreAuthorize("@ss.hasPermission('infra:system-setting:update')")
public Boolean updatePartialSystemSetting(@Validated @RequestBody SystemSettingUpdatePartialRequest request) {
systemSettingService.updatePartialSystemSetting(request);
public Boolean updateSystemSettingBatch(@Validated @RequestBody SystemSettingUpdateBatchRequest request) {
systemSettingService.updateSystemSettingBatch(request);
return true;
}

View File

@@ -25,7 +25,6 @@ package org.dromara.visor.module.infra.convert;
import org.dromara.visor.module.infra.entity.domain.SystemSettingDO;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
@@ -40,7 +39,6 @@ public interface SystemSettingConvert {
SystemSettingConvert MAPPER = Mappers.getMapper(SystemSettingConvert.class);
@Mapping(target = "value", ignore = true)
SystemSettingDO to(SystemSettingUpdateRequest request);
}

View File

@@ -25,7 +25,7 @@ package org.dromara.visor.module.infra.define.cache;
import cn.orionsec.kit.lang.define.cache.key.CacheKeyBuilder;
import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine;
import cn.orionsec.kit.lang.define.cache.key.struct.RedisCacheStruct;
import cn.orionsec.kit.lang.define.wrapper.Ref;
import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO;
import java.util.concurrent.TimeUnit;
@@ -39,10 +39,10 @@ import java.util.concurrent.TimeUnit;
public interface SystemSettingKeyDefine {
CacheKeyDefine SETTING = new CacheKeyBuilder()
.key("system:setting:{}")
.desc("系统设置 ${type}")
.type(Ref.class)
.struct(RedisCacheStruct.HASH)
.key("system:setting:agg")
.desc("系统聚合设置")
.type(SystemSettingAggregateVO.class)
.struct(RedisCacheStruct.STRING)
.timeout(8, TimeUnit.HOURS)
.build();

View File

@@ -40,7 +40,7 @@ public class SystemSettingOperatorType extends InitializingOperatorTypes {
public static final String UPDATE_TEXT = "<sb>{}</sb> - <sb>{}</sb> - <sb>{}</sb>";
public static final String UPDATE_PARTIAL_TEXT = "<sb>{}</sb>";
public static final String UPDATE_BATCH_TEXT = "<sb>{}</sb>";
public static final String UPDATE = "system-setting:update";

View File

@@ -52,6 +52,10 @@ public class SystemSettingDO extends BaseDO {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@Schema(description = "配置key")
@TableField("config_key")
private String configKey;
@Schema(description = "配置类型")
@TableField("type")
private String type;

View File

@@ -35,7 +35,7 @@ import java.io.Serializable;
import java.util.Map;
/**
* 系统设置部分 更新请求对象
* 系统设置 批量更新请求对象
*
* @author Jiahang Li
* @version 3.0.0
@@ -45,8 +45,8 @@ import java.util.Map;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemSettingUpdatePartialRequest", description = "系统设置部分 更新请求对象")
public class SystemSettingUpdatePartialRequest implements Serializable {
@Schema(name = "SystemSettingUpdateBatchRequest", description = "系统设置 批量更新请求对象")
public class SystemSettingUpdateBatchRequest implements Serializable {
private static final long serialVersionUID = 1L;
@@ -57,6 +57,6 @@ public class SystemSettingUpdatePartialRequest implements Serializable {
@NotEmpty
@Schema(description = "配置")
private Map<String, Object> settings;
private Map<String, String> settings;
}

View File

@@ -59,6 +59,6 @@ public class SystemSettingUpdateRequest implements Serializable {
private String item;
@Schema(description = "配置值")
private Object value;
private String value;
}

View File

@@ -20,37 +20,34 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.module.infra.handler.setting.strategy;
package org.dromara.visor.module.infra.entity.vo;
import cn.orionsec.kit.lang.utils.Exceptions;
import org.dromara.visor.common.handler.data.strategy.AbstractGenericsDataStrategy;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel;
import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingModel;
import org.springframework.stereotype.Component;
/**
* SFTP 系统配置策略
* 系统设置 聚合响应对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/10/9 11:44
* @since 2025/1/3 17:46
*/
@Component
public class SftpSystemSettingStrategy extends AbstractGenericsDataStrategy<SftpSystemSettingModel> {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemSettingAggregateVO", description = "系统设置 聚合响应对象")
public class SystemSettingAggregateVO {
public SftpSystemSettingStrategy() {
super(SftpSystemSettingModel.class);
}
@Schema(description = "SFTP 设置")
private SftpSystemSettingModel sftp;
@Override
public SftpSystemSettingModel getDefault() {
return SftpSystemSettingModel.builder()
.previewSize(2)
.build();
}
@Schema(description = "加密设置")
private EncryptSystemSettingModel encrypt;
@Override
public SftpSystemSettingModel parse(String serialModel) {
throw Exceptions.unsupported();
}
}
}

View File

@@ -22,12 +22,14 @@
*/
package org.dromara.visor.module.infra.enums;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.dromara.visor.common.handler.data.GenericsStrategyDefinition;
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
import org.dromara.visor.common.handler.data.strategy.GenericsDataStrategy;
import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSettingStrategy;
import org.dromara.visor.common.constant.Const;
import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel;
import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingModel;
import java.util.Map;
/**
* 系统配置类型枚举
@@ -38,34 +40,55 @@ import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSetting
*/
@Getter
@AllArgsConstructor
public enum SystemSettingTypeEnum implements GenericsStrategyDefinition {
public enum SystemSettingTypeEnum {
/**
* SFTP 配置
*/
SFTP(SftpSystemSettingStrategy.class),
SFTP("sftp", SftpSystemSettingModel.class),
/**
* 加密配置
*/
ENCRYPT("encrypt", EncryptSystemSettingModel.class),
;
SystemSettingTypeEnum(Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass) {
this.type = this.name();
this.strategyClass = strategyClass;
private final String prefix;
private final Class<?> modelClass;
/**
* 解析
*
* @param settings settings
* @return model
*/
@SuppressWarnings("unchecked")
public <T> T parseModel(Map<String, String> settings) {
return (T) JSON.parseObject(JSON.toJSONString(settings)).toJavaObject(modelClass);
}
private final String type;
private final Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass;
public static SystemSettingTypeEnum of(String type) {
if (type == null) {
return null;
}
for (SystemSettingTypeEnum value : values()) {
if (value.type.equals(type)) {
if (value.name().equals(type)) {
return value;
}
}
return null;
}
/**
* 获取 key
*
* @param item item
* @return key
*/
public String getConfigKey(String item) {
return prefix + Const.DOT + item;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.module.infra.framework.service.impl;
import cn.orionsec.kit.lang.function.Functions;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.framework.config.core.service.ConfigFrameworkService;
import org.dromara.visor.module.infra.dao.SystemSettingDAO;
import org.dromara.visor.module.infra.entity.domain.SystemSettingDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 配置框架服务实现
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025/1/6 23:35
*/
@Slf4j
@Service
public class ConfigFrameworkServiceImpl implements ConfigFrameworkService {
@Resource
private SystemSettingDAO systemSettingDAO;
@Override
public Map<String, String> getAllConfig() {
return systemSettingDAO.of()
.stream()
.collect(Collectors.toMap(
SystemSettingDO::getConfigKey,
SystemSettingDO::getValue,
Functions.right()));
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.module.infra.handler.setting.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 加密系统配置模型
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/10/9 11:45
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EncryptSystemSettingModel {
/**
* 加密公钥
*/
private String publicKey;
/**
* 加密私钥
*/
private String privateKey;
}

View File

@@ -26,7 +26,6 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
/**
* SFTP 系统配置模型
@@ -39,7 +38,7 @@ import org.dromara.visor.common.handler.data.model.GenericsDataModel;
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SftpSystemSettingModel implements GenericsDataModel {
public class SftpSystemSettingModel {
/**
* 预览大小

View File

@@ -22,11 +22,11 @@
*/
package org.dromara.visor.module.infra.service;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdatePartialRequest;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateBatchRequest;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest;
import org.dromara.visor.module.infra.entity.vo.AppInfoVO;
import java.util.Map;
import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO;
import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel;
/**
* 系统设置服务
@@ -45,26 +45,40 @@ public interface SystemSettingService {
AppInfoVO getAppInfo();
/**
* 更新系统设置
* 获取系统聚合设置
*
* @param request request
* @return effect
* @return setting
*/
Integer updateSystemSetting(SystemSettingUpdateRequest request);
SystemSettingAggregateVO getSystemAggregateSetting();
/**
* 生成密钥对
*
* @return keypair
*/
EncryptSystemSettingModel generatorKeypair();
/**
* 通过类型查询系统设置
*
* @param type type
* @param <T> T
* @return row
*/
<T> T getSystemSettingByType(String type);
/**
* 更新部分系统设置
*
* @param request request
*/
void updatePartialSystemSetting(SystemSettingUpdatePartialRequest request);
void updateSystemSetting(SystemSettingUpdateRequest request);
/**
* 通过类型查询系统设置
* 更新系统设置
*
* @param type type
* @return row
* @param request request
*/
Map<String, Object> getSystemSettingByType(String type);
void updateSystemSettingBatch(SystemSettingUpdateBatchRequest request);
}

View File

@@ -23,38 +23,46 @@
package org.dromara.visor.module.infra.service.impl;
import cn.orionsec.kit.ext.process.ProcessAwaitExecutor;
import cn.orionsec.kit.lang.define.wrapper.Pair;
import cn.orionsec.kit.lang.function.Functions;
import cn.orionsec.kit.lang.support.Attempt;
import cn.orionsec.kit.lang.utils.Arrays1;
import cn.orionsec.kit.lang.utils.Refs;
import cn.orionsec.kit.lang.utils.Objects1;
import cn.orionsec.kit.lang.utils.Strings;
import cn.orionsec.kit.lang.utils.collect.Maps;
import cn.orionsec.kit.lang.utils.crypto.Keys;
import cn.orionsec.kit.lang.utils.crypto.RSA;
import cn.orionsec.kit.lang.utils.crypto.Signatures;
import cn.orionsec.kit.lang.utils.io.Streams;
import cn.orionsec.kit.spring.SpringHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.dromara.visor.common.constant.AppConst;
import org.dromara.visor.common.constant.Const;
import org.dromara.visor.common.constant.ErrorMessage;
import org.dromara.visor.common.utils.Valid;
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
import org.dromara.visor.framework.redis.core.utils.RedisMaps;
import org.dromara.visor.framework.config.core.event.ConfigUpdateEvent;
import org.dromara.visor.framework.redis.core.utils.RedisStrings;
import org.dromara.visor.framework.redis.core.utils.RedisUtils;
import org.dromara.visor.module.infra.dao.SystemSettingDAO;
import org.dromara.visor.module.infra.define.cache.SystemSettingKeyDefine;
import org.dromara.visor.module.infra.define.operator.SystemSettingOperatorType;
import org.dromara.visor.module.infra.entity.domain.SystemSettingDO;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdatePartialRequest;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateBatchRequest;
import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest;
import org.dromara.visor.module.infra.entity.vo.AppInfoVO;
import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO;
import org.dromara.visor.module.infra.enums.SystemSettingTypeEnum;
import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel;
import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingModel;
import org.dromara.visor.module.infra.service.SystemSettingService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -81,28 +89,103 @@ public class SystemSettingServiceImpl implements SystemSettingService {
}
@Override
public Integer updateSystemSetting(SystemSettingUpdateRequest request) {
String type = request.getType();
String item = request.getItem();
Object value = request.getValue();
// 更新
SystemSettingDO update = new SystemSettingDO();
update.setValue(Refs.json(value));
LambdaQueryWrapper<SystemSettingDO> wrapper = systemSettingDAO.lambda()
.eq(SystemSettingDO::getType, type)
.eq(SystemSettingDO::getItem, item);
int effect = systemSettingDAO.update(update, wrapper);
// 删除缓存
RedisUtils.delete(SystemSettingKeyDefine.SETTING.format(type));
// 设置日志参数
OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_TEXT, type, item, value));
return effect;
public SystemSettingAggregateVO getSystemAggregateSetting() {
// 查询缓存
SystemSettingAggregateVO cache = RedisStrings.getJson(SystemSettingKeyDefine.SETTING);
SystemSettingAggregateVO result = Objects1.def(cache, SystemSettingAggregateVO::new);
if (cache == null) {
// 查询数据库
Map<String, List<SystemSettingDO>> typeGroup = systemSettingDAO.of()
.createWrapper()
.select(SystemSettingDO::getType,
SystemSettingDO::getItem,
SystemSettingDO::getValue)
.in(SystemSettingDO::getType,
SystemSettingTypeEnum.SFTP.name(),
SystemSettingTypeEnum.ENCRYPT.name())
.then()
.stream()
.collect(Collectors.groupingBy(SystemSettingDO::getType));
// 数据组合
typeGroup.forEach((k, v) -> {
// 类型数据
SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(k);
Map<String, String> typeSettings = v.stream()
.collect(Collectors.toMap(
SystemSettingDO::getItem,
SystemSettingDO::getValue,
Functions.right()));
Object setting = settingType.parseModel(typeSettings);
if (SystemSettingTypeEnum.SFTP.equals(settingType)) {
// SFTP 设置
result.setSftp((SftpSystemSettingModel) setting);
} else if (SystemSettingTypeEnum.ENCRYPT.equals(settingType)) {
// 加密设置
EncryptSystemSettingModel encryptSetting = (EncryptSystemSettingModel) setting;
encryptSetting.setPrivateKey(null);
result.setEncrypt(encryptSetting);
}
});
// 设置缓存
RedisStrings.setJson(SystemSettingKeyDefine.SETTING, result);
}
return result;
}
@Override
public void updatePartialSystemSetting(SystemSettingUpdatePartialRequest request) {
public EncryptSystemSettingModel generatorKeypair() {
// 生成密钥对
Pair<RSAPublicKey, RSAPrivateKey> pair = RSA.generatorKeys();
return EncryptSystemSettingModel.builder()
.publicKey(Keys.getPublicKey(pair.getKey()))
.privateKey(Keys.getPrivateKey(pair.getValue()))
.build();
}
@Override
public <T> T getSystemSettingByType(String type) {
SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(type);
Valid.notNull(settingType, ErrorMessage.ERROR_TYPE);
// 查询数据库
Map<String, String> settings = systemSettingDAO.of()
.createWrapper()
.eq(SystemSettingDO::getType, type)
.then()
.stream()
.collect(Collectors.toMap(
SystemSettingDO::getItem,
SystemSettingDO::getValue,
Functions.right()));
// 解析
return settingType.parseModel(settings);
}
@Override
public void updateSystemSetting(SystemSettingUpdateRequest request) {
String type = request.getType();
Map<String, Object> settings = request.getSettings();
SystemSettingTypeEnum settingType = Valid.valid(SystemSettingTypeEnum::of, type);
String item = request.getItem();
String value = request.getValue();
// 更新
SystemSettingDO update = new SystemSettingDO();
update.setValue(value);
LambdaQueryWrapper<SystemSettingDO> wrapper = systemSettingDAO.lambda()
.eq(SystemSettingDO::getType, type)
.eq(SystemSettingDO::getItem, item);
systemSettingDAO.update(update, wrapper);
// 删除缓存
RedisUtils.delete(SystemSettingKeyDefine.SETTING);
// 设置日志参数
OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_TEXT, type, item, value));
// 触发修改事件
SpringHolder.publishEvent(ConfigUpdateEvent.of(settingType.getConfigKey(item), value));
}
@Override
public void updateSystemSettingBatch(SystemSettingUpdateBatchRequest request) {
String type = request.getType();
SystemSettingTypeEnum settingType = Valid.valid(SystemSettingTypeEnum::of, type);
Map<String, String> settings = request.getSettings();
// 删除
LambdaQueryWrapper<SystemSettingDO> deleteWrapper = systemSettingDAO.lambda()
.eq(SystemSettingDO::getType, type)
@@ -112,63 +195,25 @@ public class SystemSettingServiceImpl implements SystemSettingService {
List<SystemSettingDO> rows = settings.entrySet()
.stream()
.map(s -> SystemSettingDO.builder()
.configKey(settingType.getConfigKey(s.getKey()))
.type(type)
.item(s.getKey())
.value(Refs.json(s.getValue()))
.value(s.getValue())
.build())
.collect(Collectors.toList());
// 插入
systemSettingDAO.insertBatch(rows);
// 删除缓存
RedisUtils.delete(SystemSettingKeyDefine.SETTING.format(type));
RedisUtils.delete(SystemSettingKeyDefine.SETTING);
// 设置日志参数
OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_PARTIAL_TEXT, type));
}
@Override
public Map<String, Object> getSystemSettingByType(String type) {
SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(type);
Valid.notNull(settingType, ErrorMessage.ERROR_TYPE);
// 查询缓存
String key = SystemSettingKeyDefine.SETTING.format(type);
Map<String, String> settings = RedisMaps.entities(key);
boolean setCache = Maps.isEmpty(settings);
// 查询数据库
if (Maps.isEmpty(settings)) {
settings = systemSettingDAO.of()
.createWrapper()
.eq(SystemSettingDO::getType, type)
.then()
.stream()
.collect(Collectors.toMap(
SystemSettingDO::getItem,
SystemSettingDO::getValue,
Functions.right()));
}
// 初始化
if (Maps.isEmpty(settings)) {
// 获取默认值
Map<String, Object> defaultConfig = settingType.getDefault().toMap();
settings = Maps.map(defaultConfig, Function.identity(), Refs::json);
// 插入默认值
List<SystemSettingDO> entities = settings
.entrySet()
.stream()
.map(s -> {
SystemSettingDO entity = new SystemSettingDO();
entity.setType(type);
entity.setItem(s.getKey());
entity.setValue(s.getValue());
return entity;
}).collect(Collectors.toList());
systemSettingDAO.insertBatch(entities);
}
// 设置缓存
if (setCache) {
RedisMaps.putAll(key, SystemSettingKeyDefine.SETTING, settings);
}
// unRef
return Maps.map(settings, Function.identity(), Refs::unref);
OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_BATCH_TEXT, type));
// 触发修改事件
Map<String, String> eventConfig = rows.stream()
.collect(Collectors.toMap(
SystemSettingDO::getConfigKey,
SystemSettingDO::getValue,
Functions.right()));
SpringHolder.publishEvent(ConfigUpdateEvent.of(eventConfig));
}
/**

View File

@@ -5,6 +5,7 @@
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="org.dromara.visor.module.infra.entity.domain.SystemSettingDO">
<id column="id" property="id"/>
<result column="config_key" property="configKey"/>
<result column="type" property="type"/>
<result column="item" property="item"/>
<result column="value" property="value"/>
@@ -17,7 +18,7 @@
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, type, item, value, create_time, update_time, creator, updater, deleted
id, config_key, type, item, value, create_time, update_time, creator, updater, deleted
</sql>
</mapper>