ny
23 小时以前 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package jnpf.base.util.dataSet;
 
import jnpf.base.ActionResult;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.model.dataset.DataSetConfig;
import jnpf.base.model.dataset.DataSetOptions;
import jnpf.base.model.dataset.DataSetSwapModel;
import jnpf.base.service.DataInterfaceService;
import jnpf.base.service.DictionaryDataService;
import jnpf.permission.*;
import jnpf.util.*;
import jnpf.util.data.DataSourceContextHolder;
import jnpf.util.visiual.DataTypeConst;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
 
/**
 * 数据集数据转换多线程
 *
 * @author JNPF开发平台组
 * @version v5.0.0
 * @copyright 引迈信息技术有限公司
 * @date 2024/7/15 16:45:09
 */
@Slf4j
@Component
public class DataSetExecutor {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserApi userApi;
    @Autowired
    private OrganizeApi organizeApi;
    @Autowired
    private PositionApi positionApi;
    @Autowired
    private RoleApi roleApi;
    @Autowired
    private GroupApi groupApi;
    @Autowired
    private DictionaryDataService dictionaryDataApi;
    @Autowired
    private DataInterfaceService dataInterFaceApi;
 
 
    private String tenantId = "";
    private static long DEFAULT_CACHE_TIME = 60 * 5;
 
 
    /**
     * 添加多线程
     */
    public void executorRedis(Map<String, Object> localCache, String printId, List<DataSetSwapModel> swapList, Map<String, Object> map) {
        tenantId = Optional.ofNullable(DataSourceContextHolder.getDatasourceId()).orElse("");
        Map<String, OateSetExParam> listExecutor = new HashMap<>();
        if (MapUtils.isNotEmpty(map)) {
            for (String key : map.keySet()) {
                if (Objects.isNull(map.get(key))) continue;
                List<Map<String, Object>> list = (List<Map<String, Object>>) map.get(key);
                boolean needOrg = false, needDep = false, needPos = false, needUser = false, needRole = false, needGroup = false;
                for (Map<String, Object> item : list) {
                    for (DataSetSwapModel model : swapList) {
                        String field = model.getField();
                        String type = model.getType();
                        if(StringUtil.isEmpty(field) || StringUtil.isEmpty(type)) continue;
                        String[] fields = field.split("\\.");
 
                        DataSetConfig config = model.getConfig();
                        if (!key.equals(fields[0]) || item.get(fields[1]) == null || "".equals(item.get(fields[1]))) {
                            continue;
                        }
                        String redisKey;
                        switch (type) {
                            case DataSetConstant.KEY_ORG:
                                needOrg = true;
                                break;
                            case DataSetConstant.KEY_DEP:
                                needDep = true;
                                break;
                            case DataSetConstant.KEY_POS:
                                needPos = true;
                                break;
                            case DataSetConstant.KEY_USER:
                                needUser = true;
                                break;
                            case DataSetConstant.KEY_ROLE:
                                needRole = true;
                                break;
                            case DataSetConstant.KEY_GROUP:
                                needGroup = true;
                                break;
                            case DataSetConstant.KEY_USERS:
                                needOrg = true;
                                needDep = true;
                                needPos = true;
                                needUser = true;
                                needRole = true;
                                needGroup = true;
                                break;
                            case DataSetConstant.KEY_SELECT:
                                if (DataTypeConst.STATIC.equals(config.getDataType())) {
                                    redisKey = String.format("%s-%s-%s", printId, field, DataTypeConst.STATIC);
                                    if (!localCache.containsKey(redisKey)) {
                                        listExecutor.putIfAbsent(redisKey, new OateSetExParam(redisKey, DataSetConstant.KEY_SELECT, null, config));
                                    }
                                }
                                if (DataTypeConst.DICTIONARY.equals(config.getDataType())) {
                                    redisKey = String.format("%s-%s-%s", tenantId, DataTypeConst.DICTIONARY, model.getConfig().getDictionaryType());
                                    if (!localCache.containsKey(redisKey)) {
                                        listExecutor.putIfAbsent(redisKey, new OateSetExParam(redisKey, DataSetConstant.KEY_SELECT, null, config));
                                    }
                                }
                                if (DataTypeConst.DYNAMIC.equals(config.getDataType())) {
                                    redisKey = String.format("%s-%s-%s", tenantId, DataTypeConst.DYNAMIC, model.getConfig().getPropsUrl());
                                    if (!localCache.containsKey(redisKey)) {
                                        listExecutor.putIfAbsent(redisKey, new OateSetExParam(redisKey, DataSetConstant.KEY_SELECT, null, config));
                                    }
                                }
                                needGroup = true;
                                break;
                            default:
                                break;
                        }
                    }
                }
                //添加系统缓存
                Map<String, String> reidsKeyMap = new LinkedHashMap<>();
                if (needOrg) reidsKeyMap.put(tenantId + CacheKeyUtil.SYS_ORG_Tree, DataSetConstant.KEY_ORG);
                if (needDep) reidsKeyMap.put(tenantId + CacheKeyUtil.SYS_DEP, DataSetConstant.KEY_DEP);
                if (needPos) reidsKeyMap.put(tenantId + CacheKeyUtil.SYS_POS, DataSetConstant.KEY_POS);
                if (needUser) reidsKeyMap.put(tenantId + CacheKeyUtil.SYS_USER, DataSetConstant.KEY_USER);
                if (needRole) reidsKeyMap.put(tenantId + CacheKeyUtil.SYS_ROLE, DataSetConstant.KEY_ROLE);
                if (needGroup) reidsKeyMap.put(tenantId + CacheKeyUtil.SYS_GROUP, DataSetConstant.KEY_GROUP);
                for (String redisKey : reidsKeyMap.keySet()) {
                    if (!localCache.containsKey(redisKey)) {
                        listExecutor.putIfAbsent(redisKey, new OateSetExParam(redisKey, reidsKeyMap.get(redisKey), null, null));
                    }
                }
            }
 
            //执行多线程方法
            if (!listExecutor.isEmpty()) {
                this.execute(localCache, listExecutor);
            }
        }
    }
 
    /**
     * 执行多线程
     */
    private void execute(Map<String, Object> localCache, Map<String, OateSetExParam> listExecutor) {
        CountDownLatch countDownLatch = new CountDownLatch(listExecutor.size());
        for (String key : listExecutor.keySet()) {
            OateSetExParam item = listExecutor.get(key);
            String redisKey = item.getRedisKey();
            ThreadPoolExecutorUtil.getExecutor().execute(() -> {
                try {
                    switch (item.getType()) {
                        case DataSetConstant.KEY_USER:
                            //人员
                            Map<String, Object> userMap;
                            if (redisUtil.exists(redisKey)) {
                                userMap = redisUtil.getMap(redisKey);
                                userMap = Optional.ofNullable(userMap).orElse(new HashMap<>(20));
                            } else {
                                userMap = userApi.getUserMap("id-fullName");
                                if (DataSetSwapUtil.NEEDCACHE_SYS) {
                                    redisUtil.insert(redisKey, userMap, DEFAULT_CACHE_TIME);
                                }
                            }
                            localCache.put(CacheKeyUtil.SYS_USER, userMap);
                            break;
                        case DataSetConstant.KEY_ORG:
                            Map<String, Object> orgMap;
                            if (redisUtil.exists(redisKey)) {
                                orgMap = redisUtil.getMap(redisKey);
                                orgMap = Optional.ofNullable(orgMap).orElse(new HashMap<>(20));
                            } else {
                                orgMap = organizeApi.getAllOrgsTreeName();
                                if (DataSetSwapUtil.NEEDCACHE_SYS) {
                                    redisUtil.insert(redisKey, orgMap, DEFAULT_CACHE_TIME);
                                }
                            }
                            localCache.put(CacheKeyUtil.SYS_ORG_Tree, orgMap);
                            break;
                        case DataSetConstant.KEY_DEP:
                            Map<String, Object> depMap;
                            if (redisUtil.exists(redisKey)) {
                                depMap = redisUtil.getMap(redisKey);
                                depMap = Optional.ofNullable(depMap).orElse(new HashMap<>(20));
                            } else {
                                depMap = organizeApi.getOrgMap("id-fullName", "");
                                if (DataSetSwapUtil.NEEDCACHE_SYS) {
                                    redisUtil.insert(redisKey, depMap, DEFAULT_CACHE_TIME);
                                }
                            }
                            localCache.put(CacheKeyUtil.SYS_DEP, depMap);
                            break;
                        case DataSetConstant.KEY_POS:
                            Map<String, String> posMap;
                            if (redisUtil.exists(redisKey)) {
                                posMap = redisUtil.getMap(redisKey);
                                posMap = Optional.ofNullable(posMap).orElse(new HashMap<>(20));
                            } else {
                                posMap = positionApi.getPosFullNameMap();
                                if (DataSetSwapUtil.NEEDCACHE_SYS) {
                                    redisUtil.insert(redisKey, posMap, DEFAULT_CACHE_TIME);
                                }
                            }
                            localCache.put(CacheKeyUtil.SYS_POS, posMap);
                            break;
                        case DataSetConstant.KEY_ROLE:
                            Map<String, Object> roleMap;
                            if (redisUtil.exists(redisKey)) {
                                roleMap = redisUtil.getMap(redisKey);
                                roleMap = Optional.ofNullable(roleMap).orElse(new HashMap<>(20));
                            } else {
                                roleMap = roleApi.getRoleMap("id-fullName");
                                if (DataSetSwapUtil.NEEDCACHE_SYS) {
                                    redisUtil.insert(redisKey, roleMap, DEFAULT_CACHE_TIME);
                                }
                            }
                            localCache.put(CacheKeyUtil.SYS_ROLE, roleMap);
                            break;
                        case DataSetConstant.KEY_GROUP:
                            Map<String, Object> groupMap;
                            if (redisUtil.exists(redisKey)) {
                                groupMap = redisUtil.getMap(redisKey);
                                groupMap = Optional.ofNullable(groupMap).orElse(new HashMap<>(20));
                            } else {
                                groupMap = groupApi.getGroupMap("id-fullName");
                                if (DataSetSwapUtil.NEEDCACHE_SYS) {
                                    redisUtil.insert(redisKey, groupMap, DEFAULT_CACHE_TIME);
                                }
                            }
                            localCache.put(CacheKeyUtil.SYS_GROUP, groupMap);
                            break;
 
                        case DataSetConstant.KEY_SELECT:
                            DataSetConfig config = (DataSetConfig) item.getParam();
                            Map<String, String> selectMap = new HashMap<>(16);
                            List<Map<String, Object>> options = new ArrayList<>();
                            //静态数据
                            if (DataTypeConst.STATIC.equals(config.getDataType())) {
                                if (!localCache.containsKey(redisKey)) {
                                    if (!redisUtil.exists(redisKey)) {
                                        if (config.getOptions() != null) {
                                            List<DataSetOptions> configOptions = config.getOptions();
                                            for (DataSetOptions dso : configOptions) {
                                                selectMap.put(dso.getId(), dso.getFullName());
                                            }
                                        }
                                        redisUtil.insert(redisKey, selectMap, DEFAULT_CACHE_TIME);
                                        localCache.put(redisKey, selectMap);
                                    } else {
                                        localCache.put(redisKey, redisUtil.getMap(redisKey));
                                    }
                                }
                            }
 
                            //数据字典
                            if (DataTypeConst.DICTIONARY.equals(config.getDataType())) {
                                if (!localCache.containsKey(redisKey)) {
                                    if (!redisUtil.exists(redisKey)) {
                                        List<DictionaryDataEntity> list = dictionaryDataApi.getDicList(config.getDictionaryType());
                                        options = list.stream().map(dic -> {
                                            Map<String, Object> dictionaryMap = new HashMap<>(16);
                                            dictionaryMap.put("id", dic.getId());
                                            dictionaryMap.put("enCode", dic.getEnCode());
                                            dictionaryMap.put("fullName", dic.getFullName());
                                            return dictionaryMap;
                                        }).collect(Collectors.toList());
                                        String dictionaryData = JsonUtil.getObjectToString(options);
                                        redisUtil.insert(redisKey, dictionaryData, DEFAULT_CACHE_TIME);
                                        localCache.put(redisKey, options);
                                    } else {
                                        String dictionaryStringData = redisUtil.getString(redisKey).toString();
                                        localCache.put(redisKey, JsonUtil.getJsonToListMap(dictionaryStringData));
                                    }
                                }
                            }
 
                            //数据接口
                            if (DataTypeConst.DYNAMIC.equals(config.getDataType())) {
                                if (!localCache.containsKey(redisKey)) {
                                    if (!redisUtil.exists(redisKey)) {
                                        List<Map<String,Object>> dataList = new ArrayList<>();
                                        ActionResult data = dataInterFaceApi.infoToId(config.getPropsUrl(), null, null);
                                        if (data != null && data.getData() != null) {
                                            if (data.getData() instanceof List) {
                                                dataList = (List<Map<String, Object>>) data.getData();
                                            }
                                        }
                                        String dynamicData = JsonUtil.getObjectToString(dataList);
                                        redisUtil.insert(redisKey, dynamicData, DEFAULT_CACHE_TIME);
                                        localCache.put(redisKey, dataList);
                                    } else {
                                        String dynamicDataStringData = redisUtil.getString(redisKey).toString();
                                        localCache.put(redisKey, JsonUtil.getJsonToListMap(dynamicDataStringData));
                                    }
                                }
                            }
                            break;
                        default:
 
                            break;
                    }
                } catch (Exception e) {
                    log.error("线程执行错误:" + e.getMessage());
//                    e.printStackTrace();
                } finally {
                    //每执行一次数值减少一
                    countDownLatch.countDown();
                    //也可以给await()设置超时时间,如果超过300s(也可以是时,分)则不再等待,直接执行下面代码。
                    //countDownLatch.await(300,TimeUnit.SECONDS);
                }
            });
        }
 
        try {
            //等待计数器归零
            countDownLatch.await();
        } catch (InterruptedException e) {
            log.error("线程计数错误:" + e.getMessage());
//            e.printStackTrace();
        }
    }
}
 
@Data
class OateSetExParam {
    private String redisKey;
    private String type;
    private String interfaceId;
    private Object param;
 
    public OateSetExParam(String redisKey, String type, String interfaceId, Object param) {
        this.redisKey = redisKey;
        this.type = type;
        this.interfaceId = interfaceId;
        this.param = param;
    }
}