| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- package com.railway.system.service.impl;
- import com.railway.common.annotation.DataSource;
- import com.railway.common.core.redis.RedisCache;
- import com.railway.common.enums.DataSourceType;
- import java.util.Collection;
- import java.util.List;
- import javax.annotation.PostConstruct;
- import org.springframework.stereotype.Service;
- import com.railway.common.constant.Constants;
- import com.railway.common.constant.UserConstants;
- import com.railway.common.core.text.Convert;
- import com.railway.common.exception.ServiceException;
- import com.railway.common.utils.StringUtils;
- import com.railway.system.domain.SysConfig;
- import com.railway.system.mapper.SysConfigMapper;
- import com.railway.system.service.ISysConfigService;
- /**
- * 参数配置 服务层实现
- *
- * @author railway
- */
- @Service
- public class SysConfigServiceImpl implements ISysConfigService {
- private final SysConfigMapper configMapper;
- private final RedisCache redisCache;
- public SysConfigServiceImpl(SysConfigMapper configMapper,
- RedisCache redisCache) {
- this.configMapper = configMapper;
- this.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 selectRegisterUser() {
- return getBoolValue("sys.account.registerUser");
- }
- /**
- * 获取验证码开关
- *
- * @return true开启,false关闭
- */
- @Override
- public boolean selectCaptchaOnOff() {
- return getBoolValue("sys.account.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(Constants.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() != configId) {
- return UserConstants.NOT_UNIQUE;
- }
- return UserConstants.UNIQUE;
- }
- /**
- * 设置cache key
- *
- * @param configKey 参数键
- * @return 缓存键key
- */
- private String getCacheKey(String configKey) {
- return Constants.SYS_CONFIG_KEY + configKey;
- }
- /**
- * 获取Bool类型的系统配置
- * @param configKey 参数键
- * @return true开启,false关闭
- */
- private boolean getBoolValue(String configKey){
- String captchaOnOff = selectConfigByKey(configKey);
- if (StringUtils.isEmpty(captchaOnOff)) {
- return true;
- }
- return Convert.toBool(captchaOnOff);
- }
- }
|