Blame view

ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java 5.38 KB
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;

f7f27c96   YunaiV   开始迁移字典类型模块
6
  

46444bd0   RuoYi   RuoYi-Vue 1.0
7
8
  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.stereotype.Service;

cee572f2   RuoYi   若依 3.0
9
  import com.ruoyi.common.annotation.DataSource;

0d546e4b   RuoYi   参数管理支持缓存操作
10
  import com.ruoyi.common.constant.Constants;

46444bd0   RuoYi   RuoYi-Vue 1.0
11
  import com.ruoyi.common.constant.UserConstants;

cee572f2   RuoYi   若依 3.0
12
  import com.ruoyi.common.core.redis.RedisCache;

0d546e4b   RuoYi   参数管理支持缓存操作
13
  import com.ruoyi.common.core.text.Convert;

cee572f2   RuoYi   若依 3.0
14
  import com.ruoyi.common.enums.DataSourceType;

57b49dd5   RuoYi   限制系统内置参数不允许删除
15
  import com.ruoyi.common.exception.CustomException;

46444bd0   RuoYi   RuoYi-Vue 1.0
16
  import com.ruoyi.common.utils.StringUtils;

cee572f2   RuoYi   若依 3.0
17
18
19
  import com.ruoyi.system.domain.SysConfig;

  import com.ruoyi.system.mapper.SysConfigMapper;

  import com.ruoyi.system.service.ISysConfigService;

46444bd0   RuoYi   RuoYi-Vue 1.0
20
21
22
  

  /**

   * 参数配置 服务层实现

f7f27c96   YunaiV   开始迁移字典类型模块
23
   *

46444bd0   RuoYi   RuoYi-Vue 1.0
24
25
26
   * @author ruoyi

   */

  @Service

f7f27c96   YunaiV   开始迁移字典类型模块
27
  public class SysConfigServiceImpl implements ISysConfigService {

46444bd0   RuoYi   RuoYi-Vue 1.0
28
29
30
      @Autowired

      private SysConfigMapper configMapper;

  

0d546e4b   RuoYi   参数管理支持缓存操作
31
32
33
34
35
36
37
      @Autowired

      private RedisCache redisCache;

  

      /**

       * 项目启动时,初始化参数到缓存

       */

      @PostConstruct

f7f27c96   YunaiV   开始迁移字典类型模块
38
      public void init() {

0d546e4b   RuoYi   参数管理支持缓存操作
39
          List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());

f7f27c96   YunaiV   开始迁移字典类型模块
40
          for (SysConfig config : configsList) {

0d546e4b   RuoYi   参数管理支持缓存操作
41
42
43
44
              redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());

          }

      }

  

46444bd0   RuoYi   RuoYi-Vue 1.0
45
46
      /**

       * 查询参数配置信息

f7f27c96   YunaiV   开始迁移字典类型模块
47
       *

46444bd0   RuoYi   RuoYi-Vue 1.0
48
49
50
51
       * @param configId 参数配置ID

       * @return 参数配置信息

       */

      @Override

cee572f2   RuoYi   若依 3.0
52
      @DataSource(DataSourceType.MASTER)

f7f27c96   YunaiV   开始迁移字典类型模块
53
      public SysConfig selectConfigById(Long configId) {

46444bd0   RuoYi   RuoYi-Vue 1.0
54
55
56
57
58
59
60
          SysConfig config = new SysConfig();

          config.setConfigId(configId);

          return configMapper.selectConfig(config);

      }

  

      /**

       * 根据键名查询参数配置信息

f7f27c96   YunaiV   开始迁移字典类型模块
61
       *

46444bd0   RuoYi   RuoYi-Vue 1.0
62
63
64
65
       * @param configKey 参数key

       * @return 参数键值

       */

      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
66
      public String selectConfigByKey(String configKey) {

0d546e4b   RuoYi   参数管理支持缓存操作
67
          String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));

f7f27c96   YunaiV   开始迁移字典类型模块
68
          if (StringUtils.isNotEmpty(configValue)) {

0d546e4b   RuoYi   参数管理支持缓存操作
69
70
              return configValue;

          }

46444bd0   RuoYi   RuoYi-Vue 1.0
71
72
73
          SysConfig config = new SysConfig();

          config.setConfigKey(configKey);

          SysConfig retConfig = configMapper.selectConfig(config);

f7f27c96   YunaiV   开始迁移字典类型模块
74
          if (StringUtils.isNotNull(retConfig)) {

0d546e4b   RuoYi   参数管理支持缓存操作
75
76
77
78
              redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());

              return retConfig.getConfigValue();

          }

          return StringUtils.EMPTY;

46444bd0   RuoYi   RuoYi-Vue 1.0
79
80
81
82
      }

  

      /**

       * 查询参数配置列表

f7f27c96   YunaiV   开始迁移字典类型模块
83
       *

46444bd0   RuoYi   RuoYi-Vue 1.0
84
85
86
87
       * @param config 参数配置信息

       * @return 参数配置集合

       */

      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
88
      public List<SysConfig> selectConfigList(SysConfig config) {

46444bd0   RuoYi   RuoYi-Vue 1.0
89
90
91
92
93
          return configMapper.selectConfigList(config);

      }

  

      /**

       * 新增参数配置

f7f27c96   YunaiV   开始迁移字典类型模块
94
       *

46444bd0   RuoYi   RuoYi-Vue 1.0
95
96
97
98
       * @param config 参数配置信息

       * @return 结果

       */

      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
99
      public int insertConfig(SysConfig config) {

0d546e4b   RuoYi   参数管理支持缓存操作
100
          int row = configMapper.insertConfig(config);

f7f27c96   YunaiV   开始迁移字典类型模块
101
          if (row > 0) {

0d546e4b   RuoYi   参数管理支持缓存操作
102
103
104
              redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());

          }

          return row;

46444bd0   RuoYi   RuoYi-Vue 1.0
105
106
107
108
      }

  

      /**

       * 修改参数配置

f7f27c96   YunaiV   开始迁移字典类型模块
109
       *

46444bd0   RuoYi   RuoYi-Vue 1.0
110
111
112
113
       * @param config 参数配置信息

       * @return 结果

       */

      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
114
      public int updateConfig(SysConfig config) {

0d546e4b   RuoYi   参数管理支持缓存操作
115
          int row = configMapper.updateConfig(config);

f7f27c96   YunaiV   开始迁移字典类型模块
116
          if (row > 0) {

0d546e4b   RuoYi   参数管理支持缓存操作
117
118
119
              redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());

          }

          return row;

46444bd0   RuoYi   RuoYi-Vue 1.0
120
121
122
      }

  

      /**

0d546e4b   RuoYi   参数管理支持缓存操作
123
       * 批量删除参数信息

f7f27c96   YunaiV   开始迁移字典类型模块
124
       *

0d546e4b   RuoYi   参数管理支持缓存操作
125
       * @param configIds 需要删除的参数ID

46444bd0   RuoYi   RuoYi-Vue 1.0
126
127
128
       * @return 结果

       */

      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
129
130
      public int deleteConfigByIds(Long[] configIds) {

          for (Long configId : configIds) {

57b49dd5   RuoYi   限制系统内置参数不允许删除
131
              SysConfig config = selectConfigById(configId);

f7f27c96   YunaiV   开始迁移字典类型模块
132
              if (StringUtils.equals(UserConstants.YES, config.getConfigType())) {

57b49dd5   RuoYi   限制系统内置参数不允许删除
133
134
135
                  throw new CustomException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));

              }

          }

0d546e4b   RuoYi   参数管理支持缓存操作
136
          int count = configMapper.deleteConfigByIds(configIds);

f7f27c96   YunaiV   开始迁移字典类型模块
137
          if (count > 0) {

0d546e4b   RuoYi   参数管理支持缓存操作
138
139
140
141
              Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");

              redisCache.deleteObject(keys);

          }

          return count;

46444bd0   RuoYi   RuoYi-Vue 1.0
142
143
144
      }

  

      /**

0d546e4b   RuoYi   参数管理支持缓存操作
145
       * 清空缓存数据

bcee37b8   RuoYi   若依 1.1
146
       */

f39be3ab   Sxile   添加缺少的@ Override
147
      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
148
      public void clearCache() {

0d546e4b   RuoYi   参数管理支持缓存操作
149
150
          Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");

          redisCache.deleteObject(keys);

bcee37b8   RuoYi   若依 1.1
151
152
153
      }

  

      /**

46444bd0   RuoYi   RuoYi-Vue 1.0
154
       * 校验参数键名是否唯一

f7f27c96   YunaiV   开始迁移字典类型模块
155
       *

46444bd0   RuoYi   RuoYi-Vue 1.0
156
157
158
159
       * @param config 参数配置信息

       * @return 结果

       */

      @Override

f7f27c96   YunaiV   开始迁移字典类型模块
160
      public String checkConfigKeyUnique(SysConfig config) {

46444bd0   RuoYi   RuoYi-Vue 1.0
161
162
          Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();

          SysConfig info = configMapper.checkConfigKeyUnique(config.getConfigKey());

f7f27c96   YunaiV   开始迁移字典类型模块
163
          if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) {

46444bd0   RuoYi   RuoYi-Vue 1.0
164
165
166
167
              return UserConstants.NOT_UNIQUE;

          }

          return UserConstants.UNIQUE;

      }

0d546e4b   RuoYi   参数管理支持缓存操作
168
169
170
  

      /**

       * 设置cache key

f7f27c96   YunaiV   开始迁移字典类型模块
171
       *

0d546e4b   RuoYi   参数管理支持缓存操作
172
173
174
       * @param configKey 参数键

       * @return 缓存键key

       */

f7f27c96   YunaiV   开始迁移字典类型模块
175
      private String getCacheKey(String configKey) {

0d546e4b   RuoYi   参数管理支持缓存操作
176
177
          return Constants.SYS_CONFIG_KEY + configKey;

      }

46444bd0   RuoYi   RuoYi-Vue 1.0
178
  }