SysConfigServiceImpl.java 5.7 KB

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