Browse Source

行政区域增加redis缓存

guobing 2 weeks ago
parent
commit
589c6cedf1

+ 2 - 2
delivery-module-system/delivery-module-system-biz/src/main/java/com/unis/module/system/controller/admin/arealevel/AreaLevelController.java

@@ -77,7 +77,7 @@ public class AreaLevelController {
77
     @Operation(summary = "获得行政区划")
77
     @Operation(summary = "获得行政区划")
78
     @Parameter(name = "id", description = "编号", required = true, example = "1024")
78
     @Parameter(name = "id", description = "编号", required = true, example = "1024")
79
     @PreAuthorize("@ss.hasPermission('system:area-level:query')")
79
     @PreAuthorize("@ss.hasPermission('system:area-level:query')")
80
-    public CommonResult<AreaLevelRespVO> getAreaLevel(@RequestParam("id") Integer id) {
80
+    public CommonResult<AreaLevelRespVO> getAreaLevel(@RequestParam("id") Long id) {
81
         AreaLevelDO areaLevel = areaLevelService.getAreaLevel(id);
81
         AreaLevelDO areaLevel = areaLevelService.getAreaLevel(id);
82
         return success(BeanUtils.toBean(areaLevel, AreaLevelRespVO.class));
82
         return success(BeanUtils.toBean(areaLevel, AreaLevelRespVO.class));
83
     }
83
     }
@@ -146,7 +146,7 @@ public class AreaLevelController {
146
             return error(400, "无此用户");
146
             return error(400, "无此用户");
147
         }
147
         }
148
 
148
 
149
-        return success(areaLevelService.getLoginUserXzqhConcise(user.getAreaCode(),user));
149
+        return success(areaLevelService.getLoginUserXzqhConcise(user.getAreaCode()));
150
 
150
 
151
     }
151
     }
152
 
152
 

+ 2 - 2
delivery-module-system/delivery-module-system-biz/src/main/java/com/unis/module/system/service/arealevel/AreaLevelService.java

@@ -45,7 +45,7 @@ public interface AreaLevelService {
45
      * @return 行政区划
45
      * @return 行政区划
46
      */
46
      */
47
 
47
 
48
-    AreaLevelDO getAreaLevel(Integer id);
48
+    AreaLevelDO getAreaLevel(Long id);
49
 
49
 
50
 
50
 
51
     /**
51
     /**
@@ -68,5 +68,5 @@ public interface AreaLevelService {
68
     List<AreaLevelDO> getAreaListByParentCode(String parentCode);
68
     List<AreaLevelDO> getAreaListByParentCode(String parentCode);
69
 
69
 
70
     List<AreaLevelDO> getAreaLevelListAll(AdminUserDO user);
70
     List<AreaLevelDO> getAreaLevelListAll(AdminUserDO user);
71
-    List<AreaLevelDO> getLoginUserXzqhConcise(String areaCode,AdminUserDO user);
71
+    List<AreaLevelDO> getLoginUserXzqhConcise(String areaCode);
72
 }
72
 }

+ 170 - 46
delivery-module-system/delivery-module-system-biz/src/main/java/com/unis/module/system/service/arealevel/AreaLevelServiceImpl.java

@@ -6,6 +6,7 @@ import com.unis.framework.security.core.LoginUser;
6
 import com.unis.framework.security.core.util.SecurityFrameworkUtils;
6
 import com.unis.framework.security.core.util.SecurityFrameworkUtils;
7
 import com.unis.module.system.dal.dataobject.user.AdminUserDO;
7
 import com.unis.module.system.dal.dataobject.user.AdminUserDO;
8
 import lombok.extern.slf4j.Slf4j;
8
 import lombok.extern.slf4j.Slf4j;
9
+import org.springframework.data.redis.core.RedisTemplate;
9
 import org.springframework.stereotype.Service;
10
 import org.springframework.stereotype.Service;
10
 
11
 
11
 import javax.annotation.Resource;
12
 import javax.annotation.Resource;
@@ -14,6 +15,7 @@ import org.springframework.validation.annotation.Validated;
14
 import org.springframework.transaction.annotation.Transactional;
15
 import org.springframework.transaction.annotation.Transactional;
15
 
16
 
16
 import java.util.*;
17
 import java.util.*;
18
+import java.util.stream.Collectors;
17
 
19
 
18
 import com.unis.module.system.controller.admin.arealevel.vo.*;
20
 import com.unis.module.system.controller.admin.arealevel.vo.*;
19
 import com.unis.module.system.dal.dataobject.arealevel.AreaLevelDO;
21
 import com.unis.module.system.dal.dataobject.arealevel.AreaLevelDO;
@@ -39,31 +41,97 @@ public class AreaLevelServiceImpl implements AreaLevelService {
39
     @Resource
41
     @Resource
40
     private AreaLevelMapper areaLevelMapper;
42
     private AreaLevelMapper areaLevelMapper;
41
 
43
 
44
+    @Resource
45
+    private RedisTemplate<String, Object> redisTemplate;
46
+
47
+    private static final String ALL_AREA_LEVEL_KEY = "all_area_levels";
48
+
42
     @Override
49
     @Override
50
+    @Transactional(rollbackFor = Exception.class)
43
     public Long createAreaLevel(AreaLevelSaveReqVO createReqVO) {
51
     public Long createAreaLevel(AreaLevelSaveReqVO createReqVO) {
44
-        // 插入
45
-        AreaLevelDO areaLevel = BeanUtils.toBean(createReqVO, AreaLevelDO.class);
46
-        areaLevel.setId(YitIdHelper.nextId());
47
-        areaLevelMapper.insert(areaLevel);
48
-        // 返回
49
-        return areaLevel.getId();
52
+        try {
53
+            // 插入
54
+            AreaLevelDO areaLevel = BeanUtils.toBean(createReqVO, AreaLevelDO.class);
55
+            areaLevel.setId(YitIdHelper.nextId());
56
+            areaLevelMapper.insert(areaLevel);
57
+
58
+            // 删除缓存
59
+            redisTemplate.delete(ALL_AREA_LEVEL_KEY);
60
+
61
+            // 重新获取并设置缓存
62
+            List<AreaLevelDO> allAreaLevels = areaLevelMapper.selectList();
63
+            // 将数据存入 Redis,设置过期时间为不过期
64
+            AreaLevelDO areaLevelDO = new AreaLevelDO();
65
+            areaLevelDO.setAreaLevel(0);
66
+            areaLevelDO.setAreaCode("0");
67
+            areaLevelDO.setAreaName("国家粮食交易中心");
68
+            areaLevelDO.setId(0L);
69
+            allAreaLevels.add(areaLevelDO);
70
+            redisTemplate.opsForValue().set(ALL_AREA_LEVEL_KEY, allAreaLevels);
71
+
72
+            // 返回
73
+            return areaLevel.getId();
74
+        } catch (Exception e) {
75
+            // 发生异常时回滚数据库事务
76
+            throw e;
77
+        }
50
     }
78
     }
51
 
79
 
52
     @Override
80
     @Override
81
+    @Transactional(rollbackFor = Exception.class)
53
     public void updateAreaLevel(AreaLevelSaveReqVO updateReqVO) {
82
     public void updateAreaLevel(AreaLevelSaveReqVO updateReqVO) {
54
-        // 校验存在
55
-        validateAreaLevelExists(updateReqVO.getId());
56
-        // 更新
57
-        AreaLevelDO updateObj = BeanUtils.toBean(updateReqVO, AreaLevelDO.class);
58
-        areaLevelMapper.updateById(updateObj);
83
+        try {
84
+            // 校验存在
85
+            validateAreaLevelExists(updateReqVO.getId());
86
+            // 更新
87
+            AreaLevelDO updateObj = BeanUtils.toBean(updateReqVO, AreaLevelDO.class);
88
+            areaLevelMapper.updateById(updateObj);
89
+
90
+            // 删除缓存
91
+            redisTemplate.delete(ALL_AREA_LEVEL_KEY);
92
+
93
+            // 重新获取并设置缓存
94
+            List<AreaLevelDO> allAreaLevels = areaLevelMapper.selectList();
95
+            // 将数据存入 Redis,设置过期时间为不过期
96
+            AreaLevelDO areaLevelDO = new AreaLevelDO();
97
+            areaLevelDO.setAreaLevel(0);
98
+            areaLevelDO.setAreaCode("0");
99
+            areaLevelDO.setAreaName("国家粮食交易中心");
100
+            areaLevelDO.setId(0L);
101
+            allAreaLevels.add(areaLevelDO);
102
+            redisTemplate.opsForValue().set(ALL_AREA_LEVEL_KEY, allAreaLevels);
103
+        } catch (Exception e) {
104
+            // 发生异常时回滚数据库事务
105
+            throw e;
106
+        }
59
     }
107
     }
60
 
108
 
61
     @Override
109
     @Override
110
+    @Transactional(rollbackFor = Exception.class)
62
     public void deleteAreaLevel(Long id) {
111
     public void deleteAreaLevel(Long id) {
63
-        // 校验存在
64
-        validateAreaLevelExists(id);
65
-        // 删除
66
-        areaLevelMapper.deleteById(id);
112
+        try {
113
+            // 校验存在
114
+            validateAreaLevelExists(id);
115
+            // 删除
116
+            areaLevelMapper.deleteById(id);
117
+
118
+            // 删除缓存
119
+            redisTemplate.delete(ALL_AREA_LEVEL_KEY);
120
+
121
+            // 重新获取并设置缓存
122
+            List<AreaLevelDO> allAreaLevels = areaLevelMapper.selectList();
123
+            // 将数据存入 Redis,设置过期时间为不过期
124
+            AreaLevelDO areaLevelDO = new AreaLevelDO();
125
+            areaLevelDO.setAreaLevel(0);
126
+            areaLevelDO.setAreaCode("0");
127
+            areaLevelDO.setAreaName("国家粮食交易中心");
128
+            areaLevelDO.setId(0L);
129
+            allAreaLevels.add(areaLevelDO);
130
+            redisTemplate.opsForValue().set(ALL_AREA_LEVEL_KEY, allAreaLevels);
131
+        } catch (Exception e) {
132
+            // 发生异常时回滚数据库事务
133
+            throw e;
134
+        }
67
     }
135
     }
68
 
136
 
69
     private void validateAreaLevelExists(Long id) {
137
     private void validateAreaLevelExists(Long id) {
@@ -73,77 +141,133 @@ public class AreaLevelServiceImpl implements AreaLevelService {
73
     }
141
     }
74
 
142
 
75
     @Override
143
     @Override
76
-    public AreaLevelDO getAreaLevel(Integer id) {
77
-        return areaLevelMapper.selectById(id);
144
+    public AreaLevelDO getAreaLevel(Long id) {
145
+        AreaLevelDO resp = new AreaLevelDO();
146
+
147
+        List<AreaLevelDO> allAreaLevels = (List<AreaLevelDO>) redisTemplate.opsForValue().get(ALL_AREA_LEVEL_KEY);
148
+        if (allAreaLevels != null) {
149
+            for (AreaLevelDO allAreaLevel : allAreaLevels) {
150
+                if (Objects.equals(id, allAreaLevel.getId())) {
151
+                    resp = allAreaLevel;
152
+                    break;
153
+                }
154
+            }
155
+        }else {
156
+            resp = areaLevelMapper.selectById(id);
157
+        }
158
+        return resp;
78
     }
159
     }
79
 
160
 
80
     @Override
161
     @Override
81
     public List<AreaLevelDO> getAreaShengLevel() {
162
     public List<AreaLevelDO> getAreaShengLevel() {
82
-        return areaLevelMapper.selectShengLevel();
163
+        List<AreaLevelDO> respList;
164
+
165
+        List<AreaLevelDO> allAreaLevels = (List<AreaLevelDO>) redisTemplate.opsForValue().get(ALL_AREA_LEVEL_KEY);
166
+        if (allAreaLevels != null) {
167
+            respList = allAreaLevels.stream().filter(areaLevelDO -> areaLevelDO.getAreaLevel() == 1).collect(Collectors.toList());
168
+        }else {
169
+            respList = areaLevelMapper.selectShengLevel();
170
+        }
171
+        return respList;
83
 
172
 
84
     }
173
     }
85
 
174
 
86
     @Override
175
     @Override
87
     public List<AreaLevelDO> getAreaListByParentCode(String parentCode) {
176
     public List<AreaLevelDO> getAreaListByParentCode(String parentCode) {
88
-        return areaLevelMapper.selectListByParentCode(parentCode);
177
+        List<AreaLevelDO> respList;
178
+
179
+        List<AreaLevelDO> allAreaLevels = (List<AreaLevelDO>) redisTemplate.opsForValue().get(ALL_AREA_LEVEL_KEY);
180
+        if (allAreaLevels != null) {
181
+            respList = allAreaLevels.stream().filter(areaLevelDO -> parentCode.equals(areaLevelDO.getParentCode())).collect(Collectors.toList());
182
+        }else {
183
+            respList = areaLevelMapper.selectListByParentCode(parentCode);
184
+        }
185
+        return respList;
89
     }
186
     }
90
 
187
 
91
     @Override
188
     @Override
92
     public List<AreaLevelDO> getAreaLevelListAll(AdminUserDO user) {
189
     public List<AreaLevelDO> getAreaLevelListAll(AdminUserDO user) {
93
-        log.info("user: {}", user);
94
-        //国家局或者管理员
95
-        if (ObjectUtil.isEmpty(user.getAreaLevel()) || user.getAreaLevel() == 0) {
190
+        // 尝试从 Redis 中获取所有行政区划数据
191
+        List<AreaLevelDO> allAreaLevels = (List<AreaLevelDO>) redisTemplate.opsForValue().get(ALL_AREA_LEVEL_KEY);
192
+        if (allAreaLevels == null) {
193
+            // 如果 Redis 中没有数据,则从数据库中获取
194
+            allAreaLevels = areaLevelMapper.selectList();
195
+            // 将数据存入 Redis,设置过期时间为不过期
96
             AreaLevelDO areaLevelDO = new AreaLevelDO();
196
             AreaLevelDO areaLevelDO = new AreaLevelDO();
97
             areaLevelDO.setAreaLevel(0);
197
             areaLevelDO.setAreaLevel(0);
98
             areaLevelDO.setAreaCode("0");
198
             areaLevelDO.setAreaCode("0");
99
             areaLevelDO.setAreaName("国家粮食交易中心");
199
             areaLevelDO.setAreaName("国家粮食交易中心");
100
             areaLevelDO.setId(0L);
200
             areaLevelDO.setId(0L);
101
-            List<AreaLevelDO> temp = areaLevelMapper.selectList();
102
-            temp.add(areaLevelDO);
103
-            return  temp;
201
+            allAreaLevels.add(areaLevelDO);
202
+            redisTemplate.opsForValue().set(ALL_AREA_LEVEL_KEY, allAreaLevels);
203
+        }
104
 
204
 
205
+        // 国家局或者管理员
206
+        if (ObjectUtil.isEmpty(user.getAreaLevel()) || user.getAreaLevel() == 0) {
207
+            return allAreaLevels;
105
         }
208
         }
106
         // 省一级用户
209
         // 省一级用户
107
         else if (user.getAreaLevel() == 1) {
210
         else if (user.getAreaLevel() == 1) {
108
-            AreaLevelDO areaLevelDO = areaLevelMapper.selectListByAreaCode(user.getAreaCode());
109
-            List<AreaLevelDO> temp = getSubAreasRecursively(user.getAreaCode());
110
-            temp.add(areaLevelDO);
211
+            AreaLevelDO areaLevelDO = getAreaLevelFromList(allAreaLevels, user.getAreaCode());
212
+            List<AreaLevelDO> temp = getSubAreasRecursivelyFromList(allAreaLevels, user.getAreaCode());
213
+            if (areaLevelDO != null) {
214
+                temp.add(areaLevelDO);
215
+            }
111
             return temp;
216
             return temp;
112
         }
217
         }
113
         // 市一级用户
218
         // 市一级用户
114
         else if (user.getAreaLevel() == 2) {
219
         else if (user.getAreaLevel() == 2) {
115
-            AreaLevelDO areaLevelDO = areaLevelMapper.selectListByAreaCode(user.getAreaCode());
116
-            List<AreaLevelDO> temp = getSubAreasRecursively(user.getAreaCode());
117
-            temp.add(areaLevelDO);
220
+            AreaLevelDO areaLevelDO = getAreaLevelFromList(allAreaLevels, user.getAreaCode());
221
+            List<AreaLevelDO> temp = getSubAreasRecursivelyFromList(allAreaLevels, user.getAreaCode());
222
+            if (areaLevelDO != null) {
223
+                temp.add(areaLevelDO);
224
+            }
118
             return temp;
225
             return temp;
119
         }
226
         }
120
         // 县一级用户
227
         // 县一级用户
121
         else if (user.getAreaLevel() == 3) {
228
         else if (user.getAreaLevel() == 3) {
122
             // 县级是最低级别,只返回自身
229
             // 县级是最低级别,只返回自身
123
-            AreaLevelDO areaLevelDO = areaLevelMapper.selectListByAreaCode(user.getAreaCode());
124
-            return Collections.singletonList(areaLevelDO);
230
+            AreaLevelDO areaLevelDO = getAreaLevelFromList(allAreaLevels, user.getAreaCode());
231
+            if (areaLevelDO != null) {
232
+                return Collections.singletonList(areaLevelDO);
233
+            }
125
         }
234
         }
126
         // 返回空列表或其他默认行为
235
         // 返回空列表或其他默认行为
127
         return Collections.emptyList();
236
         return Collections.emptyList();
128
     }
237
     }
129
 
238
 
130
-    @Override
131
-    public List<AreaLevelDO> getLoginUserXzqhConcise(String areaCode,AdminUserDO user) {
132
-//        AreaLevelDO areaLevelDO = new AreaLevelDO();
133
-
134
-        return areaLevelMapper.selectListByParentCode(areaCode);
239
+    private AreaLevelDO getAreaLevelFromList(List<AreaLevelDO> allAreaLevels, String areaCode) {
240
+        for (AreaLevelDO areaLevel : allAreaLevels) {
241
+            if (areaCode.equals(areaLevel.getAreaCode())) {
242
+                return areaLevel;
243
+            }
244
+        }
245
+        return null;
135
     }
246
     }
136
 
247
 
137
-    private List<AreaLevelDO> getSubAreasRecursively(String parentCode) {
138
-        List<AreaLevelDO> subAreas = areaLevelMapper.selectListByParentCode(parentCode);
139
-        if (ObjectUtil.isEmpty(subAreas) || subAreas.isEmpty()) {
140
-            return subAreas;
248
+    private List<AreaLevelDO> getSubAreasRecursivelyFromList(List<AreaLevelDO> allAreaLevels, String parentCode) {
249
+        List<AreaLevelDO> subAreas = new ArrayList<>();
250
+        for (AreaLevelDO areaLevel : allAreaLevels) {
251
+            if (parentCode.equals(areaLevel.getParentCode())) {
252
+                subAreas.add(areaLevel);
253
+                subAreas.addAll(getSubAreasRecursivelyFromList(allAreaLevels, areaLevel.getAreaCode()));
254
+            }
141
         }
255
         }
142
-        List<AreaLevelDO> allSubAreas = new ArrayList<>(subAreas);
143
-        for (AreaLevelDO area : subAreas) {
144
-            allSubAreas.addAll(getSubAreasRecursively(area.getAreaCode()));
256
+        return subAreas;
257
+    }
258
+
259
+    @Override
260
+    public List<AreaLevelDO> getLoginUserXzqhConcise(String areaCode) {
261
+        List<AreaLevelDO> respList;
262
+
263
+        List<AreaLevelDO> allAreaLevels = (List<AreaLevelDO>) redisTemplate.opsForValue().get(ALL_AREA_LEVEL_KEY);
264
+        if (allAreaLevels != null) {
265
+            respList = allAreaLevels.stream().filter(areaLevelDO -> areaCode.equals(areaLevelDO.getParentCode())).collect(Collectors.toList());
266
+        }else {
267
+            respList = areaLevelMapper.selectListByParentCode(areaCode);
145
         }
268
         }
146
-        return allSubAreas;
269
+
270
+        return respList;
147
     }
271
     }
148
 
272
 
149
     @Override
273
     @Override