SysConfigServiceImpl.java 5.0 KB

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