|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+package com.sundot.airport.ledger.util;
|
|
|
2
|
+
|
|
|
3
|
+import com.sundot.airport.common.core.domain.entity.SysDept;
|
|
|
4
|
+import com.sundot.airport.common.core.domain.entity.SysUser;
|
|
|
5
|
+import com.sundot.airport.common.enums.DeptType;
|
|
|
6
|
+import com.sundot.airport.system.domain.BasePosition;
|
|
|
7
|
+import com.sundot.airport.system.mapper.BasePositionMapper;
|
|
|
8
|
+import com.sundot.airport.system.mapper.SysDeptMapper;
|
|
|
9
|
+import com.sundot.airport.system.mapper.SysUserMapper;
|
|
|
10
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11
|
+import org.springframework.stereotype.Component;
|
|
|
12
|
+
|
|
|
13
|
+import java.util.HashMap;
|
|
|
14
|
+import java.util.List;
|
|
|
15
|
+import java.util.Map;
|
|
|
16
|
+import java.util.Set;
|
|
|
17
|
+
|
|
|
18
|
+import java.util.stream.Collectors;
|
|
|
19
|
+
|
|
|
20
|
+/**
|
|
|
21
|
+ * 组织信息补充工具类
|
|
|
22
|
+ */
|
|
|
23
|
+@Component
|
|
|
24
|
+public class OrgInfoHelper {
|
|
|
25
|
+
|
|
|
26
|
+ @Autowired
|
|
|
27
|
+ private SysUserMapper sysUserMapper;
|
|
|
28
|
+
|
|
|
29
|
+ @Autowired
|
|
|
30
|
+ private SysDeptMapper sysDeptMapper;
|
|
|
31
|
+
|
|
|
32
|
+ @Autowired
|
|
|
33
|
+ private BasePositionMapper basePositionMapper;
|
|
|
34
|
+
|
|
|
35
|
+ // 缓存
|
|
|
36
|
+ private volatile Map<String, SysUser> localUserCache = null;
|
|
|
37
|
+ private volatile Map<Long, SysDept> localDeptCache = null;
|
|
|
38
|
+ private volatile Map<String, Map<String, Object>> localOrgInfoCache = new HashMap<>();
|
|
|
39
|
+
|
|
|
40
|
+ // 位置信息缓存
|
|
|
41
|
+ private volatile Map<String, Long> localPositionCache = null;
|
|
|
42
|
+ private volatile Map<String, Long> localAreaCache = null;
|
|
|
43
|
+ private volatile Map<String, Long> localChannelCache = null;
|
|
|
44
|
+
|
|
|
45
|
+ // 缓存过期时间(毫秒)
|
|
|
46
|
+ private volatile long cacheTimestamp = 0L;
|
|
|
47
|
+ private static final long CACHE_EXPIRE_MILLIS = 24 * 60 * 60 * 1000; // 24小时
|
|
|
48
|
+
|
|
|
49
|
+
|
|
|
50
|
+
|
|
|
51
|
+ /**
|
|
|
52
|
+ * 【关键】确保基础缓存已加载到内存(双重检查锁 + 过期检查)
|
|
|
53
|
+ */
|
|
|
54
|
+ private void ensureLocalCacheLoaded() {
|
|
|
55
|
+ long now = System.currentTimeMillis();
|
|
|
56
|
+
|
|
|
57
|
+ // 检查缓存是否过期
|
|
|
58
|
+ boolean expired = (now - cacheTimestamp) > CACHE_EXPIRE_MILLIS;
|
|
|
59
|
+
|
|
|
60
|
+ if (localUserCache == null || localDeptCache == null || localPositionCache == null || expired) {
|
|
|
61
|
+ synchronized (this) {
|
|
|
62
|
+ if (localUserCache == null || localDeptCache == null || localPositionCache == null || expired) {
|
|
|
63
|
+ loadLocalCacheFromDB();
|
|
|
64
|
+ cacheTimestamp = System.currentTimeMillis(); // 记录缓存时间
|
|
|
65
|
+ }
|
|
|
66
|
+ }
|
|
|
67
|
+ }
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ /**
|
|
|
71
|
+ * 直接从数据库加载基础缓存到本地内存(纯内存,零Redis)
|
|
|
72
|
+ */
|
|
|
73
|
+ private void loadLocalCacheFromDB() {
|
|
|
74
|
+ // 1. 查询所有用户
|
|
|
75
|
+ Map<String, SysUser> userCache = new HashMap<>();
|
|
|
76
|
+ List<SysUser> allUsers = sysUserMapper.selectUserList(new SysUser());
|
|
|
77
|
+ if (allUsers != null) {
|
|
|
78
|
+ for (SysUser user : allUsers) {
|
|
|
79
|
+ if (user.getNickName() != null && !user.getNickName().trim().isEmpty()) {
|
|
|
80
|
+ userCache.put(user.getNickName().trim(), user);
|
|
|
81
|
+ }
|
|
|
82
|
+ }
|
|
|
83
|
+ }
|
|
|
84
|
+ localUserCache = userCache;
|
|
|
85
|
+
|
|
|
86
|
+ // 2. 查询所有部门
|
|
|
87
|
+ Map<Long, SysDept> deptCache = new HashMap<>();
|
|
|
88
|
+ List<SysDept> allDepts = sysDeptMapper.selectDeptList(new SysDept());
|
|
|
89
|
+ if (allDepts != null) {
|
|
|
90
|
+ for (SysDept dept : allDepts) {
|
|
|
91
|
+ deptCache.put(dept.getDeptId(), dept);
|
|
|
92
|
+ }
|
|
|
93
|
+ }
|
|
|
94
|
+ localDeptCache = deptCache;
|
|
|
95
|
+
|
|
|
96
|
+ // 3. 查询所有位置并缓存
|
|
|
97
|
+ List<BasePosition> allPositions = basePositionMapper.selectBasePositionList(new BasePosition());
|
|
|
98
|
+ Map<String, Long> positionCache = new HashMap<>();
|
|
|
99
|
+ Map<String, Long> areaCache = new HashMap<>();
|
|
|
100
|
+ Map<String, Long> channelCache = new HashMap<>();
|
|
|
101
|
+
|
|
|
102
|
+ if (allPositions != null) {
|
|
|
103
|
+ for (BasePosition pos : allPositions) {
|
|
|
104
|
+ if (pos.getName() != null && !pos.getName().trim().isEmpty()) {
|
|
|
105
|
+ String name = pos.getName().trim();
|
|
|
106
|
+ String type = pos.getPositionType();
|
|
|
107
|
+
|
|
|
108
|
+ if ("POSITION".equals(type)) {
|
|
|
109
|
+ positionCache.put(name, pos.getId());
|
|
|
110
|
+ } else if ("REGIONAL".equals(type)) {
|
|
|
111
|
+ areaCache.put(name, pos.getId());
|
|
|
112
|
+ } else if ("CHANNEL".equals(type)) {
|
|
|
113
|
+ channelCache.put(name, pos.getId());
|
|
|
114
|
+ }
|
|
|
115
|
+ }
|
|
|
116
|
+ }
|
|
|
117
|
+ }
|
|
|
118
|
+
|
|
|
119
|
+ localPositionCache = positionCache;
|
|
|
120
|
+ localAreaCache = areaCache;
|
|
|
121
|
+ localChannelCache = channelCache;
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
|
124
|
+ /**
|
|
|
125
|
+ * 从内存中的基础缓存构建组织信息
|
|
|
126
|
+ */
|
|
|
127
|
+ private Map<String, Object> buildOrgInfoFromMemory(Map<String, SysUser> userCache,
|
|
|
128
|
+ Map<Long, SysDept> deptCache,
|
|
|
129
|
+ String personName) {
|
|
|
130
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
131
|
+ if (personName == null || userCache == null || deptCache == null) {
|
|
|
132
|
+ return result;
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ // 从缓存中获取用户
|
|
|
136
|
+ SysUser user = userCache.get(personName);
|
|
|
137
|
+ if (user == null || user.getDeptId() == null) {
|
|
|
138
|
+ return result;
|
|
|
139
|
+ }
|
|
|
140
|
+
|
|
|
141
|
+ result.put("userId", user.getUserId());
|
|
|
142
|
+
|
|
|
143
|
+ // 从缓存中获取部门
|
|
|
144
|
+ SysDept userDept = deptCache.get(user.getDeptId());
|
|
|
145
|
+ if (userDept == null) {
|
|
|
146
|
+ return result;
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ String deptType = userDept.getDeptType();
|
|
|
150
|
+
|
|
|
151
|
+ // 根据部门类型构建层级关系(全部从缓存获取父级部门)
|
|
|
152
|
+ if (DeptType.STATION.getCode().equals(deptType)) {
|
|
|
153
|
+ result.put("stationId", user.getDeptId());
|
|
|
154
|
+ result.put("stationName", userDept.getDeptName());
|
|
|
155
|
+ } else if (DeptType.BRIGADE.getCode().equals(deptType)) {
|
|
|
156
|
+ result.put("deptId", user.getDeptId());
|
|
|
157
|
+ result.put("deptName", userDept.getDeptName());
|
|
|
158
|
+ Long parentId = userDept.getParentId();
|
|
|
159
|
+ if (parentId != null && parentId != 0) {
|
|
|
160
|
+ SysDept parentDept = deptCache.get(parentId);
|
|
|
161
|
+ if (parentDept != null) {
|
|
|
162
|
+ result.put("stationId", parentDept.getDeptId());
|
|
|
163
|
+ result.put("stationName", parentDept.getDeptName());
|
|
|
164
|
+ }
|
|
|
165
|
+ }
|
|
|
166
|
+ } else if (DeptType.MANAGER.getCode().equals(deptType)) {
|
|
|
167
|
+ result.put("teamId", user.getDeptId());
|
|
|
168
|
+ result.put("teamName", userDept.getDeptName());
|
|
|
169
|
+ Long parentId = userDept.getParentId();
|
|
|
170
|
+ if (parentId != null && parentId != 0) {
|
|
|
171
|
+ SysDept parentDept = deptCache.get(parentId);
|
|
|
172
|
+ if (parentDept != null) {
|
|
|
173
|
+ result.put("deptId", parentDept.getDeptId());
|
|
|
174
|
+ result.put("deptName", parentDept.getDeptName());
|
|
|
175
|
+ Long grandParentId = parentDept.getParentId();
|
|
|
176
|
+ if (grandParentId != null && grandParentId != 0) {
|
|
|
177
|
+ SysDept grandParentDept = deptCache.get(grandParentId);
|
|
|
178
|
+ if (grandParentDept != null) {
|
|
|
179
|
+ result.put("stationId", grandParentDept.getDeptId());
|
|
|
180
|
+ result.put("stationName", grandParentDept.getDeptName());
|
|
|
181
|
+ }
|
|
|
182
|
+ }
|
|
|
183
|
+ }
|
|
|
184
|
+ }
|
|
|
185
|
+ } else if (DeptType.TEAMS.getCode().equals(deptType)) {
|
|
|
186
|
+ result.put("groupId", user.getDeptId());
|
|
|
187
|
+ result.put("groupName", userDept.getDeptName());
|
|
|
188
|
+ Long parentId = userDept.getParentId();
|
|
|
189
|
+ if (parentId != null && parentId != 0) {
|
|
|
190
|
+ SysDept parentDept = deptCache.get(parentId);
|
|
|
191
|
+ if (parentDept != null) {
|
|
|
192
|
+ result.put("teamId", parentDept.getDeptId());
|
|
|
193
|
+ result.put("teamName", parentDept.getDeptName());
|
|
|
194
|
+ Long grandParentId = parentDept.getParentId();
|
|
|
195
|
+ if (grandParentId != null && grandParentId != 0) {
|
|
|
196
|
+ SysDept grandParentDept = deptCache.get(grandParentId);
|
|
|
197
|
+ if (grandParentDept != null) {
|
|
|
198
|
+ result.put("deptId", grandParentDept.getDeptId());
|
|
|
199
|
+ result.put("deptName", grandParentDept.getDeptName());
|
|
|
200
|
+ Long greatGrandParentId = grandParentDept.getParentId();
|
|
|
201
|
+ if (greatGrandParentId != null && greatGrandParentId != 0) {
|
|
|
202
|
+ SysDept greatGrandParentDept = deptCache.get(greatGrandParentId);
|
|
|
203
|
+ if (greatGrandParentDept != null) {
|
|
|
204
|
+ result.put("stationId", greatGrandParentDept.getDeptId());
|
|
|
205
|
+ result.put("stationName", greatGrandParentDept.getDeptName());
|
|
|
206
|
+ }
|
|
|
207
|
+ }
|
|
|
208
|
+ }
|
|
|
209
|
+ }
|
|
|
210
|
+ }
|
|
|
211
|
+ }
|
|
|
212
|
+ }
|
|
|
213
|
+
|
|
|
214
|
+ return result;
|
|
|
215
|
+ }
|
|
|
216
|
+
|
|
|
217
|
+
|
|
|
218
|
+
|
|
|
219
|
+ /**
|
|
|
220
|
+ * 批量填充组织信息
|
|
|
221
|
+ * 【极致优化】全部使用内存缓存,零Redis查询
|
|
|
222
|
+ */
|
|
|
223
|
+ public <T> void batchFillOrgInfo(List<T> objects, java.util.function.Function<T, String> nameGetter) {
|
|
|
224
|
+ if (objects == null || objects.isEmpty()) {
|
|
|
225
|
+ return;
|
|
|
226
|
+ }
|
|
|
227
|
+
|
|
|
228
|
+ // 1. 收集所有需要查询的姓名
|
|
|
229
|
+ List<String> names = objects.stream()
|
|
|
230
|
+ .map(nameGetter)
|
|
|
231
|
+ .filter(name -> name != null && !name.trim().isEmpty())
|
|
|
232
|
+ .distinct()
|
|
|
233
|
+ .map(String::trim)
|
|
|
234
|
+ .collect(Collectors.toList());
|
|
|
235
|
+
|
|
|
236
|
+ // 2. 确保基础缓存已加载到内存
|
|
|
237
|
+ ensureLocalCacheLoaded();
|
|
|
238
|
+
|
|
|
239
|
+ // 3. 【关键】从内存缓存中批量构建组织信息
|
|
|
240
|
+ Map<String, Map<String, Object>> orgInfoMap = new HashMap<>();
|
|
|
241
|
+
|
|
|
242
|
+ for (String name : names) {
|
|
|
243
|
+ // 先从本地缓存查找
|
|
|
244
|
+ Map<String, Object> cached = localOrgInfoCache.get(name);
|
|
|
245
|
+ if (cached != null) {
|
|
|
246
|
+ orgInfoMap.put(name, cached);
|
|
|
247
|
+ continue;
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ // 从基础缓存构建
|
|
|
251
|
+ Map<String, Object> orgInfo = buildOrgInfoFromMemory(localUserCache, localDeptCache, name);
|
|
|
252
|
+ if (!orgInfo.isEmpty()) {
|
|
|
253
|
+ orgInfoMap.put(name, orgInfo);
|
|
|
254
|
+ localOrgInfoCache.put(name, orgInfo); // 存入本地缓存
|
|
|
255
|
+ }
|
|
|
256
|
+ }
|
|
|
257
|
+
|
|
|
258
|
+ // 4. 从内存快速填充(零Redis查询)
|
|
|
259
|
+ Map<String, Map<String, Object>> finalOrgInfoMap = orgInfoMap;
|
|
|
260
|
+ objects.forEach(obj -> {
|
|
|
261
|
+ String name = nameGetter.apply(obj);
|
|
|
262
|
+ if (name != null && !name.trim().isEmpty()) {
|
|
|
263
|
+ Map<String, Object> orgInfo = finalOrgInfoMap.get(name.trim());
|
|
|
264
|
+ if (orgInfo != null && !orgInfo.isEmpty()) {
|
|
|
265
|
+ try {
|
|
|
266
|
+ setFieldValue(obj, "userId", orgInfo.get("userId"));
|
|
|
267
|
+ setFieldValue(obj, "personId", orgInfo.get("userId"));
|
|
|
268
|
+ setFieldValue(obj, "personUserId", orgInfo.get("userId"));
|
|
|
269
|
+ setFieldValue(obj, "deptId", orgInfo.get("deptId"));
|
|
|
270
|
+ setFieldValue(obj, "deptName", orgInfo.get("deptName"));
|
|
|
271
|
+ setFieldValue(obj, "teamId", orgInfo.get("teamId"));
|
|
|
272
|
+ setFieldValue(obj, "teamName", orgInfo.get("teamName"));
|
|
|
273
|
+ setFieldValue(obj, "groupId", orgInfo.get("groupId"));
|
|
|
274
|
+ setFieldValue(obj, "groupName", orgInfo.get("groupName"));
|
|
|
275
|
+ } catch (Exception e) {
|
|
|
276
|
+ // 忽略异常
|
|
|
277
|
+ }
|
|
|
278
|
+ }
|
|
|
279
|
+ }
|
|
|
280
|
+ });
|
|
|
281
|
+ }
|
|
|
282
|
+
|
|
|
283
|
+ /**
|
|
|
284
|
+ * 批量填充组织信息和位置信息(同时处理人员组织和位置)
|
|
|
285
|
+ */
|
|
|
286
|
+ public <T> void batchFillOrgAndLocationInfo(
|
|
|
287
|
+ List<T> objects,
|
|
|
288
|
+ java.util.function.Function<T, String> nameGetter,
|
|
|
289
|
+ java.util.function.Function<T, String> positionGetter,
|
|
|
290
|
+ java.util.function.Function<T, String> locationGetter,
|
|
|
291
|
+ java.util.function.Function<T, String> channelGetter) {
|
|
|
292
|
+ if (objects == null || objects.isEmpty()) {
|
|
|
293
|
+ return;
|
|
|
294
|
+ }
|
|
|
295
|
+
|
|
|
296
|
+ // 1. 收集所有需要查询的姓名
|
|
|
297
|
+ List<String> names = objects.stream()
|
|
|
298
|
+ .map(nameGetter)
|
|
|
299
|
+ .filter(name -> name != null && !name.trim().isEmpty())
|
|
|
300
|
+ .distinct()
|
|
|
301
|
+ .map(String::trim)
|
|
|
302
|
+ .collect(Collectors.toList());
|
|
|
303
|
+
|
|
|
304
|
+ // 2. 确保基础缓存已加载到内存(包含位置信息)
|
|
|
305
|
+ ensureLocalCacheLoaded();
|
|
|
306
|
+
|
|
|
307
|
+ // 3. 从内存缓存中批量构建组织信息
|
|
|
308
|
+ Map<String, Map<String, Object>> orgInfoMap = new HashMap<>();
|
|
|
309
|
+ for (String name : names) {
|
|
|
310
|
+ Map<String, Object> cached = localOrgInfoCache.get(name);
|
|
|
311
|
+ if (cached != null) {
|
|
|
312
|
+ orgInfoMap.put(name, cached);
|
|
|
313
|
+ continue;
|
|
|
314
|
+ }
|
|
|
315
|
+
|
|
|
316
|
+ Map<String, Object> orgInfo = buildOrgInfoFromMemory(localUserCache, localDeptCache, name);
|
|
|
317
|
+ if (!orgInfo.isEmpty()) {
|
|
|
318
|
+ orgInfoMap.put(name, orgInfo);
|
|
|
319
|
+ localOrgInfoCache.put(name, orgInfo);
|
|
|
320
|
+ }
|
|
|
321
|
+ }
|
|
|
322
|
+ Map<String, Map<String, Object>> finalOrgInfoMap = orgInfoMap;
|
|
|
323
|
+ Map<String, Long> finalPositionCache = localPositionCache;
|
|
|
324
|
+ Map<String, Long> finalAreaCache = localAreaCache;
|
|
|
325
|
+ Map<String, Long> finalChannelCache = localChannelCache;
|
|
|
326
|
+
|
|
|
327
|
+ objects.forEach(obj -> {
|
|
|
328
|
+ try {
|
|
|
329
|
+ // 填充组织信息
|
|
|
330
|
+ String name = nameGetter.apply(obj);
|
|
|
331
|
+ if (name != null && !name.trim().isEmpty()) {
|
|
|
332
|
+ Map<String, Object> orgInfo = finalOrgInfoMap.get(name.trim());
|
|
|
333
|
+ if (orgInfo != null && !orgInfo.isEmpty()) {
|
|
|
334
|
+ setFieldValue(obj, "userId", orgInfo.get("userId"));
|
|
|
335
|
+ setFieldValue(obj, "personId", orgInfo.get("userId"));
|
|
|
336
|
+ setFieldValue(obj, "personUserId", orgInfo.get("userId"));
|
|
|
337
|
+ setFieldValue(obj, "deptId", orgInfo.get("deptId"));
|
|
|
338
|
+ setFieldValue(obj, "deptName", orgInfo.get("deptName"));
|
|
|
339
|
+ setFieldValue(obj, "teamId", orgInfo.get("teamId"));
|
|
|
340
|
+ setFieldValue(obj, "teamName", orgInfo.get("teamName"));
|
|
|
341
|
+ setFieldValue(obj, "groupId", orgInfo.get("groupId"));
|
|
|
342
|
+ setFieldValue(obj, "groupName", orgInfo.get("groupName"));
|
|
|
343
|
+ }
|
|
|
344
|
+ }
|
|
|
345
|
+
|
|
|
346
|
+ // 填充位置信息
|
|
|
347
|
+ if (positionGetter != null) {
|
|
|
348
|
+ String positionName = positionGetter.apply(obj);
|
|
|
349
|
+ if (positionName != null && !positionName.trim().isEmpty()) {
|
|
|
350
|
+ Long positionId = finalPositionCache.get(positionName.trim());
|
|
|
351
|
+ if (positionId != null) {
|
|
|
352
|
+ setFieldValue(obj, "positionId", positionId);
|
|
|
353
|
+ }
|
|
|
354
|
+ }
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ if (locationGetter != null) {
|
|
|
358
|
+ String locationName = locationGetter.apply(obj);
|
|
|
359
|
+ if (locationName != null && !locationName.trim().isEmpty()) {
|
|
|
360
|
+ Long areaId = finalAreaCache.get(locationName.trim());
|
|
|
361
|
+ if (areaId != null) {
|
|
|
362
|
+ setFieldValue(obj, "areaId", areaId);
|
|
|
363
|
+ }
|
|
|
364
|
+ }
|
|
|
365
|
+ }
|
|
|
366
|
+
|
|
|
367
|
+ if (channelGetter != null) {
|
|
|
368
|
+ String channelName = channelGetter.apply(obj);
|
|
|
369
|
+ if (channelName != null && !channelName.trim().isEmpty()) {
|
|
|
370
|
+ Long channelId = finalChannelCache.get(channelName.trim());
|
|
|
371
|
+ if (channelId != null) {
|
|
|
372
|
+ setFieldValue(obj, "channelId", channelId);
|
|
|
373
|
+ }
|
|
|
374
|
+ }
|
|
|
375
|
+ }
|
|
|
376
|
+ } catch (Exception e) {
|
|
|
377
|
+ // 忽略异常
|
|
|
378
|
+ }
|
|
|
379
|
+ });
|
|
|
380
|
+ }
|
|
|
381
|
+
|
|
|
382
|
+ /**
|
|
|
383
|
+ * 仅根据组织名称填充组织ID(不通过人员姓名)
|
|
|
384
|
+ * 适用于有组织名称但没有人员姓名的台账
|
|
|
385
|
+ */
|
|
|
386
|
+ public <T> void batchFillOrgIdByNameOnly(
|
|
|
387
|
+ List<T> objects,
|
|
|
388
|
+ java.util.function.Function<T, String> deptNameGetter,
|
|
|
389
|
+ java.util.function.Function<T, String> teamNameGetter,
|
|
|
390
|
+ java.util.function.Function<T, String> groupNameGetter) {
|
|
|
391
|
+ if (objects == null || objects.isEmpty()) {
|
|
|
392
|
+ return;
|
|
|
393
|
+ }
|
|
|
394
|
+
|
|
|
395
|
+ // 确保部门缓存已加载到内存
|
|
|
396
|
+ ensureLocalCacheLoaded();
|
|
|
397
|
+
|
|
|
398
|
+ // 从内存获取部门缓存
|
|
|
399
|
+ Map<Long, SysDept> deptCache = localDeptCache;
|
|
|
400
|
+ if (deptCache == null || deptCache.isEmpty()) {
|
|
|
401
|
+ return;
|
|
|
402
|
+ }
|
|
|
403
|
+
|
|
|
404
|
+ // 建立部门名称→ID的映射
|
|
|
405
|
+ Map<String, Long> deptNameMap = new HashMap<>();
|
|
|
406
|
+ for (SysDept dept : deptCache.values()) {
|
|
|
407
|
+ if (dept.getDeptName() != null && !dept.getDeptName().trim().isEmpty()) {
|
|
|
408
|
+ deptNameMap.put(dept.getDeptName().trim(), dept.getDeptId());
|
|
|
409
|
+ }
|
|
|
410
|
+ }
|
|
|
411
|
+
|
|
|
412
|
+ // 填充组织ID
|
|
|
413
|
+ objects.forEach(obj -> {
|
|
|
414
|
+ try {
|
|
|
415
|
+ // 填充部门ID
|
|
|
416
|
+ if (deptNameGetter != null) {
|
|
|
417
|
+ String deptName = deptNameGetter.apply(obj);
|
|
|
418
|
+ if (deptName != null && !deptName.trim().isEmpty()) {
|
|
|
419
|
+ Long deptId = deptNameMap.get(deptName.trim());
|
|
|
420
|
+ if (deptId != null) {
|
|
|
421
|
+ setFieldValueSilently(obj, "deptId", deptId);
|
|
|
422
|
+ }
|
|
|
423
|
+ }
|
|
|
424
|
+ }
|
|
|
425
|
+
|
|
|
426
|
+ // 填充队室ID
|
|
|
427
|
+ if (teamNameGetter != null) {
|
|
|
428
|
+ String teamName = teamNameGetter.apply(obj);
|
|
|
429
|
+ if (teamName != null && !teamName.trim().isEmpty()) {
|
|
|
430
|
+ Long teamId = deptNameMap.get(teamName.trim());
|
|
|
431
|
+ if (teamId != null) {
|
|
|
432
|
+ setFieldValueSilently(obj, "teamId", teamId);
|
|
|
433
|
+ }
|
|
|
434
|
+ }
|
|
|
435
|
+ }
|
|
|
436
|
+
|
|
|
437
|
+ // 填充小组ID
|
|
|
438
|
+ if (groupNameGetter != null) {
|
|
|
439
|
+ String groupName = groupNameGetter.apply(obj);
|
|
|
440
|
+ if (groupName != null && !groupName.trim().isEmpty()) {
|
|
|
441
|
+ Long groupId = deptNameMap.get(groupName.trim());
|
|
|
442
|
+ if (groupId != null) {
|
|
|
443
|
+ setFieldValueSilently(obj, "groupId", groupId);
|
|
|
444
|
+ }
|
|
|
445
|
+ }
|
|
|
446
|
+ }
|
|
|
447
|
+ } catch (Exception ignored) {
|
|
|
448
|
+ // 忽略异常
|
|
|
449
|
+ }
|
|
|
450
|
+ });
|
|
|
451
|
+ }
|
|
|
452
|
+
|
|
|
453
|
+ /**
|
|
|
454
|
+ * 静默设置字段值(不抛异常)
|
|
|
455
|
+ */
|
|
|
456
|
+ private void setFieldValueSilently(Object obj, String fieldName, Object value) {
|
|
|
457
|
+ try {
|
|
|
458
|
+ if (value != null) {
|
|
|
459
|
+ java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName);
|
|
|
460
|
+ field.setAccessible(true);
|
|
|
461
|
+ field.set(obj, value);
|
|
|
462
|
+ }
|
|
|
463
|
+ } catch (Exception ignored) {
|
|
|
464
|
+ // 忽略异常
|
|
|
465
|
+ }
|
|
|
466
|
+ }
|
|
|
467
|
+
|
|
|
468
|
+ /**
|
|
|
469
|
+ * 通过反射设置字段值
|
|
|
470
|
+ */
|
|
|
471
|
+ private void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
|
|
|
472
|
+ if (value == null) {
|
|
|
473
|
+ return;
|
|
|
474
|
+ }
|
|
|
475
|
+ Class<?> clazz = obj.getClass();
|
|
|
476
|
+ try {
|
|
|
477
|
+ java.lang.reflect.Field field = clazz.getDeclaredField(fieldName);
|
|
|
478
|
+ field.setAccessible(true);
|
|
|
479
|
+ field.set(obj, value);
|
|
|
480
|
+ } catch (NoSuchFieldException ignored) {
|
|
|
481
|
+ // 字段不存在,忽略
|
|
|
482
|
+ }
|
|
|
483
|
+ }
|
|
|
484
|
+
|
|
|
485
|
+ /**
|
|
|
486
|
+ * 手动触发构建完整缓存(可用于预热)
|
|
|
487
|
+ */
|
|
|
488
|
+ public void buildCompleteOrgCache() {
|
|
|
489
|
+ ensureLocalCacheLoaded();
|
|
|
490
|
+ }
|
|
|
491
|
+
|
|
|
492
|
+ /**
|
|
|
493
|
+ * 【新增】立即清理所有缓存,释放内存
|
|
|
494
|
+ */
|
|
|
495
|
+ public void clearCache() {
|
|
|
496
|
+ synchronized (this) {
|
|
|
497
|
+ localUserCache = null;
|
|
|
498
|
+ localDeptCache = null;
|
|
|
499
|
+ localOrgInfoCache.clear();
|
|
|
500
|
+ localPositionCache = null;
|
|
|
501
|
+ localAreaCache = null;
|
|
|
502
|
+ localChannelCache = null;
|
|
|
503
|
+ cacheTimestamp = 0L;
|
|
|
504
|
+ }
|
|
|
505
|
+ }
|
|
|
506
|
+
|
|
|
507
|
+ /**
|
|
|
508
|
+ * 【新增】获取当前缓存状态信息
|
|
|
509
|
+ */
|
|
|
510
|
+ public String getCacheStatus() {
|
|
|
511
|
+ if (localUserCache == null || localDeptCache == null) {
|
|
|
512
|
+ return "缓存未加载";
|
|
|
513
|
+ }
|
|
|
514
|
+ long now = System.currentTimeMillis();
|
|
|
515
|
+ long age = (now - cacheTimestamp) / 1000; // 秒
|
|
|
516
|
+ long remain = (CACHE_EXPIRE_MILLIS - (now - cacheTimestamp)) / 1000; // 剩余秒数
|
|
|
517
|
+ return String.format("缓存已加载 | 用户: %d | 部门: %d | 组织信息: %d | 岗位: %d | 区域: %d | 通道: %d | 已存在: %d秒 | 剩余: %d秒",
|
|
|
518
|
+ localUserCache.size(),
|
|
|
519
|
+ localDeptCache.size(),
|
|
|
520
|
+ localOrgInfoCache.size(),
|
|
|
521
|
+ localPositionCache != null ? localPositionCache.size() : 0,
|
|
|
522
|
+ localAreaCache != null ? localAreaCache.size() : 0,
|
|
|
523
|
+ localChannelCache != null ? localChannelCache.size() : 0,
|
|
|
524
|
+ age,
|
|
|
525
|
+ Math.max(0, remain)
|
|
|
526
|
+ );
|
|
|
527
|
+ }
|
|
|
528
|
+}
|