updateConfigByKey支持乐观锁

This commit is contained in:
dataprince 2024-01-19 11:40:11 +08:00
parent 6a6bf19db5
commit ee8f101807
5 changed files with 17 additions and 20 deletions

View File

@ -72,7 +72,7 @@ public class SysConfigController extends BaseController {
*/
@SaCheckPermission("system:config:query")
@GetMapping(value = "/configKey/{configKey}")
public R<String> getConfigKey(@PathVariable String configKey) {
public R<SysConfigVo> getConfigKey(@PathVariable String configKey) {
return R.ok(configService.selectConfigByKey(configKey),"操作成功");
}

View File

@ -47,7 +47,7 @@ public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo
private final StringBuilder failureMsg = new StringBuilder();
public SysUserImportListener(Boolean isUpdateSupport) {
String initPassword = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("sys.user.initPassword");
String initPassword = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("sys.user.initPassword").getConfigValue();
this.userService = SpringUtils.getBean(ISysUserService.class);
this.deptService = SpringUtils.getBean(ISysDeptService.class);
this.password = BCrypt.hashpw(initPassword);

View File

@ -30,7 +30,7 @@ public interface ISysConfigService extends IBaseService<SysConfig>
* @param configKey 参数键名
* @return 参数键值
*/
String selectConfigByKey(String configKey);
SysConfigVo selectConfigByKey(String configKey);
/**
* 获取验证码开关

View File

@ -14,6 +14,7 @@ import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.redis.utils.CacheUtils;
import com.ruoyi.system.domain.bo.SysConfigBo;
import com.ruoyi.system.domain.vo.SysConfigVo;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
@ -38,6 +39,9 @@ import static com.ruoyi.system.domain.table.SysConfigTableDef.SYS_CONFIG;
@Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService, ConfigService {
@Resource
private SysConfigMapper configMapper;
@Override
public QueryWrapper query() {
return super.query().from(SYS_CONFIG);
@ -62,12 +66,8 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
*/
@Cacheable(cacheNames = CacheNames.SYS_CONFIG, key = "#configKey")
@Override
public String selectConfigByKey(String configKey) {
SysConfig retConfig = this.getOne(query().where(SYS_CONFIG.CONFIG_KEY.eq(configKey)));
if (StringUtils.isNotNull(retConfig)) {
return retConfig.getConfigValue();
}
return StringUtils.EMPTY;
public SysConfigVo selectConfigByKey(String configKey) {
return this.getOneAs(query().where(SYS_CONFIG.CONFIG_KEY.eq(configKey)), SysConfigVo.class);
}
/**
@ -77,7 +77,7 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
*/
@Override
public boolean selectCaptchaEnabled() {
String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled");
String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled").getConfigValue();
if (StringUtils.isEmpty(captchaEnabled)) {
return true;
}
@ -154,7 +154,7 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
/**
* 根据Key修改参数配置
*
* UPDATE "sys_config" SET "config_key" = 'sys.oss.previewListResource' , "config_value" = 'false' , "update_by" = 1 , "update_time" = '2024-01-19 11:20:01' , "version" = "version" + 1 WHERE "tenant_id" = 0 AND "config_key" = 'sys.oss.previewListResource' AND "version" = 0
* @param configBo 参数配置信息
* @return true 更新成功false 更新失败
*/
@ -162,13 +162,10 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
@Override
public boolean updateConfigByKey(SysConfigBo configBo) {
SysConfig config = MapstructUtils.convert(configBo, SysConfig.class);
QueryWrapper queryWrapper = query().where(SYS_CONFIG.CONFIG_KEY.eq(config.getConfigKey()));
SysConfig sysConfig = this.getOne(queryWrapper);
SysConfig configUpdate = new SysConfig();
configUpdate.setConfigId(sysConfig.getConfigId());
configUpdate.setConfigValue(config.getConfigValue());
return this.updateById(configUpdate);
QueryWrapper queryWrapper = query().where(SYS_CONFIG.CONFIG_KEY.eq(config.getConfigKey()))
.and(SYS_CONFIG.VERSION.eq(config.getVersion()));
int updatedRows = configMapper.updateByQuery(config, queryWrapper);
return updatedRows > 0;
}
/**
@ -222,6 +219,6 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
*/
@Override
public String getConfigValue(String configKey) {
return SpringUtils.getAopProxy(this).selectConfigByKey(configKey);
return SpringUtils.getAopProxy(this).selectConfigByKey(configKey).getConfigValue();
}
}

View File

@ -688,7 +688,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
String initPassword = configService.selectConfigByKey("sys.user.initPassword");
String initPassword = configService.selectConfigByKey("sys.user.initPassword").getConfigValue();
for (SysUser user : userList) {
try {
// 验证是否存在这个用户