懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.system.service.impl;
2
3 import java.util.Collection;
4 import java.util.List;
5 import javax.annotation.PostConstruct;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8 import com.jcdm.common.annotation.DataSource;
9 import com.jcdm.common.constant.CacheConstants;
10 import com.jcdm.common.constant.UserConstants;
11 import com.jcdm.common.core.redis.RedisCache;
12 import com.jcdm.common.core.text.Convert;
13 import com.jcdm.common.enums.DataSourceType;
14 import com.jcdm.common.exception.ServiceException;
15 import com.jcdm.common.utils.StringUtils;
16 import com.jcdm.system.domain.SysConfig;
17 import com.jcdm.system.mapper.SysConfigMapper;
18 import com.jcdm.system.service.ISysConfigService;
19
20 /**
21  * 参数配置 服务层实现
22  * 
23  * @author jc
24  */
25 @Service
26 public class SysConfigServiceImpl implements ISysConfigService
27 {
28     @Autowired
29     private SysConfigMapper configMapper;
30
31     @Autowired
32     private RedisCache redisCache;
33
34     /**
35      * 项目启动时,初始化参数到缓存
36      */
37     @PostConstruct
38     public void init()
39     {
40         loadingConfigCache();
41     }
42
43     /**
44      * 查询参数配置信息
45      * 
46      * @param configId 参数配置ID
47      * @return 参数配置信息
48      */
49     @Override
50     @DataSource(DataSourceType.MASTER)
51     public SysConfig selectConfigById(Long configId)
52     {
53         SysConfig config = new SysConfig();
54         config.setConfigId(configId);
55         return configMapper.selectConfig(config);
56     }
57
58     /**
59      * 根据键名查询参数配置信息
60      * 
61      * @param configKey 参数key
62      * @return 参数键值
63      */
64     @Override
65     public String selectConfigByKey(String configKey)
66     {
67         String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
68         if (StringUtils.isNotEmpty(configValue))
69         {
70             return configValue;
71         }
72         SysConfig config = new SysConfig();
73         config.setConfigKey(configKey);
74         SysConfig retConfig = configMapper.selectConfig(config);
75         if (StringUtils.isNotNull(retConfig))
76         {
77             redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
78             return retConfig.getConfigValue();
79         }
80         return StringUtils.EMPTY;
81     }
82
83     /**
84      * 获取验证码开关
85      * 
86      * @return true开启,false关闭
87      */
88     @Override
89     public boolean selectCaptchaEnabled()
90     {
91         String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled");
92         if (StringUtils.isEmpty(captchaEnabled))
93         {
94             return true;
95         }
96         return Convert.toBool(captchaEnabled);
97     }
98
99     /**
100      * 查询参数配置列表
101      * 
102      * @param config 参数配置信息
103      * @return 参数配置集合
104      */
105     @Override
106     public List<SysConfig> selectConfigList(SysConfig config)
107     {
108         return configMapper.selectConfigList(config);
109     }
110
111     /**
112      * 新增参数配置
113      * 
114      * @param config 参数配置信息
115      * @return 结果
116      */
117     @Override
118     public int insertConfig(SysConfig config)
119     {
120         int row = configMapper.insertConfig(config);
121         if (row > 0)
122         {
123             redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
124         }
125         return row;
126     }
127
128     /**
129      * 修改参数配置
130      * 
131      * @param config 参数配置信息
132      * @return 结果
133      */
134     @Override
135     public int updateConfig(SysConfig config)
136     {
137         SysConfig temp = configMapper.selectConfigById(config.getConfigId());
138         if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey()))
139         {
140             redisCache.deleteObject(getCacheKey(temp.getConfigKey()));
141         }
142
143         int row = configMapper.updateConfig(config);
144         if (row > 0)
145         {
146             redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
147         }
148         return row;
149     }
150
151     /**
152      * 批量删除参数信息
153      * 
154      * @param configIds 需要删除的参数ID
155      */
156     @Override
157     public void deleteConfigByIds(Long[] configIds)
158     {
159         for (Long configId : configIds)
160         {
161             SysConfig config = selectConfigById(configId);
162             if (StringUtils.equals(UserConstants.YES, config.getConfigType()))
163             {
164                 throw new ServiceException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
165             }
166             configMapper.deleteConfigById(configId);
167             redisCache.deleteObject(getCacheKey(config.getConfigKey()));
168         }
169     }
170
171     /**
172      * 加载参数缓存数据
173      */
174     @Override
175     public void loadingConfigCache()
176     {
177         List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
178         for (SysConfig config : configsList)
179         {
180             redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
181         }
182     }
183
184     /**
185      * 清空参数缓存数据
186      */
187     @Override
188     public void clearConfigCache()
189     {
190         Collection<String> keys = redisCache.keys(CacheConstants.SYS_CONFIG_KEY + "*");
191         redisCache.deleteObject(keys);
192     }
193
194     /**
195      * 重置参数缓存数据
196      */
197     @Override
198     public void resetConfigCache()
199     {
200         clearConfigCache();
201         loadingConfigCache();
202     }
203
204     /**
205      * 校验参数键名是否唯一
206      * 
207      * @param config 参数配置信息
208      * @return 结果
209      */
210     @Override
211     public boolean checkConfigKeyUnique(SysConfig config)
212     {
213         Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
214         SysConfig info = configMapper.checkConfigKeyUnique(config.getConfigKey());
215         if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue())
216         {
217             return UserConstants.NOT_UNIQUE;
218         }
219         return UserConstants.UNIQUE;
220     }
221
222     /**
223      * 设置cache key
224      * 
225      * @param configKey 参数键
226      * @return 缓存键key
227      */
228     private String getCacheKey(String configKey)
229     {
230         return CacheConstants.SYS_CONFIG_KEY + configKey;
231     }
232 }