From dd9ef8431bf57081829b36d0564277971c001f48 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期三, 21 十二月 2022 21:33:29 +0800 Subject: [PATCH] fix 修复 根据key更新参数配置报null问题 --- ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java | 453 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 227 insertions(+), 226 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java index 1017d23..c39ec74 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java @@ -1,226 +1,227 @@ -package com.ruoyi.system.service.impl; - -import java.util.Collection; -import java.util.List; -import javax.annotation.PostConstruct; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import com.ruoyi.common.annotation.DataSource; -import com.ruoyi.common.constant.CacheConstants; -import com.ruoyi.common.constant.UserConstants; -import com.ruoyi.common.core.redis.RedisCache; -import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.enums.DataSourceType; -import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.system.domain.SysConfig; -import com.ruoyi.system.mapper.SysConfigMapper; -import com.ruoyi.system.service.ISysConfigService; - -/** - * 鍙傛暟閰嶇疆 鏈嶅姟灞傚疄鐜� - * - * @author ruoyi - */ -@Service -public class SysConfigServiceImpl implements ISysConfigService -{ - @Autowired - private SysConfigMapper configMapper; - - @Autowired - private RedisCache redisCache; - - /** - * 椤圭洰鍚姩鏃讹紝鍒濆鍖栧弬鏁板埌缂撳瓨 - */ - @PostConstruct - public void init() - { - loadingConfigCache(); - } - - /** - * 鏌ヨ鍙傛暟閰嶇疆淇℃伅 - * - * @param configId 鍙傛暟閰嶇疆ID - * @return 鍙傛暟閰嶇疆淇℃伅 - */ - @Override - @DataSource(DataSourceType.MASTER) - public SysConfig selectConfigById(Long configId) - { - SysConfig config = new SysConfig(); - config.setConfigId(configId); - return configMapper.selectConfig(config); - } - - /** - * 鏍规嵁閿悕鏌ヨ鍙傛暟閰嶇疆淇℃伅 - * - * @param configKey 鍙傛暟key - * @return 鍙傛暟閿�� - */ - @Override - public String selectConfigByKey(String configKey) - { - String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey))); - if (StringUtils.isNotEmpty(configValue)) - { - return configValue; - } - SysConfig config = new SysConfig(); - config.setConfigKey(configKey); - SysConfig retConfig = configMapper.selectConfig(config); - if (StringUtils.isNotNull(retConfig)) - { - redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue()); - return retConfig.getConfigValue(); - } - return StringUtils.EMPTY; - } - - /** - * 鑾峰彇楠岃瘉鐮佸紑鍏� - * - * @return true寮�鍚紝false鍏抽棴 - */ - @Override - public boolean selectCaptchaOnOff() - { - String captchaOnOff = selectConfigByKey("sys.account.captchaOnOff"); - if (StringUtils.isEmpty(captchaOnOff)) - { - return true; - } - return Convert.toBool(captchaOnOff); - } - - /** - * 鏌ヨ鍙傛暟閰嶇疆鍒楄〃 - * - * @param config 鍙傛暟閰嶇疆淇℃伅 - * @return 鍙傛暟閰嶇疆闆嗗悎 - */ - @Override - public List<SysConfig> selectConfigList(SysConfig config) - { - return configMapper.selectConfigList(config); - } - - /** - * 鏂板鍙傛暟閰嶇疆 - * - * @param config 鍙傛暟閰嶇疆淇℃伅 - * @return 缁撴灉 - */ - @Override - public int insertConfig(SysConfig config) - { - int row = configMapper.insertConfig(config); - if (row > 0) - { - redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); - } - return row; - } - - /** - * 淇敼鍙傛暟閰嶇疆 - * - * @param config 鍙傛暟閰嶇疆淇℃伅 - * @return 缁撴灉 - */ - @Override - public int updateConfig(SysConfig config) - { - int row = configMapper.updateConfig(config); - if (row > 0) - { - redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); - } - return row; - } - - /** - * 鎵归噺鍒犻櫎鍙傛暟淇℃伅 - * - * @param configIds 闇�瑕佸垹闄ょ殑鍙傛暟ID - */ - @Override - public void deleteConfigByIds(Long[] configIds) - { - for (Long configId : configIds) - { - SysConfig config = selectConfigById(configId); - if (StringUtils.equals(UserConstants.YES, config.getConfigType())) - { - throw new ServiceException(String.format("鍐呯疆鍙傛暟銆�%1$s銆戜笉鑳藉垹闄� ", config.getConfigKey())); - } - configMapper.deleteConfigById(configId); - redisCache.deleteObject(getCacheKey(config.getConfigKey())); - } - } - - /** - * 鍔犺浇鍙傛暟缂撳瓨鏁版嵁 - */ - @Override - public void loadingConfigCache() - { - List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig()); - for (SysConfig config : configsList) - { - redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue()); - } - } - - /** - * 娓呯┖鍙傛暟缂撳瓨鏁版嵁 - */ - @Override - public void clearConfigCache() - { - Collection<String> keys = redisCache.keys(CacheConstants.SYS_CONFIG_KEY + "*"); - redisCache.deleteObject(keys); - } - - /** - * 閲嶇疆鍙傛暟缂撳瓨鏁版嵁 - */ - @Override - public void resetConfigCache() - { - clearConfigCache(); - loadingConfigCache(); - } - - /** - * 鏍¢獙鍙傛暟閿悕鏄惁鍞竴 - * - * @param config 鍙傛暟閰嶇疆淇℃伅 - * @return 缁撴灉 - */ - @Override - public String checkConfigKeyUnique(SysConfig config) - { - Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId(); - SysConfig info = configMapper.checkConfigKeyUnique(config.getConfigKey()); - if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) - { - return UserConstants.NOT_UNIQUE; - } - return UserConstants.UNIQUE; - } - - /** - * 璁剧疆cache key - * - * @param configKey 鍙傛暟閿� - * @return 缂撳瓨閿甼ey - */ - private String getCacheKey(String configKey) - { - return CacheConstants.SYS_CONFIG_KEY + configKey; - } -} +package com.ruoyi.system.service.impl; + +import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.ruoyi.common.constant.CacheNames; +import com.ruoyi.common.constant.UserConstants; +import com.ruoyi.common.core.domain.PageQuery; +import com.ruoyi.common.core.page.TableDataInfo; +import com.ruoyi.common.core.service.ConfigService; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.redis.CacheUtils; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.system.domain.SysConfig; +import com.ruoyi.system.mapper.SysConfigMapper; +import com.ruoyi.system.service.ISysConfigService; +import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * 鍙傛暟閰嶇疆 鏈嶅姟灞傚疄鐜� + * + * @author Lion Li + */ +@RequiredArgsConstructor +@Service +public class SysConfigServiceImpl implements ISysConfigService, ConfigService { + + private final SysConfigMapper baseMapper; + + @Override + public TableDataInfo<SysConfig> selectPageConfigList(SysConfig config, PageQuery pageQuery) { + Map<String, Object> params = config.getParams(); + LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<SysConfig>() + .like(StringUtils.isNotBlank(config.getConfigName()), SysConfig::getConfigName, config.getConfigName()) + .eq(StringUtils.isNotBlank(config.getConfigType()), SysConfig::getConfigType, config.getConfigType()) + .like(StringUtils.isNotBlank(config.getConfigKey()), SysConfig::getConfigKey, config.getConfigKey()) + .between(params.get("beginTime") != null && params.get("endTime") != null, + SysConfig::getCreateTime, params.get("beginTime"), params.get("endTime")); + Page<SysConfig> page = baseMapper.selectPage(pageQuery.build(), lqw); + return TableDataInfo.build(page); + } + + /** + * 鏌ヨ鍙傛暟閰嶇疆淇℃伅 + * + * @param configId 鍙傛暟閰嶇疆ID + * @return 鍙傛暟閰嶇疆淇℃伅 + */ + @Override + @DS("master") + public SysConfig selectConfigById(Long configId) { + return baseMapper.selectById(configId); + } + + /** + * 鏍规嵁閿悕鏌ヨ鍙傛暟閰嶇疆淇℃伅 + * + * @param configKey 鍙傛暟key + * @return 鍙傛暟閿�� + */ + @Cacheable(cacheNames = CacheNames.SYS_CONFIG, key = "#configKey") + @Override + public String selectConfigByKey(String configKey) { + SysConfig retConfig = baseMapper.selectOne(new LambdaQueryWrapper<SysConfig>() + .eq(SysConfig::getConfigKey, configKey)); + if (ObjectUtil.isNotNull(retConfig)) { + return retConfig.getConfigValue(); + } + return StringUtils.EMPTY; + } + + /** + * 鑾峰彇楠岃瘉鐮佸紑鍏� + * + * @return true寮�鍚紝false鍏抽棴 + */ + @Override + public boolean selectCaptchaEnabled() { + String captchaEnabled = SpringUtils.getAopProxy(this).selectConfigByKey("sys.account.captchaEnabled"); + if (StringUtils.isEmpty(captchaEnabled)) { + return true; + } + return Convert.toBool(captchaEnabled); + } + + /** + * 鏌ヨ鍙傛暟閰嶇疆鍒楄〃 + * + * @param config 鍙傛暟閰嶇疆淇℃伅 + * @return 鍙傛暟閰嶇疆闆嗗悎 + */ + @Override + public List<SysConfig> selectConfigList(SysConfig config) { + Map<String, Object> params = config.getParams(); + LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<SysConfig>() + .like(StringUtils.isNotBlank(config.getConfigName()), SysConfig::getConfigName, config.getConfigName()) + .eq(StringUtils.isNotBlank(config.getConfigType()), SysConfig::getConfigType, config.getConfigType()) + .like(StringUtils.isNotBlank(config.getConfigKey()), SysConfig::getConfigKey, config.getConfigKey()) + .between(params.get("beginTime") != null && params.get("endTime") != null, + SysConfig::getCreateTime, params.get("beginTime"), params.get("endTime")); + return baseMapper.selectList(lqw); + } + + /** + * 鏂板鍙傛暟閰嶇疆 + * + * @param config 鍙傛暟閰嶇疆淇℃伅 + * @return 缁撴灉 + */ + @CachePut(cacheNames = CacheNames.SYS_CONFIG, key = "#config.configKey") + @Override + public String insertConfig(SysConfig config) { + int row = baseMapper.insert(config); + if (row > 0) { + return config.getConfigValue(); + } + throw new ServiceException("鎿嶄綔澶辫触"); + } + + /** + * 淇敼鍙傛暟閰嶇疆 + * + * @param config 鍙傛暟閰嶇疆淇℃伅 + * @return 缁撴灉 + */ + @CachePut(cacheNames = CacheNames.SYS_CONFIG, key = "#config.configKey") + @Override + public String updateConfig(SysConfig config) { + int row = 0; + if (config.getConfigId() != null) { + SysConfig temp = baseMapper.selectById(config.getConfigId()); + if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey())) { + CacheUtils.evict(CacheNames.SYS_CONFIG, temp.getConfigKey()); + } + row = baseMapper.updateById(config); + } else { + row = baseMapper.update(config, new LambdaQueryWrapper<SysConfig>() + .eq(SysConfig::getConfigKey, config.getConfigKey())); + } + if (row > 0) { + return config.getConfigValue(); + } + throw new ServiceException("鎿嶄綔澶辫触"); + } + + /** + * 鎵归噺鍒犻櫎鍙傛暟淇℃伅 + * + * @param configIds 闇�瑕佸垹闄ょ殑鍙傛暟ID + */ + @Override + public void deleteConfigByIds(Long[] configIds) { + for (Long configId : configIds) { + SysConfig config = selectConfigById(configId); + if (StringUtils.equals(UserConstants.YES, config.getConfigType())) { + throw new ServiceException(String.format("鍐呯疆鍙傛暟銆�%1$s銆戜笉鑳藉垹闄� ", config.getConfigKey())); + } + CacheUtils.evict(CacheNames.SYS_CONFIG, config.getConfigKey()); + } + baseMapper.deleteBatchIds(Arrays.asList(configIds)); + } + + /** + * 鍔犺浇鍙傛暟缂撳瓨鏁版嵁 + */ + @Override + public void loadingConfigCache() { + List<SysConfig> configsList = selectConfigList(new SysConfig()); + configsList.forEach(config -> + CacheUtils.put(CacheNames.SYS_CONFIG, config.getConfigKey(), config.getConfigValue())); + } + + /** + * 娓呯┖鍙傛暟缂撳瓨鏁版嵁 + */ + @Override + public void clearConfigCache() { + CacheUtils.clear(CacheNames.SYS_CONFIG); + } + + /** + * 閲嶇疆鍙傛暟缂撳瓨鏁版嵁 + */ + @Override + public void resetConfigCache() { + clearConfigCache(); + loadingConfigCache(); + } + + /** + * 鏍¢獙鍙傛暟閿悕鏄惁鍞竴 + * + * @param config 鍙傛暟閰嶇疆淇℃伅 + * @return 缁撴灉 + */ + @Override + public String checkConfigKeyUnique(SysConfig config) { + Long configId = ObjectUtil.isNull(config.getConfigId()) ? -1L : config.getConfigId(); + SysConfig info = baseMapper.selectOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getConfigKey, config.getConfigKey())); + if (ObjectUtil.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) { + return UserConstants.NOT_UNIQUE; + } + return UserConstants.UNIQUE; + } + + /** + * 鏍规嵁鍙傛暟 key 鑾峰彇鍙傛暟鍊� + * + * @param configKey 鍙傛暟 key + * @return 鍙傛暟鍊� + */ + @Override + public String getConfigValue(String configKey) { + return SpringUtils.getAopProxy(this).selectConfigByKey(configKey); + } + +} -- Gitblit v1.9.3