shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
package com.product.admin.service;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.admin.entity.*;
import com.product.admin.service.idel.ISystemMenusService;
import com.product.common.lang.DateUtils;
import com.product.common.lang.StringUtils;
import com.product.core.cache.util.RedisUtil;
import com.product.core.config.CoreConst;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.core.util.BeanRefUtil;
import com.product.core.util.JsonUtil;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * Copyright LX-BASE
 *
 * @Title: SystemMenusService
 * @Project: LX-BASE-SERVER
 * @Date: 2020-06-09 14:20
 * @Author: ZhouJie
 * @Description: 菜单管理service层
 */
@Service
public class SystemMenusService extends AbstractBaseService implements ISystemMenusService {
 
    @Autowired
    public BaseDao baseDao;
    @Autowired
    public RolesService rolesService;
    @Autowired
    CodeService codeService;
    @Autowired
    UpdateLoginUserInfoService infoService;
 
    @Autowired
    public RouterService routerService;
 
    @Autowired
    UpdateLoginUserInfoService updateLoginUserInfoService;
 
 
    private static final String MENU_CACHE_KEY = "system-cache:menus";
 
    public void refreshOnlineUserMenu(String function_uuid) {
        if (!StringUtils.isEmpty(function_uuid)) {
            //刷新菜单
            String sql = "  select GROUP_CONCAT(role_uuid) role_uuid from product_sys_function_permission "
                    + " where function_uuid =?  ";
            FieldSetEntity fsro = baseDao.getFieldSetEntityBySQL(sql, new Object[]{function_uuid}, false);
            if (fsro != null) {
                String role_uuid = fsro.getString("role_uuid");
                if (!StringUtils.isEmpty(role_uuid)) {
                    infoService.updateUserInfoByUpdateRole(role_uuid.split(","), true);
                }
            }
        }
    }
 
    /**
     * 初始化菜单缓存
     */
    public void initSystemMenu() {
        initSystemMenu(null, null);
    }
 
    public void refreshMenu(String uuid) {
        initSystemMenu(null, new String[]{uuid});
    }
 
    public synchronized void refreshMenuByRole(String uuid) {
        routerService.updateRouter(new String[]{uuid});
        initSystemMenu(new String[]{uuid}, null);
    }
 
    public synchronized void refreshMenuByRole(String[] uuid) {
        initSystemMenu(uuid, null);
    }
 
    public void refreshMenu() {
        initSystemMenu();
    }
 
    /**
     * 初始化菜单缓存
     *
     * @param role_uuid
     * @param menu_uuid
     */
    private void initSystemMenu(String[] role_uuid, String[] menu_uuid) {
        StringBuilder sql = new StringBuilder();
        String menuFilter = BaseUtil.buildQuestionMarkFilter("a.uuid", menu_uuid, true);
        //功能菜单
        sql.append(" SELECT c.uuid,a.uuid AS menu_uuid,a.menu_icon, a.menu_name, a.tricode, a.tricode_parent, a.menu_color ");
        sql.append(" , a.is_show, a.sequence, b.uuid AS function_uuid, b.function_name ");
        sql.append(" , b.table_uuid, c.button_name, c.uuid AS button_uuid, c.button_title, c.route_name ");
        sql.append(" , e.page_url, e.page_name, e.page_type, NULL AS portal_uuid,b.function_type_uuid function_type ");
        sql.append(" FROM product_sys_menus a ");
        sql.append(" JOIN ");
        sql.append(" product_sys_functions b ");
        sql.append(" ON ");
        sql.append(" a.function_uuid = b.uuid AND a.is_show = 1 and b.status_uuid=1 ");
        if (menu_uuid != null && menu_uuid.length > 0) {
            sql.append(" AND ");
            sql.append(menuFilter);
        }
        sql.append(" JOIN ");
        sql.append(" ( ");
        sql.append(" select ");
        sql.append(" button.uuid , button_title,button_name,route_name,button.function_uuid ");
        sql.append(" from ");
        sql.append(" product_sys_function_buttons button ");
        sql.append(" join product_sys_function_permission permission  on button.status_uuid=1 and  is_main=1 and button.client_type_uuid='Web' and  concat(',',permission.button_uuid,',') like concat(',%',button.uuid,'%,') ");
        String roleFilter = "";
        if (role_uuid != null && role_uuid.length > 0) {
            roleFilter = BaseUtil.buildQuestionMarkFilter("role_uuid", role_uuid, true);
            sql.append(" AND ");
            sql.append(roleFilter);
        }
        sql.append(" GROUP BY button.uuid , button_title,button_name,route_name,button.function_uuid ");
        sql.append(" ) c ");
        sql.append(" ON ");
        sql.append(" c.function_uuid = b.uuid ");
        sql.append(" JOIN ");
        sql.append(" product_sys_link d ");
        sql.append(" ON ");
        sql.append(" d.line_from = c.uuid AND d.from_type = 0 ");
        sql.append(" JOIN ");
        sql.append(" product_sys_mvc_page e ");
        sql.append(" ON ");
        sql.append(" e.uuid = d.line_to ");
        if (role_uuid == null || role_uuid.length <= 0) {
            sql.append(" UNION ALL ");
            sql.append(" SELECT null,a.uuid AS menu_uuid, a.menu_icon, a.menu_name, a.tricode, a.tricode_parent, a.menu_color ");
            sql.append(" , a.is_show, a.sequence, NULL, NULL ");
            sql.append(" , NULL, NULL, NULL, NULL, NULL ");
            sql.append(" , NULL, NULL, NULL, f.uuid ,null");
            sql.append(" FROM ");
            sql.append(" product_sys_menus a ");
            sql.append(" LEFT JOIN ");
            sql.append(" product_sys_portals f ");
            sql.append(" ON ");
            sql.append(" f.portal_menu = a.tricode ");
            sql.append(" WHERE function_uuid is  null or function_uuid=''  ");
            if (menu_uuid != null && menu_uuid.length > 0) {
                sql.append(" AND ");
                sql.append(menuFilter);
            }
        }
        DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{});
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            sql.setLength(0);
            sql.append("SELECT button.uuid, role_uuid  ");
            sql.append(" FROM product_sys_function_buttons button ");
            sql.append(" JOIN product_sys_function_permission permission ON is_main = 1 and button.client_type_uuid='Web' ");
            sql.append(" AND concat( ',', permission.button_uuid, ',' ) LIKE concat( ',%', button.uuid, '%,' ) ");
            if (!StringUtils.isEmpty(roleFilter)) {
                sql.append(" and ").append(roleFilter);
            }
            DataTableEntity roleTempDt = baseDao.listTable(sql.toString(), new Object[]{});
            if (BaseUtil.dataTableIsEmpty(roleTempDt)) {
                return;
            }
            dt.getFieldSets().forEach((k, v) -> {
                List<FieldSetEntity> fieldSetEntity = roleTempDt.getFieldSetEntity(k);
                if (fieldSetEntity != null && fieldSetEntity.size() > 0) {
                    String uuid = "";
                    for (FieldSetEntity setEntity : fieldSetEntity) {
                        if (uuid.length() > 0) {
                            uuid += ",";
                        }
                        uuid += setEntity.getString(CmnConst.ROLE_UUID);
                    }
                    for (FieldSetEntity setEntity : v) {
                        setEntity.setValue(CmnConst.ROLE_UUID, uuid);
                    }
                }
            });
        } else {
            if (menu_uuid != null && menu_uuid.length > 0) {
                //删除菜单
                MenuTreeEntity menuTreeEntity = getMenuCache();
                menuTreeEntity.removeMenu(menu_uuid);
                setMenuCache(menuTreeEntity);
            }
            return;
        }
        MenuTreeEntity menuTreeEntity;
        if ((role_uuid != null && role_uuid.length > 0) || (menu_uuid != null && menu_uuid.length > 0)) {
            menuTreeEntity = getMenuCache();
            menuTreeEntity.refreshMenus(dt, role_uuid, menu_uuid);
        } else {
            // 删除菜单刷新所有
            menuTreeEntity = new MenuTreeEntity(dt);
        }
        setMenuCache(menuTreeEntity);
    }
 
    /**
     * 装载菜单缓存
     *
     * @param menuTreeEntity
     */
    public void setMenuCache(MenuTreeEntity menuTreeEntity) {
        if (menuTreeEntity == null && RedisUtil.exists(MENU_CACHE_KEY)) {
            RedisUtil.del(MENU_CACHE_KEY);
        } else if (menuTreeEntity != null) {
            RedisUtil.set(MENU_CACHE_KEY, menuTreeEntity);
        }
    }
 
    /**
     * 获取菜单缓存
     *
     * @return
     */
    public MenuTreeEntity getMenuCache() {
        if (RedisUtil.exists(MENU_CACHE_KEY)) {
            return (MenuTreeEntity) RedisUtil.get(MENU_CACHE_KEY);
        }
        return null;
    }
 
    public DataTableEntity getMenuFunction(FieldSetEntity fse) {
        String tricodeParent = fse.getString(CmnConst.TRICODE_PARENT);
//        if (StringUtils.isEmpty(tricodeParent)) {
//            return null;
//        }
        String[] parentCode = tricodeParent.split(",");
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" a.is_catalog,a.function_uuid,a.uuid,a.menu_name,a.menu_description,a.tricode,a.tricode_parent,a.menu_icon,a.is_show ");
        if (!StringUtils.isEmpty(tricodeParent)) {
            sql.append(" ,b.uuid parent_uuid,b.menu_name directory_name ");
            sql.append(" FROM product_sys_menus a");
            sql.append("  JOIN product_sys_menus b ");
            sql.append(" ON a.tricode_parent=b.tricode and length(a.function_uuid)>0 ");
            sql.append(" and ").append(BaseUtil.buildQuestionMarkFilter("b.tricode", parentCode.length, true));
        } else {
            sql.append(" FROM product_sys_menus a");
            sql.append(" where (a.tricode_parent is null or a.tricode_parent='') and length(a.function_uuid)>0 ");
            parentCode = null;
        }
        sql.append(" order by a.sequence asc ");
        DataTableEntity dt = baseDao.listTable(sql.toString(), parentCode);
        baseDao.loadPromptData(dt);
        return dt;
    }
 
 
    public DataTableEntity getMenuDirectory() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" a.uuid,a.menu_name,a.tricode,a.tricode_parent,a.menu_icon,a.is_catalog,a.sequence ");
        sql.append(" ,b.uuid parent_uuid ");
        sql.append(" FROM product_sys_menus a");
        sql.append(" LEFT JOIN product_sys_menus b ");
        sql.append(" ON a.tricode_parent=b.tricode ");
        sql.append(" where  (a.function_uuid is null or a.function_uuid ='')");
        sql.append(" order by length(a.tricode),a.sequence asc ,a.tricode ");
        DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{});
        return BaseUtil.dataTableToTreeTable(dt, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, null);
    }
 
    public DataTableEntity getMenuChildren(FieldSetEntity fse) {
        String tricode = fse.getString(CmnConst.TRICODE);
        if (StringUtils.isEmpty(tricode)) {
            return null;
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_MENUS, "(function_uuid is null or function_uuid ='' ) and tricode like concat(?,'-%')", new Object[]{tricode},
                new Object[]{"uuid,menu_name,tricode,tricode_parent,menu_icon,is_catalog"}, CmnConst.SEQUENCE);
        return BaseUtil.dataTableToTreeTable(dt, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, null);
    }
 
    /**
     * 新增菜单
     *
     * @param fs
     * @return
     * @throws BaseException
     */
    @Transactional
    public FieldSetEntity addMenu(FieldSetEntity fs) throws BaseException {
        codeService.createCode(fs, CmnConst.PRODUCT_SYS_MENUS, CmnConst.TRICODE, !StringUtils.isEmpty(fs.getString(CmnConst.TRICODE_PARENT)) ? fs.getString(CmnConst.TRICODE_PARENT) : "");
        fs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 获取登录帐号
        fs.setValue(CmnConst.CREATED_UTC_DATETIME, DateUtils.getDateTime());// 创建时间
        baseDao.add(fs);
 
        return fs;
    }
 
    /**
     * 修改菜单
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean updateMenu(FieldSetEntity fse) throws BaseException {
        FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_MENUS, fse.getString(CmnConst.UUID), false);
        String new_parent_code = fse.getString(CmnConst.TRICODE_PARENT);
        String parent_code = fss.getString(CmnConst.TRICODE_PARENT);
        fse.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());// 获取登录帐号
        fse.setValue("updated_utc_datetime", DateUtils.getDateTime());// 创建时间
        List<Object> updateCacheUuid = new ArrayList<>();
        if (!StringUtils.isEmpty(new_parent_code) || !StringUtils.isEmpty(parent_code)) {
            boolean status = false;
            if ((StringUtils.isEmpty(new_parent_code) && !StringUtils.isEmpty(parent_code)) || (!StringUtils.isEmpty(new_parent_code) && StringUtils.isEmpty(parent_code)) || !parent_code.equals(new_parent_code)) {
                // 改编码
                status = true;
            }
            if (status) {
                codeService.createCode(fse, CmnConst.PRODUCT_SYS_MENUS, CmnConst.TRICODE,
                        !StringUtils.isEmpty(new_parent_code) ? new_parent_code : "");
                //当前菜单所有下级
                DataTableEntity d = baseDao.listTable("SELECT uuid,tricode,tricode_parent FROM product_sys_menus where tricode_parent like concat(?,'%') ORDER BY tricode", new Object[]{fss.getString(CmnConst.TRICODE)});
                if (!BaseUtil.dataTableIsEmpty(d)) {
                    // key = 旧编码  value = 新编码
                    Map<String, String> codeMap = Maps.newHashMap();
                    codeMap.put(fss.getString(CmnConst.TRICODE), fse.getString(CmnConst.TRICODE));
                    for (int i = 0; i < d.getRows(); i++) {
                        //在map中获取父级的新编码
                        String newParentCode = codeMap.get(d.getString(i, CmnConst.TRICODE_PARENT));
                        String fixCode = codeService.createFixCode(CmnConst.PRODUCT_SYS_MENUS, CmnConst.TRICODE, newParentCode);
                        //将新编码放入map中
                        codeMap.put(d.getString(i, CmnConst.TRICODE), fixCode);
                        d.setFieldValue(i, CmnConst.TRICODE, fixCode);
                        d.setFieldValue(i, CmnConst.TRICODE_PARENT, newParentCode);
                    }
                    if (codeMap.size() > 1) {
                        baseDao.update(d);
                        Object[] uuids = d.getUuids();
                        updateCacheUuid.addAll(Arrays.asList(uuids));
                    }
                }
            }
        }
        // 处理文件删除
        if (!StringUtils.isEmpty(fse.getString("del"))) {
            String[] dels = fss.getString("del").split(",");
            baseDao.delete("product_sys_attachments", BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, dels.length, true),
                    dels);
        }
        //周杰  2020年12月17日   上午09:39
        boolean flag = baseDao.update(fse);
        updateCacheUuid.add(fse.getUUID());
        if (flag) {
            //菜单被隐藏时 隐藏子级菜单
            if (!"1".equals(fse.getString("is_show"))) {
                //菜单是否有效,子菜单同主菜单
                String usql = " UPDATE  product_sys_menus SET is_show = ? WHERE tricode_parent = ? ";
                Object args[] = {fse.getString("is_show"), fse.getString(CmnConst.TRICODE)};
                baseDao.executeUpdate(usql, args);
            }
 
 
            //刷新菜单
            String sql = " select  GROUP_CONCAT(role_uuid) role_uuid  from ( select role_uuid from product_sys_function_permission "
                    + " where function_uuid in( select function_uuid from product_sys_menus where tricode like ? ) GROUP BY role_uuid) a ";
            FieldSetEntity fsro = baseDao.getFieldSetEntityBySQL(sql, new Object[]{fse.getString(CmnConst.TRICODE) + "%"}, false);
            if (fsro != null) {
                String role_uuid = fsro.getString("role_uuid");
                if (!StringUtils.isEmpty(role_uuid)) {
                    infoService.updateUserInfoByUpdateRole(role_uuid.split(","), true);
                }
            }
        }
        return flag;
    }
 
    public JSONArray getMenuTopFunction() {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        String role_uuids = null;
        if (currentUser.getCurrentStaff() != null && !currentUser.isManager()) {
            role_uuids = currentUser.getCurrentStaff().getString("role_uuids");
        } else if (currentUser.getCurrentManager() != null && currentUser.isManager()) {
            role_uuids = currentUser.getCurrentManager().getString("role_uuids");
        }
        if (StringUtils.isEmpty(role_uuids)) {
            //角色uuid不能为空
            throw new BaseException(SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getValue(), SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getText());
        }
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" menu_name, ");
        sql.append(" uuid, ");
        sql.append(" function_uuid, ");
        sql.append(" tricode, ");
        sql.append(" SUBSTRING_INDEX( tricode_parent, '-', 1 ) tricode_parent ");
        sql.append(" FROM ");
        sql.append(" `product_sys_menus` a ");
        sql.append(" WHERE ");
        sql.append(" ( tricode_parent IS NULL OR tricode_parent = '' ) ");
        sql.append(" OR ( ");
        sql.append(" ( tricode_parent IS NOT NULL AND tricode_parent != '' ) ");
        sql.append(" AND function_uuid IN ( ");
        sql.append(" SELECT ");
        sql.append(" b.function_uuid ");
        sql.append(" FROM ");
        sql.append(" product_sys_function_permission p ");
        sql.append(" JOIN product_sys_function_buttons b ON concat( ',', p.button_uuid, ',' ) LIKE concat( '%,', b.uuid, ',%' ) ");
        sql.append(" AND b.button_category_uuid = 'main' where  ");
        sql.append(BaseUtil.buildQuestionMarkFilter("p.role_uuid", role_uuids.split(",").length, true));
        sql.append(" ) ");
        sql.append(" ) ");
        sql.append(" ORDER BY ");
        sql.append(" tricode_parent, ");
        sql.append(" sequence ");
        DataTableEntity dt = baseDao.listTable(sql.toString(), role_uuids.split(","));
        return BaseUtil.dataTableToTreeData(dt, "tricode", "tricode_parent", null, true);
    }
 
    /**
     * @Date: 2020-06-10 10:12
     * @Author: ZhouJie
     * @Description: 查询菜单详情
     */
    public FieldSetEntity findMenu(FieldSetEntity fse) throws BaseException {
        FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_MENUS, fse.getString(CmnConst.UUID), true);
        return baseDao.listInternationDataTable(fs, null);
    }
 
    /**
     * 树型菜单
     *
     * @return
     */
    public Object treeMenu() {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        String role_uuids = null;
        if (currentUser.getCurrentStaff() != null && !currentUser.isManager()) {
            role_uuids = currentUser.getCurrentStaff().getString("role_uuids");
        } else if (currentUser.getCurrentManager() != null && currentUser.isManager()) {
            role_uuids = currentUser.getCurrentManager().getString("role_uuids");
        }
        if (StringUtils.isEmpty(role_uuids)) {
            //角色uuid不能为空
            throw new BaseException(SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getValue(), SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getText());
        }
        String[] role_uuid = role_uuids.split(",");
        MenuTreeEntity menuCache = getMenuCache();
        if (menuCache != null) {
            List<MenuEntity> menuTree = menuCache.getMenuTree(role_uuid, currentUser.getUserType());
            if (menuTree == null || menuTree.size() <= 0) {
                throw new BaseException(SystemCode.SYSTEM_USER_MENU_FAIL.getValue(), SystemCode.SYSTEM_USER_MENU_FAIL.getText());
            }
            return menuTree;
        }
        throw new BaseException(SystemCode.SYSTEM_USER_MENU_FAIL.getValue(), SystemCode.SYSTEM_USER_MENU_FAIL.getText());
    }
 
    public JSONObject getMenu() {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        if (currentUser != null) {
            JSONObject object = new JSONObject();
            object.put("menus", treeMenu());
            object.put("router", routerService.getRouter());
            return object;
        }
        return null;
    }
 
 
    public void setMenuCache(JSONArray menus) throws BaseException {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        if (currentUser != null) {
            String cache_key = "user:menu:" + currentUser.getUser_id();
            RedisUtil.set(cache_key, menus);
        }
    }
 
    public static void deleteEmptyTree(JSONArray array) {
        if (array != null && !array.isEmpty()) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                if (StringUtils.isEmpty(jsonObject.getString(CmnConst.FUNCTION_UUID))) {
                    deleteEmptyTree(jsonObject.getJSONArray("children"));
                    if (jsonObject.getJSONArray("children") == null || jsonObject.getJSONArray("children").isEmpty()) {
                        array.remove(i);
                        i--;
                        continue;
                    }
                }
            }
        }
    }
 
    /**
     * @throws BaseException
     * @Date: 2020-06-10 10:12
     * @Author: ZhouJie
     * @Description: 菜单列表
     */
    public JSONArray listMenu() throws BaseException {
        // 2020年12月25日10:26:40 xupengcheng  优化菜单国际化
        if (SpringMVCContextHolder.getCurrentUser() != null) {
            DataTableEntity data = baseDao.listTable(CmnConst.PRODUCT_SYS_MENUS, null, "length(tricode),sequence asc");
            if (!BaseUtil.dataTableIsEmpty(data)) {
                baseDao.listInternationDataTable(data, null);
                baseDao.loadPromptData(data);
                return MenusTree(data, null);
            }
            return new JSONArray();
        }
        // 用户获取失败
        throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(),
                SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), "listMenu");
    }
 
    public JSONArray MenusTree(DataTableEntity data, String dept_parent) throws BaseException {
        // 2020年12月25日10:26:40 xupengcheng  优化菜单国际化
        JSONArray top_array = new JSONArray();
        if (data != null && data.getRows() > 0) {
            for (int i = 0; i < data.getRows(); i++) {
                FieldSetEntity fs = data.getFieldSetEntity(i);
                if (StringUtils.isEmpty(fs.getString(CmnConst.TRICODE_PARENT)) || (!StringUtils.isEmpty(dept_parent)
                        && dept_parent.equals(fs.getString(CmnConst.TRICODE_PARENT)))) {
                    JSONObject menu_top = new JSONObject();
                    menu_top.put(CmnConst.UUID, fs.getString(CmnConst.UUID));
                    menu_top.put(CmnConst.MENU_NAME, fs.getString("menu_name"));
                    menu_top.put(CmnConst.TRICODE, fs.getString(CmnConst.TRICODE));
                    menu_top.put(CmnConst.TRICODE_PARENT, fs.getString(CmnConst.TRICODE_PARENT));
                    menu_top.put(CmnConst.IS_SHOW, fs.getString(CmnConst.IS_SHOW));
                    menu_top.put("is_catalog", fs.getString("is_catalog"));
                    menu_top.put(CmnConst.MENU_DESCRIPTION, fs.getString(CmnConst.MENU_DESCRIPTION));
                    menu_top.put("menu_route_component", fs.getString("menu_route_component"));
                    menu_top.put("function_uuid", fs.getString("function_uuid"));
                    menu_top.put("children", MenusChildrenTree(data, fs.getString(CmnConst.TRICODE)));
                    top_array.add(menu_top);
                }
            }
        }
        return top_array;
    }
 
    public JSONArray MenusChildrenTree(DataTableEntity data, String tricode_parent) throws BaseException {
        // 2020年12月25日10:26:40 xupengcheng  优化菜单国际化
        JSONArray children_array = new JSONArray();
        if (data != null && data.getRows() > 0 && !StringUtils.isEmpty(tricode_parent)) {
            for (int i = 0; i < data.getRows(); i++) {
                FieldSetEntity fs = data.getFieldSetEntity(i);
                if (tricode_parent.equals(fs.getString(CmnConst.TRICODE_PARENT))) {
                    JSONObject menu_children = new JSONObject();
                    menu_children.put(CmnConst.UUID, fs.getString(CmnConst.UUID));
                    menu_children.put(CmnConst.TRICODE, fs.getString(CmnConst.TRICODE));
                    menu_children.put(CmnConst.MENU_NAME, fs.getString("menu_name"));
                    menu_children.put(CmnConst.TRICODE_PARENT, fs.getString(CmnConst.TRICODE_PARENT));
                    menu_children.put(CmnConst.IS_SHOW, fs.getString(CmnConst.IS_SHOW));
                    menu_children.put("is_catalog", fs.getString("is_catalog"));
                    menu_children.put(CmnConst.MENU_DESCRIPTION, fs.getString(CmnConst.MENU_DESCRIPTION));
                    menu_children.put("menu_route_component", fs.getString("menu_route_component"));
                    menu_children.put("function_uuid", fs.getString("function_uuid"));
                    FieldSetEntity fss = data.getFieldSetEntity(i);
                    menu_children.put("children", MenusChildrenTree(data, fss.getString(CmnConst.TRICODE)));
                    children_array.add(menu_children);
                }
            }
        }
        return children_array;
    }
 
    /***
     * 查询顶级模块
     *
     * @param tricode        菜单code
     * @param org_level_uuid 组织架构uuid
     * @return
     */
    public DataTableEntity getMenuList(String tricode, String org_level_uuid) throws BaseException {
 
        String sql = "select menus.uuid,menus.menu_name,menus.menu_description,menus.tricode,menus.tricode_parent,menus.is_show,menus.sequence,menus.menu_icon,menus.created_by,menus.created_utc_datetime, "
                + "menus.function_uuid,functions.function_name,modules.product_uuid,products.product_name  "
                + "from product_sys_menus menus "
                + "left join product_sys_functions functions on menus.function_uuid = functions.uuid "
                + "left join product_sys_modules modules on modules.uuid = functions.module_uuid "
                + "left join product_sys_products products on modules.product_uuid = products.uuid  where 1=1 ";
        Object[] object = null;
        DataTableEntity dataTableEntity;
        //周杰 2020年12月17日 上午11:16
        if (!StringUtils.isEmpty(tricode)) {
            sql += " and menus.tricode like ? and length(menus.tricode) > " + tricode.length();
            object = new Object[]{tricode + "%"};
        }
        //周杰 2020年12月17日 上午11:16
        if (!StringUtils.isEmpty(org_level_uuid)) {
            sql += " and functions.group_uuid = ? ";
            if (object != null && object.length == 1) {
                object = new Object[]{tricode + "%", org_level_uuid};
            } else {
                object = new Object[]{org_level_uuid};
            }
        }
 
        dataTableEntity = baseDao.listTable(sql, object);
        return dataTableEntity;
    }
 
    /**
     * 查询下级菜单(只查一级)
     *
     * @param tricode_parent 子级菜单编码
     * @param org_level_uuid
     * @return
     * @throws BaseException
     */
    public DataTableEntity getChildrenMenuList(String tricode_parent, String org_level_uuid) throws BaseException {
        String sql = "select menus.uuid,menus.menu_name,menus.menu_description,menus.tricode,menus.tricode_parent,menus.is_show,menus.sequence,menus.menu_icon,menus.created_by,menus.created_utc_datetime, "
                + "menus.function_uuid,functions.function_name,modules.product_uuid,products.product_name  "
                + "from product_sys_menus menus "
                + "left join product_sys_functions functions on menus.function_uuid = functions.uuid "
                + "left join product_sys_modules modules on modules.uuid = functions.module_uuid "
                + "left join product_sys_products products on modules.product_uuid = products.uuid  where menus.tricode_parent=?";
        Object[] objects = new Object[]{tricode_parent};
        //周杰 2020年12月17日 上午11:16
        if (!StringUtils.isEmpty(org_level_uuid)) {
            sql += "  and functions.group_uuid=? ";
            objects = new Object[]{tricode_parent, org_level_uuid};
        }
        return baseDao.listTable(sql, objects);
    }
 
// ================后台管理返回菜单树======================start=============================================================
 
    /***
     * 后台管理封装菜单树结构
     *
     * @param dataTableEntity 需要封装的数据
     * @param tricode_parent  上级code
     * @return
     * @throws BaseException
     */
    public List<MenuTree> encapsulationTreeManger(DataTableEntity dataTableEntity, String tricode_parent)
            throws BaseException {
        List<FieldSetEntity> fieldSetEntities = dataTableEntity.getData();
        List<MenuTree> list = new ArrayList<>();
        for (FieldSetEntity fieldSetEntity : fieldSetEntities) {
            MenuTree bean = new MenuTree();
            BeanRefUtil.setFieldValue(bean, fieldSetEntity.getValues());
            list.add(bean);
        }
        return listToTreeManger(list, tricode_parent);
    }
 
    /***
     * 获取菜单树结构
     *
     * @param list 数据集合
     * @return
     */
    public List<MenuTree> listToTreeManger(List<MenuTree> list, String tricode_parent) {
        List<MenuTree> treeList = new ArrayList<>();
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(tricode_parent)) {
            tricode_parent = "0";
        }
 
        for (MenuTree tree : list) {
            if (tree.getTricode_parent().equals(tricode_parent)) {
                tree.setMrbase_sys_menus(new ArrayList<>());
                treeList.add(findChildrenManger(tree, list));
            }
        }
        return treeList;
    }
 
    /***
     * 获取下级菜单树结构
     *
     * @param tree 当前节点数据
     * @param list 所有节点数据
     * @return
     */
    private static MenuTree findChildrenManger(MenuTree tree, List<MenuTree> list) {
        for (MenuTree node : list) {
            if (node.getTricode_parent().equals(tree.getTricode())) {
                if (tree.getMrbase_sys_menus() == null) {
                    tree.setMrbase_sys_menus(new ArrayList<>());
                }
                tree.getMrbase_sys_menus().add(findChildrenManger(node, list));
            }
        }
        if (tree.getMrbase_sys_menus() == null) {
            tree.setMrbase_sys_menus(new ArrayList<>());
        }
        return tree;
    }
 
// ================后台管理返回菜单树======================start=============================================================
 
// ================登录返回菜单============================start==============================================================
 
    /**
     * 根据当前人查询拥有权限的菜单
     *
     * @param user
     * @return
     * @throws BaseException
     */
    public List<LoginMenu> getMenuTreeByuserUuid(SystemUser user) throws BaseException {
        DataTableEntity dt = this.getMenuCurrentUserListLogin(user);
        List<LoginMenu> treeMenuList = this.encapsulationTree(dt);
        return treeMenuList;
    }
 
    /**
     * @param user
     * @return
     * @throws BaseException
     */
    public DataTableEntity getMenuCurrentUserListLogin(SystemUser user) throws BaseException {
        DataTableEntity dt = rolesService.functionsByUser(user);
        if (dt != null) {
            String param = "";
            for (int i = 0; i < dt.getRows(); i++) {
                String function_uuid = dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID);
                if (function_uuid == null || function_uuid.equals("")) {
                    continue;
                }
                if (i != 0) {
                    param += ",'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'";
                } else {
                    param += "'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'";
                }
            }
            if (param.equals("")) {
                return null;
            }
            // 查出关联功能的菜单及父给菜单
            String sql1 = "select menus.uuid,menus.menu_name,menus.tricode,menus.tricode_parent,menus.function_uuid,menus.menu_icon "
                    + " from product_sys_menus menus " + " where menus.is_show = 1 " + " and menus.function_uuid in ("
                    + param + ")" + " union all select b.uuid,b.menu_name,b.tricode,b.tricode_parent,b.function_uuid,b.menu_icon from ( "
                    + " (SELECT DISTINCT CASE WHEN length( a.tricode ) > 3 THEN SUBSTRING( a.tricode, 1, 3 ) END AS code1, "
                    + " CASE WHEN length( a.tricode ) > 7 THEN SUBSTRING( a.tricode, 1, 7 )END AS code2 "
                    + " FROM product_sys_menus a" + " WHERE a.is_show = 1 " + " AND a.function_uuid IN ( " + param
                    + " ))as c)"
                    + " right JOIN product_sys_menus b ON ( c.code1 = b.tricode OR c.code2 = b.tricode ) where c.code1 is not null and b.is_show=1";
            DataTableEntity dtm = baseDao.listTable(sql1, new Object[]{});
            if (dtm.getRows() > 0) {
                for (int i = 0; i < dtm.getRows(); i++) {
                    FieldSetEntity fss = dtm.getFieldSetEntity(i);
                    FieldSetEntity fsf = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS,
                            "function_uuid=? and is_main=?", new Object[]{fss.getString(CmnConst.FUNCTION_UUID), 1}, false);
                    if (fsf != null) {
                        fss.setValue(CmnConst.BUTTON_NAME, fsf.getString(CmnConst.BUTTON_NAME));
                        fss.setValue(CmnConst.URL, fsf.getString(CmnConst.URL));
                        fss.setValue(CmnConst.BUTTON_COMPONENT, fsf.getString(CmnConst.BUTTON_COMPONENT));
                    }
 
                    DataTableEntity menu_names = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES,
                            "field_name=? and data_uuid=?", new String[]{CmnConst.MENU_NAME, fss.getString(CmnConst.UUID)});
                    if (menu_names.getRows() > 0) {
                        List<Map<String, String>> menus = new ArrayList<Map<String, String>>();
                        for (int j = 0; j < menu_names.getRows(); j++) {
                            Map<String, String> language_names = new HashMap<String, String>();
                            FieldSetEntity menu_name = menu_names.getFieldSetEntity(j);
                            language_names.put(menu_name.getString(CmnConst.LANGUAGE_CODE),
                                    menu_name.getString("field_value"));
                            menus.add(language_names);
                        }
                        fss.setValue(CmnConst.LANGUAGE_NAME, menus);
                    }
                }
            }
            return dtm;
        }
        return dt;
    }
 
    /**
     * 根据当前人查询拥有权限的指定客户端菜单
     *
     * @param client_type 客户端类型
     * @param user        当前用户
     * @return
     * @throws BaseException
     */
    public List<Map<Object, Object>> getMenuTreeByuserUuid(SystemUser user, Object client_type, Object language_code)
            throws BaseException {
        DataTableEntity dt = this.getMenuCurrentUserListLogin(user, client_type, language_code);
        return this.convertMenuInternation(dt);
    }
 
    /**
     * 菜单国际化转换
     *
     * @param dt
     * @return
     */
    private List<Map<Object, Object>> convertMenuInternation(DataTableEntity dt) throws BaseException {
        List<Map<Object, Object>> menus = new ArrayList<Map<Object, Object>>();
        // 封装上下级菜单
        Map<Object, Map<Object, Object>> parent_menu = new HashMap<Object, Map<Object, Object>>();
        if (dt != null && dt.getRows() > 0) {
            for (int j = 0; j < dt.getRows(); j++) {
                FieldSetEntity menu_name = dt.getFieldSetEntity(j);
                Map<Object, Object> menu = JsonUtil.parseJsonFieldSetToMap(menu_name);
                String pcode = menu_name.getString(CmnConst.TRICODE_PARENT);
                if (StringUtils.isEmpty(pcode) || parent_menu.get(pcode) == null) {
                    menus.add(menu);
                } else {// 上级菜单
                    Map<Object, Object> pm = parent_menu.get(pcode);
                    Object subs = pm.get("menus_tree");
                    List<Map<Object, Object>> submenus = null;
                    if (subs == null) {
                        submenus = new ArrayList<Map<Object, Object>>();
                        pm.put("menus_tree", submenus);
                    } else {
                        submenus = (List<Map<Object, Object>>) subs;
                    }
                    submenus.add(menu);
                }
                parent_menu.put(menu_name.getString(CmnConst.TRICODE), menu);
            }
        }
        return menus;
    }
 
    /**
     * 查指定客户类型的当前用户的菜单
     *
     * @param user        当前用户
     * @param client_type 客户端类型
     * @return
     * @throws BaseException
     */
    public DataTableEntity getMenuCurrentUserListLogin(SystemUser user, Object client_type, Object language_code)
            throws BaseException {
        StringBuilder function = new StringBuilder();
        Vector<String> fp = new Vector<String>();
        String role_uuids = null;
        if (user.getCurrentStaff() != null) {
            role_uuids = user.getCurrentStaff().getString("role_uuids");
        } else if (user.getCurrentManager() != null) {
            role_uuids = user.getCurrentManager().getString("role_uuids");
        }
        DataTableEntity mt = new DataTableEntity();
        if (!StringUtils.isEmpty(role_uuids)) {
            function.append(
                            " select  function_uuid uuid  from product_sys_functions a left join product_sys_function_permission b on a.uuid=b.function_uuid where CONCAT(',',client_type_uuid,',') like ? ")
                    .append(" and  b.role_uuid in (");
            fp.add("%," + client_type + ",%");
            String rs[] = role_uuids.split(",");
            for (int i = 0; i < rs.length; i++) {
                fp.add(rs[i]);
                if (i > 0)
                    function.append(",");
                function.append("?");
            }
            function.append(")");
        }
        if (function.length() > 0) {
            DataTableEntity dt = baseDao.listTable(function.toString(), fp.toArray());
            if (dt == null)
                return mt;
            Object function_uuids[] = dt.getUuids("product_sys_function_permission");
            if (function_uuids == null)
                return mt;
            StringBuilder s = new StringBuilder();
            List<Object> v = new ArrayList<>();
            for (int i = 0; i < function_uuids.length * 3; i++) {
                if (i < function_uuids.length) {
                    if (s.length() > 0)
                        s.append(",");
                    s.append("?");
                }
                v.add(function_uuids[i % function_uuids.length]);
            }
            if (function_uuids != null) {
                function = new StringBuilder();
                function.append(// "select * from ( " +
                                "select m.uuid,menu_name,menu_icon,menu_color,m.sequence,tricode,tricode_parent,bt.path path ,m.function_uuid,bt.url,bt.button_name,ifnull(bt.button_component,'/') routing_path ")
                        .append(" from product_sys_menus m   ")
                        .append(" left join product_sys_function_buttons bt on bt.function_uuid=m.function_uuid and bt.is_main=1 ")
                        .append(" where m.is_show=1 and ( m.function_uuid in (").append(s).append(") ").append(" or  m.tricode in (")
                        .append(" select tricode_parent ").append(" from product_sys_menus  where function_uuid in (")
                        .append(s).append(") ").append(" union all ")
                        .append(" select SUBSTRING( tricode_parent, 1, 3 ) ").append(" from product_sys_menus ")
                        .append(" where function_uuid in (").append(s)
                        .append(") and LENGTH(tricode_parent) > 6 ) )  order by length(tricode),sequence asc,tricode asc");
                mt = baseDao.listTable(function.toString(), v.toArray());
                baseDao.listInternationDataTable(mt,
                        CoreConst.CLIENT_TYPE_WEB.equals(client_type) ? null
                                : language_code.toString());
            }
        }
 
        return mt;
    }
 
    /**
     * 返回所有菜单
     *
     * @return
     * @throws BaseException
     */
    public List<LoginMenu> getMenuTree() throws BaseException {
 
        // 封封菜单树结构
        List<LoginMenu> treeMenuList = this.encapsulationTree(this.getMenuListLogin());
 
        return treeMenuList;
    }
 
    /***
     * 帐号登录返回菜单列表
     *
     * @return
     */
    public DataTableEntity getMenuListLogin() throws BaseException {
 
        String sql = "select menu.uuid,menu.menu_name,menu.tricode,menu.tricode_parent,menu.function_uuid,menu.menu_icon "
                + " from product_sys_menus menu where menu.is_show = 1 order by menu.sequence asc";
        DataTableEntity dataTableEntity = baseDao.listTable(sql, new Object[]{});
        return dataTableEntity;
    }
 
    /***
     * 封装菜单树结构
     *
     * @param dataTableEntity 需要封装的数据
     * @return
     * @throws BaseException
     */
    public List<LoginMenu> encapsulationTree(DataTableEntity dataTableEntity) throws BaseException {
        if (dataTableEntity == null || dataTableEntity.getRows() <= 0) {
            return new ArrayList<LoginMenu>();
        }
 
        List<FieldSetEntity> fieldSetEntities = dataTableEntity.getData();
        List<LoginMenu> list = new ArrayList<>();
        for (FieldSetEntity fieldSetEntity : fieldSetEntities) {
            LoginMenu bean = new LoginMenu();
            BeanRefUtil.setFieldValue(bean, fieldSetEntity.getValues());
            bean.setLanguage_name((List<?>) fieldSetEntity.getValue(CmnConst.LANGUAGE_NAME));
            list.add(bean);
        }
        return listToTree(list);
    }
 
    /***
     * 获取菜单树结构
     *
     * @param list 数据集合
     * @return
     */
    public List<LoginMenu> listToTree(List<LoginMenu> list) {
        List<LoginMenu> treeList = new ArrayList<>();
        for (LoginMenu tree : list) {
            if (tree.getTricode_parent() == null || "0".equals(tree.getTricode_parent())) {
                tree.setMenusTree(new ArrayList<>());
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }
 
    /***
     * 获取下级菜单树结构
     *
     * @param tree 当前节点数据
     * @param list 所有节点数据
     * @return
     */
    private static LoginMenu findChildren(LoginMenu tree, List<LoginMenu> list) {
        for (LoginMenu node : list) {
            if (node.getTricode_parent() != null && node.getTricode_parent().equals(tree.getTricode())) {
                if (tree.getMenusTree() == null) {
                    tree.setMenusTree(new ArrayList<>());
                }
                tree.getMenusTree().add(findChildren(node, list));
            }
        }
        if (tree.getMenusTree() == null) {
            tree.setMenusTree(new ArrayList<>());
        }
        return tree;
    }
 
// ================登录返回菜单============================strt==============================================================
 
    /**
     * 删除菜单
     *
     * @param fs
     * @throws BaseException
     */
    @Transactional
    public boolean deleteMenus(FieldSetEntity fse) throws BaseException {
        String uuid = fse.getString(CmnConst.UUID);
        FieldSetEntity fs = baseDao.getFieldSetBySQL(
                "SELECT uuid,(select 1 from product_sys_menus b where b.tricode like concat(a.tricode,'-%') limit 1) is_chidren from product_sys_menus a where uuid=?",
                new Object[]{uuid},
                false);
        if (FieldSetEntity.isEmpty(fs)) {
            return false;
        }
        if ("1".equals(fs.getString("is_chidren"))) {
            //有子级不允许删除
            throw new BaseException(SystemCode.DELETE_MENU_FAIL_CHILDREN_EXEIST);
        }
        return baseDao.delete("product_sys_menus", new String[]{uuid});
    }
 
    /**
     * @Date: 2020-10-27 10:48
     * @Author: ZhouJie
     * @Description: 获取当前登录人的所有菜单
     */
    public List<UserMenu> getUserMenu(SystemUser user) throws BaseException {
        DataTableEntity dt = this.getMenuUserList(user);
        List<UserMenu> treeMenuList = this.ncapsulationTree(dt);
        return treeMenuList;
    }
 
    public DataTableEntity getMenuUserList(SystemUser user) throws BaseException {
        DataTableEntity dt = rolesService.functionsByUser(user);
        if (dt != null) {
            String param = "";
            for (int i = 0; i < dt.getRows(); i++) {
                String function_uuid = dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID);
                if (function_uuid == null || function_uuid.equals("")) {
                    continue;
                }
                if (i != 0) {
                    param += ",'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'";
                } else {
                    param += "'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'";
                }
            }
            if (param.equals("")) {
                return null;
            }
            // 查出关联功能的菜单及父给菜单
            String sql1 = "select menus.uuid,menus.menu_name,menus.tricode,menus.tricode_parent,menus.function_uuid,menus.menu_icon,menus.menu_color,menus.sequence "
                    + " from product_sys_menus menus " + " where menus.is_show = 1 " + " and menus.function_uuid in ("
                    + param + ")" + " union all "
                    + " select b.uuid,b.menu_name,b.tricode,b.tricode_parent,b.function_uuid,b.menu_icon,b.menu_color,b.sequence from ( "
                    + " (SELECT DISTINCT CASE WHEN length( a.tricode ) > 3 THEN SUBSTRING( a.tricode, 1, 3 ) END AS code1,"
                    + " CASE WHEN length( a.tricode ) > 7 THEN SUBSTRING( a.tricode, 1, 7 ) END AS code2 "
                    + " FROM product_sys_menus a" + " WHERE a.is_show = 1 " + " AND a.function_uuid IN ( " + param
                    + " ))as c)"
                    + " right JOIN product_sys_menus b ON ( c.code1 = b.tricode OR c.code2 = b.tricode ) where c.code1 is not null and b.is_show=1";
            DataTableEntity dtm = baseDao.listTable(sql1, new Object[]{});
            if (dtm.getRows() > 0) {
                for (int i = 0; i < dtm.getRows(); i++) {
 
                    FieldSetEntity fss = dtm.getFieldSetEntity(i);
                    FieldSetEntity fsf = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS,
                            "function_uuid=? and is_main=?", new Object[]{fss.getString(CmnConst.FUNCTION_UUID), 1}, false);
                    if (fsf != null) {
                        fss.setValue("button_component", fsf.getString("button_component"));
                    }
 
                    DataTableEntity menu_names = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES,
                            "field_name=? and data_uuid=?", new String[]{CmnConst.MENU_NAME, fss.getString(CmnConst.UUID)});
                    if (menu_names.getRows() > 0) {
                        List<Map<String, String>> menus = new ArrayList<Map<String, String>>();
                        for (int j = 0; j < menu_names.getRows(); j++) {
                            Map<String, String> language_names = new HashMap<String, String>();
                            FieldSetEntity menu_name = menu_names.getFieldSetEntity(j);
                            language_names.put(menu_name.getString(CmnConst.LANGUAGE_CODE),
                                    menu_name.getString("field_value"));
                            menus.add(language_names);
                        }
                        fss.setValue("language_names", menus);
                    }
                }
            }
            return dtm;
        }
        return dt;
    }
 
    public List<UserMenu> ncapsulationTree(DataTableEntity dataTableEntity) throws BaseException {
        if (dataTableEntity == null || dataTableEntity.getRows() <= 0) {
            return new ArrayList<UserMenu>();
        }
 
        List<FieldSetEntity> fieldSetEntities = dataTableEntity.getData();
        List<UserMenu> list = new ArrayList<>();
        for (FieldSetEntity fieldSetEntity : fieldSetEntities) {
            UserMenu bean = new UserMenu();
            BeanRefUtil.setFieldValue(bean, fieldSetEntity.getValues());
            bean.setLanguage_name((List<?>) fieldSetEntity.getValue("language_names"));
 
            list.add(bean);
        }
        return listoTree(list);
    }
 
    public List<UserMenu> listoTree(List<UserMenu> list) {
        List<UserMenu> treeList = new ArrayList<>();
        for (UserMenu tree : list) {
            if (tree.getTricode_parent() == null || "0".equals(tree.getTricode_parent())) {
                tree.setMenusTree(new ArrayList<>());
                treeList.add(findChildrens(tree, list));
            }
        }
        return treeList;
    }
 
    private static UserMenu findChildrens(UserMenu tree, List<UserMenu> list) {
        for (UserMenu node : list) {
            if (node.getTricode_parent() != null && node.getTricode_parent().equals(tree.getTricode())) {
                if (tree.getMenusTree() == null) {
                    tree.setMenusTree(new ArrayList<>());
                }
                tree.getMenusTree().add(findChildrens(node, list));
            }
        }
        if (tree.getMenusTree() == null) {
            tree.setMenusTree(new ArrayList<>());
        }
        return tree;
    }
 
}