许鹏程
2023-05-23 0f8c9e754e780681a45b23963a0c849af9465c50
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
package com.product.admin.entity;
 
import cn.hutool.extra.spring.SpringUtil;
import com.google.common.collect.Maps;
import com.product.admin.config.CmnConst;
import com.product.admin.service.UpdateLoginUserInfoService;
import com.product.common.serialization.SerializationUtils;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
 
import java.io.Serializable;
import java.util.*;
 
/**
 * @author cheng
 * @desc 菜单树
 * @Date 2021年11月15日18:20:20
 */
public class MenuTreeEntity implements Serializable {
 
    public static void main(String[] args) {
        FieldSetEntity fse = new FieldSetEntity();
        fse.setTableName("123");
        MenuTreeEntity menuEntity = new MenuTreeEntity(new DataTableEntity());
        byte[] serialize = SerializationUtils.serialize(menuEntity);
        Object deserialize = SerializationUtils.deserialize(serialize);
        System.out.println(deserialize);
    }
 
 
    public MenuArray getInventoryMenus() {
        return inventoryMenus;
    }
 
    public void setInventoryMenus(MenuArray inventoryMenus) {
        this.inventoryMenus = inventoryMenus;
    }
 
    public Map<String, MenuEntity> getInventoryMenuMap() {
        return inventoryMenuMap;
    }
 
    public void setInventoryMenuMap(Map<String, MenuEntity> inventoryMenuMap) {
        this.inventoryMenuMap = inventoryMenuMap;
    }
 
    public Map<String, MenuEntity> getFunctionMenuMap() {
        return functionMenuMap;
    }
 
    public void setFunctionMenuMap(Map<String, MenuEntity> functionMenuMap) {
        this.functionMenuMap = functionMenuMap;
    }
 
    public Map<String, MenuArray> getRoleMenuMap() {
        return roleMenuMap;
    }
 
    public void setRoleMenuMap(Map<String, MenuArray> roleMenuMap) {
        this.roleMenuMap = roleMenuMap;
    }
 
    public Map<String, MenuEntity> getCodeMenuMap() {
        return codeMenuMap;
    }
 
    public void setCodeMenuMap(Map<String, MenuEntity> codeMenuMap) {
        this.codeMenuMap = codeMenuMap;
    }
 
    public static final long serialVersionUID = 1L;
    /**
     * 目录菜单
     */
    private MenuArray inventoryMenus = newArrayList();
 
    /**
     * 目录菜单存储 key=菜单uuid
     */
    private Map<String, MenuEntity> inventoryMenuMap = Maps.newHashMap();
    /**
     * 功能菜单存储 key=菜单uuid
     */
    private Map<String, MenuEntity> functionMenuMap = Maps.newHashMap();
    /**
     * 角色所属功能集合
     */
    private Map<String, MenuArray> roleMenuMap = Maps.newHashMap();
    /**
     * 编码菜单集合
     */
    private Map<String, MenuEntity> codeMenuMap = Maps.newHashMap();
 
 
    public MenuTreeEntity() {
    }
 
    /**
     * 初始构造方法
     *
     * @param dt
     */
    public MenuTreeEntity(DataTableEntity dt) {
        for (int i = 0; i < dt.getRows(); i++) {
            MenuEntity menuEntity = new MenuEntity(dt.getFieldSetEntity(i));
            Set<String> roleUuids = menuEntity.getRoleUuids();
            this.codeMenuMap.put(menuEntity.getTricode(), menuEntity);
            if (roleUuids != null) {
                for (String roleUuid : roleUuids) {
                    MenuArray menuEntities = this.roleMenuMap.get(roleUuid);
                    if (menuEntities == null) {
                        menuEntities = newArrayList();
                        this.roleMenuMap.put(roleUuid, menuEntities);
                    }
                    menuEntities.add(menuEntity);
                }
            }
            insertMenu(menuEntity, true);
        }
    }
 
    /**
     * 刷新菜单
     * 将已有菜单替换或新建
     *
     * @param dt
     */
    public void refreshMenus(DataTableEntity dt) {
        Map<String, MenuArray> roleMenuMap = Maps.newHashMap();
        if (dt != null) {
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fse = dt.getFieldSetEntity(i);
                MenuEntity menuEntity = refreshMenus(fse);
                //有角色
                if (menuEntity != null && menuEntity.getRoleUuids() != null) {
                    for (String roleUuid : menuEntity.getRoleUuids()) {
                        MenuArray menuEntities = roleMenuMap.get(roleUuid);
                        if (menuEntities == null) {
                            menuEntities = this.roleMenuMap.get(roleUuid);
                        }
                        if (menuEntities == null) {
                            menuEntities = newArrayList();
                            roleMenuMap.put(roleUuid, menuEntities);
                        }
                        menuEntities.add(menuEntity);
                    }
                    this.roleMenuMap.putAll(roleMenuMap);
                }
            }
        }
    }
 
    public void removeMenu(String[] menuUuids) {
        Set<String> roleUuids = new HashSet<>();
        if (menuUuids != null) {
            for (String menuUuid : menuUuids) {
                MenuEntity menuEntity = this.inventoryMenuMap.get(menuUuid);
                this.inventoryMenus.remove(menuUuid);
                this.inventoryMenuMap.remove(menuUuid);
                if (menuEntity == null) {
                    menuEntity = this.functionMenuMap.get(menuUuid);
                    if (menuEntity != null) {
                        this.functionMenuMap.remove(menuUuid);
                    }
                }
                this.functionMenuMap.remove(menuUuid);
                Collection<MenuArray> values = this.roleMenuMap.values();
 
                if (values != null) {
                    for (MenuArray value : values) {
                        if (value != null) {
                            MenuEntity remove = value.remove(menuUuid);
                            if (remove != null) {
                                roleUuids.addAll(remove.getRoleUuids());
                            }
                        }
                    }
                }
                if (menuEntity != null) {
                    this.codeMenuMap.remove(menuEntity.getTricode());
                }
            }
        }
        if (roleUuids != null && roleUuids.size() > 0) {
            //删除了对于角色的菜单
            UpdateLoginUserInfoService bean = SpringUtil.getBean(UpdateLoginUserInfoService.class);
            //通知对应角色的在线用户刷新菜单缓存
            bean.updateUserInfoByUpdateRole(roleUuids.toArray(), false);
        }
    }
 
    public void refreshMenus(DataTableEntity dt, String[] roleUuids, String[] menuUuids) {
        if (roleUuids != null) {
            for (String roleUuid : roleUuids) {
                this.roleMenuMap.remove(roleUuid);
            }
        }
        if (menuUuids != null) {
            for (String menuUuid : menuUuids) {
                MenuEntity menuEntity = this.inventoryMenuMap.get(menuUuid);
                this.inventoryMenus.remove(menuUuid);
                this.inventoryMenuMap.remove(menuUuid);
                Collection<MenuArray> values = this.roleMenuMap.values();
                if (values != null) {
                    for (MenuArray value : values) {
                        if (value != null) {
                            value.remove(menuUuid);
                        }
                    }
                }
                if (menuEntity != null) {
                    this.codeMenuMap.remove(menuEntity.getTricode());
                }
            }
        }
        refreshMenus(dt);
    }
 
    /**
     * 刷新菜单
     * 菜单替换 或 新建
     *
     * @param fse
     */
    protected MenuEntity refreshMenus(FieldSetEntity fse) {
        String menu_uuid = fse.getString("menu_uuid");
        MenuEntity menuEntity = null;
        // 判断是否是功能菜单(非目录)
        if (!StringUtils.isEmpty(fse.getString(CmnConst.FUNCTION_UUID))) {
            menuEntity = this.functionMenuMap.get(menu_uuid);
        } else if (!StringUtils.isEmpty(menu_uuid)) {
            // 非功能目录必进此代码块 否则数据有误
            // 目录
            menuEntity = this.inventoryMenuMap.get(menu_uuid);
        }
        // 已存在的,只是更新了数据
        if (menuEntity != null) {
            if (menuEntity.getTricode() != null && !menuEntity.getTricode().equals(fse.getString("tricode"))) {
                // 改变了code字段
                this.codeMenuMap.remove(menuEntity.getTricode());
            }
            menuEntity.reload(fse);
            this.codeMenuMap.put(menuEntity.getTricode(), menuEntity);
        } else {
            // 新增菜单
            return insertMenu(fse, false);
        }
        return menuEntity;
    }
 
    protected static MenuArray newArrayList(Collection<MenuEntity> c) {
        MenuArray arrayList = new MenuArray();
        if (c != null) {
            arrayList.addAll(c);
        }
        return arrayList;
    }
 
    protected static MenuArray newArrayList() {
        return new MenuArray();
    }
 
    /**
     * 重写菜单排序
     *
     * @return
     */
    protected static Comparator comparator() {
        return new menuSort();
    }
 
    protected static class menuSort implements Comparator<MenuEntity>, Serializable {
        /**
         * 序列化一定要提供固定版本号
         */
        private static final long serialVersionUID = 1L;
 
        @Override
        public int compare(MenuEntity a, MenuEntity b) {
            return a.getTricode().length() == b.getTricode().length() ?
                    (a.getSequence() == b.getSequence() ?
                            a.getTricode().compareTo(b.getTricode()) : a.getSequence() - b.getSequence()) :
                    a.getTricode().length() - b.getTricode().length();
        }
    }
 
 
    public static class MenuArray extends ArrayList<MenuEntity> implements java.io.Serializable {
 
        private Map<String, MenuEntity> m = new HashMap<>();
 
        public MenuArray() {
 
        }
 
        public Map<String, MenuEntity> getM() {
            return m;
        }
 
        public void setM(Map<String, MenuEntity> m) {
            this.m = m;
        }
 
        public boolean isExist(MenuEntity o) {
            for (int i = 0; i < this.size(); i++) {
                MenuEntity menuEntity = this.get(i);
                if (menuEntity.getMenu_uuid().equals(o.getMenu_uuid())) {
                    this.set(i, o);
                    return true;
                }
            }
            return false;
        }
 
 
        @Override
        public boolean add(MenuEntity menuEntity) {
            if (!isExist(menuEntity)) {
                //不存在存在
                super.add(menuEntity);
                this.sort(comparator());
                m.put(menuEntity.getMenu_uuid(), menuEntity);
            }
            return true;
        }
 
        @Override
        public boolean addAll(Collection<? extends MenuEntity> c) {
            MenuArray list = new MenuArray();
            for (MenuEntity menuEntity : c) {
                if (!isExist(menuEntity)) {
                    list.add(menuEntity);
                    add(menuEntity);
                }
            }
            if (list.size() > 0) {
                this.sort(comparator());
                return true;
            } else {
                return false;
            }
        }
 
        /**
         * 根据菜单uuid 删除
         *
         * @param menuUuid
         */
        public MenuEntity remove(String menuUuid) {
            MenuEntity menuEntity = m.get(menuUuid);
            if (menuEntity != null) {
                this.remove(this.indexOf(menuEntity));
 
            }
            return menuEntity;
        }
    }
 
 
    /**
     * 增加菜单
     *
     * @param fse
     */
    protected MenuEntity insertMenu(Object fse, boolean first) {
        MenuEntity menuEntity = null;
        // 判断传入类型
        if (fse instanceof MenuEntity) {
            menuEntity = (MenuEntity) fse;
        } else if (fse instanceof FieldSetEntity) {
            menuEntity = new MenuEntity((FieldSetEntity) fse);
        } else {
            throw new BaseException("", "insert menu error. type mismatch. ");
        }
        // 是否目录
        if (menuEntity.IsCatalogue()) {
            this.inventoryMenuMap.put(menuEntity.getMenu_uuid(), menuEntity);
            this.inventoryMenus.add(menuEntity);
        } else {
            this.functionMenuMap.put(menuEntity.getMenu_uuid(), menuEntity);
        }
        if (first && menuEntity.getRoleUuids() != null) {
            // 添加到功能所属菜单集合中
            for (String roleUuid : menuEntity.getRoleUuids()) {
                MenuArray menuEntities = this.roleMenuMap.get(roleUuid);
                if (menuEntities == null) {
                    menuEntities = newArrayList();
                    this.roleMenuMap.put(roleUuid, menuEntities);
                }
                menuEntities.add(menuEntity);
            }
        }
        return menuEntity;
 
    }
 
 
    /**
     * 创建菜单树
     *
     * @param menuEntityList
     * @return
     */
    protected MenuArray createMenuTree(MenuArray menuEntityList) {
        if (menuEntityList != null && menuEntityList.size() <= 0) {
            return null;
        }
        MenuArray treeData = newArrayList();
        Map<String, MenuEntity> parent_treeData = Maps.newHashMap();
        for (MenuEntity data : menuEntityList) {
            if (data == null) {
                continue;
            }
            String pcode = data.getTricode_parent();
            if (StringUtils.isEmpty(pcode) || parent_treeData.get(pcode) == null) {
 
                treeData.add(data);
            } else {//  上级
                MenuEntity pm = parent_treeData.get(pcode);
                MenuArray subs = pm.getChildren();
                MenuArray sub_data = null;
                if (subs == null) {
                    sub_data = newArrayList();
                    pm.setChildren(sub_data);
                } else {
                    sub_data = subs;
                }
                sub_data.add(data);
            }
            parent_treeData.put(data.getTricode(), data);
        }
        return treeData;
    }
 
    /**
     * 删除空目录
     */
    protected void removeEmptyInventory(MenuArray menuEntityList) {
        if (menuEntityList != null) {
            Iterator<MenuEntity> iterator = menuEntityList.iterator();
            while (iterator.hasNext()) {
                MenuEntity menuEntity = iterator.next();
                if (menuEntity.IsCatalogue() && (menuEntity.getChildren() == null || menuEntity.getChildren().size() <= 0 || !menuEntity.isShow())) {
                    iterator.remove();
                } else if (menuEntity.IsCatalogue()) {
                    removeEmptyInventory(menuEntity.getChildren());
                    if (menuEntity.getChildren() == null || menuEntity.getChildren().size() <= 0) {
                        iterator.remove();
                    }
                }
            }
 
        }
    }
 
    /**
     * 获取菜单树
     *
     * @param role_uuid
     * @return
     */
    public MenuArray getMenuTree(String[] role_uuid, int user_type) {
        if (role_uuid == null || role_uuid.length <= 0) {
            return null;
        }
        MenuArray functionSet = newArrayList();
 
        for (String role : role_uuid) {
            MenuArray menuEntities = this.roleMenuMap.get(role);
            if (menuEntities != null) {
                if (0 == user_type) {
                    for (MenuEntity menuEntity : menuEntities) {
                        //普通用户允许访问的菜单绑定的功能类型
                        if (StringUtils.equalsAny(menuEntity.getFunction_type(), "1", "5")) {
                            functionSet.add(menuEntity);
                        }
                    }
                } else {
                    functionSet.addAll(menuEntities);
                }
 
            }
        }
        if (functionSet.size() <= 0) {
            return null;
        }
        MenuArray inventoryMenus = newArrayList(this.inventoryMenus);
        for (MenuEntity menuEntity : functionSet) {
            inventoryMenus.add(menuEntity);
        }
        inventoryMenus.addAll(functionSet);
 
        inventoryMenus = newArrayList(inventoryMenus);
        MenuArray inventoryMenuTree = createMenuTree(inventoryMenus);
        removeEmptyInventory(inventoryMenuTree);
        return inventoryMenuTree;
    }
 
}