mirror of https://gitee.com/stylefeng/roses
【7.0.4】【config】更新系统初始化
parent
79c680f810
commit
9bf96b07bf
|
@ -92,4 +92,9 @@ public interface RuleConstants {
|
|||
*/
|
||||
String BASE64_IMG_PREFIX = "data:image/png;base64,";
|
||||
|
||||
/**
|
||||
* 系统配置初始化的标识的常量名称,用在sys_config表作为config_code
|
||||
*/
|
||||
String SYSTEM_CONFIG_INIT_FLAG_NAME = "SYS_CONFIG_INIT_FLAG";
|
||||
|
||||
}
|
||||
|
|
|
@ -85,7 +85,12 @@ public enum ConfigExceptionEnum implements AbstractExceptionEnum {
|
|||
/**
|
||||
* 配置容器是空,请先初始化配置容器
|
||||
*/
|
||||
CONFIG_CONTAINER_IS_NULL(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ConfigConstants.CONFIG_EXCEPTION_STEP_CODE + "10", "配置容器为空,请先初始化配置容器,请调用ConfigContext.setConfigApi()初始化");
|
||||
CONFIG_CONTAINER_IS_NULL(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ConfigConstants.CONFIG_EXCEPTION_STEP_CODE + "10", "配置容器为空,请先初始化配置容器,请调用ConfigContext.setConfigApi()初始化"),
|
||||
|
||||
/**
|
||||
* 初始化配置失败,参数为空
|
||||
*/
|
||||
CONFIG_INIT_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ConfigConstants.CONFIG_EXCEPTION_STEP_CODE + "11", "初始化配置失败,参数为空");
|
||||
|
||||
/**
|
||||
* 错误编码
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package cn.stylefeng.roses.kernel.config.api.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 初始化系统配置参数
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/8 16:38
|
||||
*/
|
||||
@Data
|
||||
public class ConfigInitRequest {
|
||||
|
||||
/**
|
||||
* 系统配置集合
|
||||
* <p>
|
||||
* key是配置编码,value是配置值
|
||||
*/
|
||||
private Map<String, String> sysConfigs;
|
||||
|
||||
}
|
|
@ -24,6 +24,7 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.config.modular.controller;
|
||||
|
||||
import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitRequest;
|
||||
import cn.stylefeng.roses.kernel.config.modular.param.SysConfigParam;
|
||||
import cn.stylefeng.roses.kernel.config.modular.service.SysConfigService;
|
||||
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
|
||||
|
@ -121,6 +122,29 @@ public class SysConfigController {
|
|||
return new SuccessResponseData(sysConfigService.findList(sysConfigParam));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统配置是否初始化的标志
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/8 17:20
|
||||
*/
|
||||
@GetResource(name = "获取系统配置是否初始化的标志", path = "/sysConfig/getInitConfigFlag")
|
||||
public ResponseData getInitConfigFlag() {
|
||||
return new SuccessResponseData(sysConfigService.getInitConfigFlag());
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化系统配置参数,用在系统第一次登录时
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/8 16:36
|
||||
*/
|
||||
@PostResource(name = "初始化系统配置参数,用在系统第一次登录时", path = "/sysConfig/initConfig")
|
||||
public ResponseData initConfig(@RequestBody ConfigInitRequest configInitRequest) {
|
||||
sysConfigService.initConfig(configInitRequest);
|
||||
return new SuccessResponseData();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.config.modular.service;
|
||||
|
||||
import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitRequest;
|
||||
import cn.stylefeng.roses.kernel.config.modular.entity.SysConfig;
|
||||
import cn.stylefeng.roses.kernel.config.modular.param.SysConfigParam;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
|
@ -96,4 +97,21 @@ public interface SysConfigService extends IService<SysConfig> {
|
|||
*/
|
||||
List<SysConfig> findList(SysConfigParam sysConfigParam);
|
||||
|
||||
/**
|
||||
* 初始化配置参数
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/8 16:48
|
||||
*/
|
||||
void initConfig(ConfigInitRequest configInitRequest);
|
||||
|
||||
/**
|
||||
* 获取配置是否初始化的标志
|
||||
*
|
||||
* @return true-系统已经初始化,false-系统没有初始化
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/8 17:20
|
||||
*/
|
||||
Boolean getInitConfigFlag();
|
||||
|
||||
}
|
||||
|
|
|
@ -25,11 +25,13 @@
|
|||
package cn.stylefeng.roses.kernel.config.modular.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
|
||||
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
|
||||
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
|
||||
import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitRequest;
|
||||
import cn.stylefeng.roses.kernel.config.modular.entity.SysConfig;
|
||||
import cn.stylefeng.roses.kernel.config.modular.mapper.SysConfigMapper;
|
||||
import cn.stylefeng.roses.kernel.config.modular.param.SysConfigParam;
|
||||
|
@ -37,6 +39,7 @@ import cn.stylefeng.roses.kernel.config.modular.service.SysConfigService;
|
|||
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
@ -46,6 +49,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 系统参数配置service接口实现类
|
||||
|
@ -134,6 +138,57 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void initConfig(ConfigInitRequest configInitRequest) {
|
||||
|
||||
if (configInitRequest == null || configInitRequest.getSysConfigs() == null) {
|
||||
throw new ConfigException(ConfigExceptionEnum.CONFIG_INIT_ERROR);
|
||||
}
|
||||
|
||||
// 添加系统已经初始化的配置
|
||||
Map<String, String> sysConfigs = configInitRequest.getSysConfigs();
|
||||
sysConfigs.put(RuleConstants.SYSTEM_CONFIG_INIT_FLAG_NAME, "true");
|
||||
|
||||
// 针对每个配置执行更新库和刷新缓存的操作
|
||||
for (Map.Entry<String, String> entry : sysConfigs.entrySet()) {
|
||||
String configCode = entry.getKey();
|
||||
String configValue = entry.getValue();
|
||||
|
||||
// 获取库数据库这条记录
|
||||
LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysConfig::getConfigCode, configCode);
|
||||
SysConfig sysConfig = this.getOne(wrapper, false);
|
||||
if (sysConfig == null) {
|
||||
continue;
|
||||
}
|
||||
sysConfig.setConfigValue(configValue);
|
||||
this.updateById(sysConfig);
|
||||
|
||||
// 更新缓存
|
||||
ConfigContext.me().putConfig(configCode, configValue);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getInitConfigFlag() {
|
||||
LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysConfig::getConfigCode, RuleConstants.SYSTEM_CONFIG_INIT_FLAG_NAME);
|
||||
SysConfig sysConfig = this.getOne(wrapper, false);
|
||||
|
||||
// 配置为空,还没初始化
|
||||
if (sysConfig == null) {
|
||||
return true;
|
||||
} else {
|
||||
String configValue = sysConfig.getConfigValue();
|
||||
if (StrUtil.isEmpty(configValue)) {
|
||||
return true;
|
||||
} else {
|
||||
return Convert.toBool(sysConfig.getConfigValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统参数配置
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue