提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.config; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.pojo.PageResult; |
|
4 |
import com.iailab.module.infra.controller.admin.config.vo.ConfigPageReqVO; |
|
5 |
import com.iailab.module.infra.controller.admin.config.vo.ConfigSaveReqVO; |
|
6 |
import com.iailab.module.infra.convert.config.ConfigConvert; |
|
7 |
import com.iailab.module.infra.dal.dataobject.config.ConfigDO; |
|
8 |
import com.iailab.module.infra.dal.mysql.config.ConfigMapper; |
|
9 |
import com.iailab.module.infra.enums.config.ConfigTypeEnum; |
|
10 |
import com.google.common.annotations.VisibleForTesting; |
|
11 |
import lombok.extern.slf4j.Slf4j; |
d9f9ba
|
12 |
import org.springframework.data.redis.core.StringRedisTemplate; |
e7c126
|
13 |
import org.springframework.stereotype.Service; |
H |
14 |
import org.springframework.validation.annotation.Validated; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
d9f9ba
|
17 |
|
H |
18 |
import java.text.SimpleDateFormat; |
|
19 |
import java.util.Calendar; |
|
20 |
import java.util.Date; |
e7c126
|
21 |
|
H |
22 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
23 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.*; |
|
24 |
|
|
25 |
/** |
|
26 |
* 参数配置 Service 实现类 |
|
27 |
*/ |
|
28 |
@Service |
|
29 |
@Slf4j |
|
30 |
@Validated |
|
31 |
public class ConfigServiceImpl implements ConfigService { |
|
32 |
|
|
33 |
@Resource |
|
34 |
private ConfigMapper configMapper; |
d9f9ba
|
35 |
|
H |
36 |
@Resource |
|
37 |
private StringRedisTemplate stringRedisTemplate; |
e7c126
|
38 |
|
H |
39 |
@Override |
|
40 |
public Long createConfig(ConfigSaveReqVO createReqVO) { |
|
41 |
// 校验参数配置 key 的唯一性 |
|
42 |
validateConfigKeyUnique(null, createReqVO.getKey()); |
|
43 |
|
|
44 |
// 插入参数配置 |
|
45 |
ConfigDO config = ConfigConvert.INSTANCE.convert(createReqVO); |
|
46 |
config.setType(ConfigTypeEnum.CUSTOM.getType()); |
|
47 |
configMapper.insert(config); |
|
48 |
return config.getId(); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public void updateConfig(ConfigSaveReqVO updateReqVO) { |
|
53 |
// 校验自己存在 |
|
54 |
validateConfigExists(updateReqVO.getId()); |
|
55 |
// 校验参数配置 key 的唯一性 |
|
56 |
validateConfigKeyUnique(updateReqVO.getId(), updateReqVO.getKey()); |
|
57 |
|
|
58 |
// 更新参数配置 |
|
59 |
ConfigDO updateObj = ConfigConvert.INSTANCE.convert(updateReqVO); |
|
60 |
configMapper.updateById(updateObj); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public void deleteConfig(Long id) { |
|
65 |
// 校验配置存在 |
|
66 |
ConfigDO config = validateConfigExists(id); |
|
67 |
// 内置配置,不允许删除 |
|
68 |
if (ConfigTypeEnum.SYSTEM.getType().equals(config.getType())) { |
|
69 |
throw exception(CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE); |
|
70 |
} |
|
71 |
// 删除 |
|
72 |
configMapper.deleteById(id); |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
public ConfigDO getConfig(Long id) { |
|
77 |
return configMapper.selectById(id); |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public ConfigDO getConfigByKey(String key) { |
|
82 |
return configMapper.selectByKey(key); |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public PageResult<ConfigDO> getConfigPage(ConfigPageReqVO pageReqVO) { |
|
87 |
return configMapper.selectPage(pageReqVO); |
|
88 |
} |
|
89 |
|
|
90 |
@VisibleForTesting |
|
91 |
public ConfigDO validateConfigExists(Long id) { |
|
92 |
if (id == null) { |
|
93 |
return null; |
|
94 |
} |
|
95 |
ConfigDO config = configMapper.selectById(id); |
|
96 |
if (config == null) { |
|
97 |
throw exception(CONFIG_NOT_EXISTS); |
|
98 |
} |
|
99 |
return config; |
|
100 |
} |
|
101 |
|
|
102 |
@VisibleForTesting |
|
103 |
public void validateConfigKeyUnique(Long id, String key) { |
|
104 |
ConfigDO config = configMapper.selectByKey(key); |
|
105 |
if (config == null) { |
|
106 |
return; |
|
107 |
} |
|
108 |
// 如果 id 为空,说明不用比较是否为相同 id 的参数配置 |
|
109 |
if (id == null) { |
|
110 |
throw exception(CONFIG_KEY_DUPLICATE); |
|
111 |
} |
|
112 |
if (!config.getId().equals(id)) { |
|
113 |
throw exception(CONFIG_KEY_DUPLICATE); |
|
114 |
} |
|
115 |
} |
|
116 |
|
d9f9ba
|
117 |
@Override |
H |
118 |
public String getValue(String configCode) { |
|
119 |
String paramValue = stringRedisTemplate.opsForValue().get(configCode); |
|
120 |
if(paramValue == null){ |
|
121 |
ConfigDO configDO = configMapper.selectByKey(configCode); |
|
122 |
paramValue = configDO.getValue(); |
|
123 |
stringRedisTemplate.opsForValue().set(configCode, paramValue); |
|
124 |
} |
|
125 |
return paramValue; |
|
126 |
} |
|
127 |
|
e7c126
|
128 |
} |