cee572f2
RuoYi
若依 3.0
|
1
|
package com.ruoyi.system.service.impl;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
2
|
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
3
|
import java.util.Collection;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
4
|
import java.util.List;
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
5
|
import javax.annotation.PostConstruct;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
6
7
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
|
cee572f2
RuoYi
若依 3.0
|
8
|
import com.ruoyi.common.annotation.DataSource;
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
9
|
import com.ruoyi.common.constant.Constants;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
10
|
import com.ruoyi.common.constant.UserConstants;
|
cee572f2
RuoYi
若依 3.0
|
11
|
import com.ruoyi.common.core.redis.RedisCache;
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
12
|
import com.ruoyi.common.core.text.Convert;
|
cee572f2
RuoYi
若依 3.0
|
13
|
import com.ruoyi.common.enums.DataSourceType;
|
57b49dd5
RuoYi
限制系统内置参数不允许删除
|
14
|
import com.ruoyi.common.exception.CustomException;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
15
|
import com.ruoyi.common.utils.StringUtils;
|
cee572f2
RuoYi
若依 3.0
|
16
17
18
|
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISysConfigService;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 参数配置 服务层实现
*
* @author ruoyi
*/
@Service
public class SysConfigServiceImpl implements ISysConfigService
{
@Autowired
private SysConfigMapper configMapper;
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
@Autowired
private RedisCache redisCache;
/**
* 项目启动时,初始化参数到缓存
*/
@PostConstruct
public void init()
{
List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
for (SysConfig config : configsList)
{
redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
}
}
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
47
48
49
50
51
52
53
|
/**
* 查询参数配置信息
*
* @param configId 参数配置ID
* @return 参数配置信息
*/
@Override
|
cee572f2
RuoYi
若依 3.0
|
54
|
@DataSource(DataSourceType.MASTER)
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
public SysConfig selectConfigById(Long configId)
{
SysConfig config = new SysConfig();
config.setConfigId(configId);
return configMapper.selectConfig(config);
}
/**
* 根据键名查询参数配置信息
*
* @param configKey 参数key
* @return 参数键值
*/
@Override
|
b508e6f2
RuoYi
新增用户导入
|
69
|
public String selectConfigByKey(String configKey)
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
70
|
{
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
71
72
73
74
75
|
String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
if (StringUtils.isNotEmpty(configValue))
{
return configValue;
}
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
76
77
78
|
SysConfig config = new SysConfig();
config.setConfigKey(configKey);
SysConfig retConfig = configMapper.selectConfig(config);
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
79
80
81
82
83
84
|
if (StringUtils.isNotNull(retConfig))
{
redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
return retConfig.getConfigValue();
}
return StringUtils.EMPTY;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
}
/**
* 查询参数配置列表
*
* @param config 参数配置信息
* @return 参数配置集合
*/
@Override
public List<SysConfig> selectConfigList(SysConfig config)
{
return configMapper.selectConfigList(config);
}
/**
* 新增参数配置
*
* @param config 参数配置信息
* @return 结果
*/
@Override
public int insertConfig(SysConfig config)
{
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
108
109
110
111
112
113
|
int row = configMapper.insertConfig(config);
if (row > 0)
{
redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
}
return row;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
114
115
116
117
118
119
120
121
122
123
124
|
}
/**
* 修改参数配置
*
* @param config 参数配置信息
* @return 结果
*/
@Override
public int updateConfig(SysConfig config)
{
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
125
126
127
128
129
130
|
int row = configMapper.updateConfig(config);
if (row > 0)
{
redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
}
return row;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
131
132
133
|
}
/**
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
134
|
* 批量删除参数信息
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
135
|
*
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
136
|
* @param configIds 需要删除的参数ID
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
137
138
139
|
* @return 结果
*/
@Override
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
140
|
public int deleteConfigByIds(Long[] configIds)
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
141
|
{
|
57b49dd5
RuoYi
限制系统内置参数不允许删除
|
142
143
144
145
146
147
148
149
|
for (Long configId : configIds)
{
SysConfig config = selectConfigById(configId);
if (StringUtils.equals(UserConstants.YES, config.getConfigType()))
{
throw new CustomException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
}
}
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
150
151
152
153
154
155
156
|
int count = configMapper.deleteConfigByIds(configIds);
if (count > 0)
{
Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
redisCache.deleteObject(keys);
}
return count;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
157
158
159
|
}
/**
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
160
|
* 清空缓存数据
|
bcee37b8
RuoYi
若依 1.1
|
161
|
*/
|
f39be3ab
Sxile
添加缺少的@ Override
|
162
|
@Override
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
163
|
public void clearCache()
|
bcee37b8
RuoYi
若依 1.1
|
164
|
{
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
165
166
|
Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
redisCache.deleteObject(keys);
|
bcee37b8
RuoYi
若依 1.1
|
167
168
169
|
}
/**
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
* 校验参数键名是否唯一
*
* @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;
}
|
0d546e4b
RuoYi
参数管理支持缓存操作
|
186
187
188
189
190
191
192
193
194
195
196
|
/**
* 设置cache key
*
* @param configKey 参数键
* @return 缓存键key
*/
private String getCacheKey(String configKey)
{
return Constants.SYS_CONFIG_KEY + configKey;
}
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
197
|
}
|