许鹏程
2023-05-24 34ff70bc904e68548b5996561d5da52a67a46d24
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package com.product.admin.entity;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Sets;
import com.product.admin.config.CmnConst;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.util.BaseUtil;
 
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
 
public class RouterEntity implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    /**
     * 根据按钮存放路由
     */
    private Map<String, RouterMeta> buttonRouterMap = new HashMap<>();
    /**
     * 根据功能存放路由
     */
    private Map<String, List<RouterMeta>> functionRouterMap = new HashMap<>();
    /**
     * 根据按钮&功能存放路由
     */
    private Map<String, RouterMeta> functionButtonRouterMap = new HashMap<>();
    /**
     * 根据 功能&按钮名称 存放的路由信息
     */
    private Map<String, RouterMeta> routerMetaMap = new HashMap<>();
    /**
     * 根据角色存放路由信息
     */
    private Map<String, List<Set<RouterMeta>>> roleRouterMap;
 
    public Map<String, RouterMeta> getButtonRouterMap() {
        return buttonRouterMap;
    }
 
    public void setButtonRouterMap(Map<String, RouterMeta> buttonRouterMap) {
        this.buttonRouterMap = buttonRouterMap;
    }
 
    public Map<String, List<RouterMeta>> getFunctionRouterMap() {
        return functionRouterMap;
    }
 
    public void setFunctionRouterMap(Map<String, List<RouterMeta>> functionRouterMap) {
        this.functionRouterMap = functionRouterMap;
    }
 
    public Map<String, RouterMeta> getFunctionButtonRouterMap() {
        return functionButtonRouterMap;
    }
 
    public void setFunctionButtonRouterMap(Map<String, RouterMeta> functionButtonRouterMap) {
        this.functionButtonRouterMap = functionButtonRouterMap;
    }
 
    public Map<String, RouterMeta> getRouterMetaMap() {
        return routerMetaMap;
    }
 
    public void setRouterMetaMap(Map<String, RouterMeta> routerMetaMap) {
        this.routerMetaMap = routerMetaMap;
    }
 
    public Map<String, List<Set<RouterMeta>>> getRoleRouterMap() {
        return roleRouterMap;
    }
 
    public void setRoleRouterMap(Map<String, List<Set<RouterMeta>>> roleRouterMap) {
        this.roleRouterMap = roleRouterMap;
    }
 
    public void updateRouter(String... role_uuid) {
        if (role_uuid != null && role_uuid.length > 0) {
            routerGroup(role_uuid);
        }
    }
 
    public void updateRouter(String function_uuid, DataTableEntity dt) {
        List<RouterMeta> temp = getRouterByFunction(function_uuid);
        List<RouterMeta> routerByFunction = null;
        if (temp != null) {
            routerByFunction = Lists.newArrayList(temp);
        }
        for (int i = 0; i < dt.getRows(); i++) {
            String button_uuid = dt.getString(i, "button_uuid");
            RouterMeta routerMeta = buttonRouterMap.get(button_uuid);
            if (routerMeta == null) {
                // 新增路由
                routerMeta = new RouterMeta(dt.getFieldSetEntity(i));
                reload(routerMeta);
            } else {
                // 历史路由 需要更新
                routerMeta.reload(dt.getFieldSetEntity(i));
                routerByFunction.remove(routerMeta);
            }
        }
        if (routerByFunction != null && routerByFunction.size() > 0) {
            removeRouter(function_uuid, routerByFunction);
        }
    }
 
    /**
     * 删除缓存中的路由
     *
     * @param function_uuid
     * @param routerMetas
     */
    public void removeRouter(String function_uuid, List<RouterMeta> routerMetas) {
        if (function_uuid != null && routerMetas != null) {
            List<RouterMeta> routerByFunction = getRouterByFunction(function_uuid);
            for (int i = 0; i < routerMetas.size(); i++) {
                RouterMeta routerMeta = routerMetas.get(0);
                if (routerByFunction != null) {
                    routerMetas.remove(i);
                    i--;
                    continue;
                }
                routerMetaMap.values().remove(routerMeta);
                functionButtonRouterMap.values().remove(routerMeta);
                functionRouterMap.values().remove(routerMeta);
                buttonRouterMap.values().remove(routerMeta);
                if (roleRouterMap != null) {
                    for (List<Set<RouterMeta>> value : roleRouterMap.values()) {
                        if (value.get(0).contains(routerMeta)) {
                            value.get(0).remove(routerMeta);
                        }
                        if (value.get(1).contains(routerMeta)) {
                            value.get(1).remove(routerMeta);
                        }
                    }
                }
            }
 
        }
    }
 
    public void routerGroup(String... roleUuid) {
        DataPoolCacheImpl dataPoolCache = DataPoolCacheImpl.getInstance();
        if (dataPoolCache == null) {
            // 跳出
            return;
        }
        String[] role = null;
        if (roleUuid != null && roleUuid.length > 0) {
            role = roleUuid;
        }
        if (this.roleRouterMap == null) {
            this.roleRouterMap = new HashMap<>();
        }
        DataTableEntity permissionInfo;
        if (role != null) {
            permissionInfo = new DataTableEntity();
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT ");
            sql.append(" role_uuid,function_uuid,button_uuid,role_uuid as uuid ");
            sql.append(" FROM product_sys_function_permission where ");
            sql.append(" role_uuid in (select uuid from product_sys_role where  ");
            sql.append(BaseUtil.buildQuestionMarkFilter("uuid", role, true));
            sql.append(" ) ");
 
            DataTableEntity permissionTemp = dataPoolCache.getBaseDao().listTable(sql.toString(), new Object[]{});
//                DataTableEntity permissionTemp = dataPoolCache.getCacheData("功能权限按角色分组", new String[]{uuid});
            if (permissionTemp != null && permissionTemp.getRows() > 0) {
                BaseUtil.dataTableMerge(permissionInfo, permissionTemp);
            }
        } else {
            permissionInfo = dataPoolCache.getCacheData("功能权限按角色分组", role);
        }
        Map<Object, List<FieldSetEntity>> fieldSets = permissionInfo.getFieldSets();
        //角色uuid集合
        Set<Object> roleSet = fieldSets.keySet();
        // 遍历角色
        for (Object role_uuid : roleSet) {
            //嵌套路由
            Set<RouterMeta> nestMetaList = Sets.newHashSet();
            //非嵌套路由
            Set<RouterMeta> newTableMeatList = Sets.newHashSet();
            //权限
            List<FieldSetEntity> permissionList = fieldSets.get(role_uuid);
            //循环权限信息
            for (FieldSetEntity fse : permissionList) {
                String function_uuid = fse.getString("function_uuid");
                String button_uuid = fse.getString("button_uuid");
                if (StringUtils.isEmpty(button_uuid) || StringUtils.isEmpty(function_uuid)) {
                    continue;
                }
                //按钮uuid集合
                String[] buttons = button_uuid.split(",");
                for (String key : buttons) {
                    RouterMeta routerMeta = getRouterByFunctionButton(function_uuid, key);
                    if (routerMeta == null) {
                        continue;
                    } else if (routerMeta.isNewTabs()) {
                        newTableMeatList.add(routerMeta);
                    } else {
                        nestMetaList.add(routerMeta);
                    }
                }
            }
            roleRouterMap.put(String.valueOf(role_uuid), Lists.newArrayList(nestMetaList, newTableMeatList));
        }
    }
 
    /**
     * 获取路由根据功能
     *
     * @param function_uuid
     * @return
     */
    public List<RouterMeta> getRouterByFunction(String function_uuid) {
        return function_uuid != null ? functionRouterMap.get(function_uuid) : null;
    }
 
    /**
     * 获取路由根据功能按钮
     *
     * @param function_uuid
     * @param button_uuid
     * @return
     */
    private RouterMeta getRouterByFunctionButton(String function_uuid, String button_uuid) {
        return button_uuid == null ? null : functionButtonRouterMap.get(function_uuid + "/" + button_uuid);
    }
 
    /**
     * 获取路由 根据按钮
     *
     * @param button_uuid
     * @return
     */
    public RouterMeta getRouterByButton(String button_uuid) {
        return button_uuid == null ? null : buttonRouterMap.get(button_uuid);
    }
 
    /**
     * 根据功能&按钮uuid获取routeName
     *
     * @param function_uuid
     * @param button_uuid
     * @param role_uuid     角色uuid数组验证传入角色是否拥有权限
     * @return
     */
    public String getRouteNameByUuid(String function_uuid, String button_uuid, String[] role_uuid) {
        if (roleRouterMap == null || role_uuid == null) {
            return null;
        }
        RouterMeta routerByFunctionButton = getRouterByFunctionButton(function_uuid, button_uuid);
        if (routerByFunctionButton == null) {
            return null;
        }
        if (!routerPermission(role_uuid, routerByFunctionButton)) {
            return null;
        }
        return routerByFunctionButton.getName();
    }
 
    /**
     * 判断是否拥有权限
     *
     * @param role_uuid
     * @param routerEntity
     * @return
     */
    public boolean routerPermission(String[] role_uuid, RouterMeta routerEntity) {
        if (role_uuid == null || role_uuid.length <= 0 || routerEntity == null) {
            return false;
        }
        for (String key : role_uuid) {
            List<Set<RouterMeta>> sets = roleRouterMap.get(key);
            if (sets != null) {
                for (Set<RouterMeta> set : sets) {
                    if (set.contains(routerEntity)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
 
 
    /**
     * 根据功能&按钮名称获取routeName
     *
     * @param function_uuid
     * @param button_name
     * @param role_uuid     角色uuid数组验证传入角色是否拥有权限
     * @return
     */
    public String getRouteNameByName(String function_uuid, String button_name, String[] role_uuid) {
        if (roleRouterMap == null || role_uuid == null) {
            //参数异常
            return null;
        }
        RouterMeta routerByFunctionButton = routerMetaMap.get(function_uuid + "/" + button_name);
        if (routerByFunctionButton == null) {
            //路由不存在
            return null;
        }
        if (routerPermission(role_uuid, routerByFunctionButton)) {
            return routerByFunctionButton.getName();
        }
 
        // 没有权限
        return null;
    }
 
 
    /**
     * 路由顶级模板
     *
     * @param openWith 打开方式 (新窗口)
     * @return
     */
    private JSONObject getRouterParentTemp(boolean openWith) {
        JSONObject temp = new JSONObject();
        temp.put(CmnConst.NAME, "");
        temp.put(CmnConst.PATH, "");
        temp.put("redirect", "noRedirect");
        if (!openWith) {
            temp.put(CmnConst.COMPONENT, "Layout");
        } else {
            temp.put(CmnConst.COMPONENT, "ParentView");
        }
        temp.put("alwaysShow", true);
        return temp;
    }
 
    protected List<RouterMeta> getFixRouter() {
        FieldSetEntity fse = new FieldSetEntity();
        fse.setTableName("product_sys_router_v");
        fse.setValue("component", "error/404.vue");
        fse.setValue("name", "404");
        fse.setValue("path", "/404");
 
 
        return Lists.newArrayList(new RouterMeta(fse));
    }
 
 
    public List<RouterMeta> getMobileRouterList(String[] role_uuid) {
        Set<RouterMeta> routerMetas = new HashSet<>();
        if (role_uuid != null && roleRouterMap != null) {
            for (String key : role_uuid) {
                List<Set<RouterMeta>> sets = roleRouterMap.get(key);
                if (sets != null) {
                    routerMetas.addAll(sets.get(0));
                    routerMetas.addAll(sets.get(1));
                }
            }
        }
        return routerMetas.parallelStream().filter(routerMeta -> routerMeta.clientType.indexOf("App") != -1).collect(Collectors.toList());
    }
 
    /**
     * 获取路由
     *
     * @param role_uuid
     * @return
     */
    public JSONArray getRouterList(String[] role_uuid) {
        JSONArray result = new JSONArray();
        Set<RouterMeta> nestMetaList = Sets.newHashSet();
        Set<RouterMeta> newTabMetas = Sets.newHashSet();
        if (role_uuid != null && roleRouterMap != null) {
            for (String key : role_uuid) {
                List<Set<RouterMeta>> sets = roleRouterMap.get(key);
                if (sets != null) {
                    nestMetaList.addAll(sets.get(0));
                    newTabMetas.addAll(sets.get(1));
                }
            }
        }
        JSONObject routerParentTemp;
        if (nestMetaList.size() > 0) {
            routerParentTemp = getRouterParentTemp(false);
            routerParentTemp.put("children", nestMetaList);
            result.add(routerParentTemp);
        }
        if (newTabMetas.size() > 0) {
            routerParentTemp = getRouterParentTemp(true);
            nestMetaList.addAll(getFixRouter());
            routerParentTemp.put("children", newTabMetas);
            result.add(routerParentTemp);
        }
//        if (newTabMetas.size() > 0) {
//            routerParentTemp = getRouterParentTemp(false);
//            nestMetaList.addAll(getFixRouter());
//            routerParentTemp.put("children", newTabMetas);
//            result.add(routerParentTemp);
//        }
        return result;
    }
 
    /**
     * 初始构造函数
     *
     * @param dt
     */
    public RouterEntity(DataTableEntity dt) {
        if (dt != null && dt.getRows() > 0) {
            for (int i = 0; i < dt.getRows(); i++) {
                RouterMeta routerMeta = new RouterMeta(dt.getFieldSetEntity(i));
                reload(routerMeta);
            }
            routerGroup();
        }
    }
 
    /**
     * 重新装载
     *
     * @param routerMeta
     */
    public void reload(RouterMeta routerMeta) {
        String function_uuid = routerMeta.getMeta().getString("group");
        String button_uuid = routerMeta.getMeta().getString("button_uuid");
        String buttonName = routerMeta.getButton();
        this.buttonRouterMap.put(button_uuid, routerMeta);
        functionButtonRouterMap.put(function_uuid + "/" + button_uuid, routerMeta);
        if (this.functionRouterMap.get(function_uuid) == null) {
            this.functionRouterMap.put(function_uuid, new ArrayList<>());
        }
        routerMetaMap.put(function_uuid + "/" + buttonName, routerMeta);
        List<RouterMeta> functionRouter = this.functionRouterMap.get(function_uuid);
        functionRouter.add(routerMeta);
    }
 
 
    /**
     * 单个路由
     */
    public class RouterMeta implements Serializable {
 
        private static final long serialVersionUID = 1L;
        /**
         * 名称
         */
        private String name;
        /**
         * 路由
         */
        private String path;
        /**
         * 组件路径
         */
        private String component;
 
        private boolean newTabs;
 
        private String buttonName;
 
        private JSONObject meta;
        /**
         * 页面类型
         */
        private String pageType;
        /**
         * 客户端类型
         */
        private String clientType;
 
        public void setName(String name) {
            this.name = name;
        }
 
        public void setPath(String path) {
            this.path = path;
        }
 
        public void setComponent(String component) {
            this.component = component;
        }
 
        public void setNewTabs(boolean newTabs) {
            this.newTabs = newTabs;
        }
 
        public String getButtonName() {
            return buttonName;
        }
 
        public void setButtonName(String buttonName) {
            this.buttonName = buttonName;
        }
 
        public void setMeta(JSONObject meta) {
            this.meta = meta;
        }
 
        public void setPageType(String pageType) {
            this.pageType = pageType;
        }
 
        public String getClientType() {
            return clientType;
        }
 
        public void setClientType(String clientType) {
            this.clientType = clientType;
        }
 
        public RouterMeta(FieldSetEntity fse) {
            reload(fse);
        }
 
        public String getButton() {
            return this.buttonName;
        }
 
        public boolean isNewTabs() {
            return newTabs;
        }
 
        public Object getMeta(String key) {
            return this.meta.get(key);
        }
 
        public String getName() {
            return name;
        }
 
        public String getPath() {
            return path;
        }
 
        public String getComponent() {
            return component;
        }
 
        public String getPageType() {
            return pageType;
        }
 
        public JSONObject getMeta() {
            return meta;
        }
 
        public void reload(FieldSetEntity fse) {
            this.name = fse.getString("name");
            this.path = fse.getString("path");
            this.component = fse.getString("component");
            this.newTabs = fse.getBoolean("open_with");
            this.buttonName = fse.getString("button_name");
            this.pageType = fse.getString("page_type");
            this.clientType = fse.getString("client_type_uuid");
            meta = new JSONObject();
            meta.put("icon", fse.getString("icon"));
            meta.put("group", fse.getString("group"));
            meta.put("title", fse.getString("title"));
            meta.put("face_uuid", fse.getString("face_uuid"));
            meta.put("noCache", fse.getBoolean("noCache"));
            meta.put("mvc_page_uuid", fse.getString("mvc_page_uuid"));
            meta.put("menu_icon", fse.getString("menu_icon"));
            meta.put("button_uuid", fse.getString("button_uuid"));
            meta.put("activeMenu", fse.getString("activeMenu"));
            if (fse.getString("face_number") != null) {
                meta.put("face_number", fse.getString("face_number"));
            }
        }
    }
 
 
}