ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
package jnpf.base.controller;
 
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jnpf.base.ActionResult;
import jnpf.base.ModuleApi;
import jnpf.base.entity.ModuleEntity;
import jnpf.base.entity.SystemEntity;
import jnpf.base.model.module.*;
import jnpf.base.model.online.VisualMenuModel;
import jnpf.base.service.ModuleService;
import jnpf.base.service.SystemService;
import jnpf.base.util.visualUtil.PubulishUtil;
import jnpf.base.vo.DownloadVO;
import jnpf.base.vo.ListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.JnpfConst;
import jnpf.constant.MsgCode;
import jnpf.constant.PermissionConst;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.emnus.ModuleTypeEnum;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.flowable.TemplateApi;
import jnpf.model.UserMenuModel;
import jnpf.model.tenant.*;
import jnpf.permission.AuthorizeApi;
import jnpf.permission.model.authorize.AuthorizeVO;
import jnpf.util.*;
import jnpf.util.context.RequestContext;
import jnpf.util.treeutil.ListToTreeUtil;
import jnpf.util.treeutil.SumTree;
import jnpf.util.treeutil.TreeViewModel;
import jnpf.util.treeutil.newtreeutil.TreeDotUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * 系统功能
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Tag(name = "系统菜单", description = "menu")
@RestController
@RequestMapping("/Menu")
public class ModuleController extends SuperController<ModuleService, ModuleEntity> implements ModuleApi {
 
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CacheKeyUtil cacheKeyUtil;
    @Autowired
    private SystemService systemService;
    @Autowired
    private ConfigValueUtil configValueUtil;
    @Autowired
    private TemplateApi templateApi;
    @Autowired
    private PubulishUtil pubulishUtil;
    @Autowired
    private AuthorizeApi authorizeApi;
 
    @Operation(summary = "获取菜单列表")
    @Parameters({
            @Parameter(name = "systemId", description = "系统id", required = true)
    })
    @GetMapping("/ModuleBySystem")
    public ActionResult<ListVO<MenuListVO>> list(PaginationMenu paginationMenu) {
        String appCode = RequestContext.getAppCode();
        MenuListModel param = this.getParam(appCode, paginationMenu.getCategory(), paginationMenu.getKeyword(), paginationMenu.getType(), paginationMenu.getEnabledMark(), null, false);
        List<ModuleEntity> data = moduleService.getList(param);
        // 递归查上级
        Map<String, ModuleEntity> moduleEntityMap = data.stream().collect(Collectors.toMap(ModuleEntity::getId, Function.identity()));
        if (StringUtil.isNotEmpty(paginationMenu.getKeyword())) {
            moduleService.getParentModule(data, moduleEntityMap);
        }
        List<UserMenuModel> list = JsonUtil.getJsonToList(moduleEntityMap.values(), UserMenuModel.class);
        for (UserMenuModel item : list) {
            if (Objects.equals(item.getType(), 3)) {
                if (Objects.equals(item.getIsButtonAuthorize(), 1)
                        || Objects.equals(item.getIsColumnAuthorize(), 1)
                        || Objects.equals(item.getIsDataAuthorize(), 1)
                        || Objects.equals(item.getIsFormAuthorize(), 1)) {
                    item.setHasPermission(1);
                }
                //添加视图标识
                if (StringUtil.isNotEmpty(item.getPropertyJson())) {
                    PropertyJsonModel model = JsonUtil.getJsonToBean(item.getPropertyJson(), PropertyJsonModel.class);
                    item.setWebType(model.getWebType());
                    if (Objects.equals(model.getWebType(), 4)) {
                        item.setHasPermission(0);
                    }
                }
            } else {
                item.setHasPermission(1);
            }
        }
        list = list.stream().sorted(Comparator.comparing(UserMenuModel::getSortCode, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(UserMenuModel::getCreatorTime, Comparator.nullsLast(Comparator.reverseOrder()))).collect(Collectors.toList());
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDot(list);
        List<MenuListVO> menuvo = JsonUtil.getJsonToList(menuList, MenuListVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
    @Operation(summary = "新建系统功能")
    @Parameters({
            @Parameter(name = "moduleCrForm", description = "实体对象", required = true)
    })
    @SaCheckPermission(value = {"permission.menu", "appConfig.appMenu"}, mode = SaMode.OR)
    @PostMapping
    public ActionResult create(@RequestBody @Valid ModuleCrForm moduleCrForm) {
        SystemEntity info = systemService.getInfoByEnCode(RequestContext.getAppCode());
        moduleCrForm.setSystemId(info.getId());
        ModuleEntity entity = JsonUtil.getJsonToBean(moduleCrForm, ModuleEntity.class);
        if (entity.getUrlAddress() != null) {
            entity.setUrlAddress(entity.getUrlAddress().trim());
        }
        if (moduleService.isExistByFullName(entity, moduleCrForm.getCategory(), moduleCrForm.getSystemId())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        if (moduleService.isExistByEnCode(entity, moduleCrForm.getCategory(), moduleCrForm.getSystemId())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        if (moduleService.isExistByAddress(entity, moduleCrForm.getCategory(), moduleCrForm.getSystemId())) {
            return ActionResult.fail(MsgCode.EXIST104.get());
        }
        moduleService.create(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }
 
    @Operation(summary = "更新系统功能")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true),
            @Parameter(name = "moduleUpForm", description = "实体对象", required = true)
    })
    @SaCheckPermission(value = {"permission.menu", "appConfig.appMenu"}, mode = SaMode.OR)
    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ModuleUpForm moduleUpForm) {
        SystemEntity info = systemService.getInfoByEnCode(RequestContext.getAppCode());
        moduleUpForm.setSystemId(info.getId());
        ModuleEntity entity = JsonUtil.getJsonToBean(moduleUpForm, ModuleEntity.class);
        //判断如果是目录则不能修改类型
        ModuleEntity moduleEntity = moduleService.getInfo(id);
        if (moduleEntity != null && moduleEntity.getType() == 1 && entity.getType() != 1 && moduleService.getListByParentId(moduleEntity.getId()).size() > 0) {
            return ActionResult.fail(MsgCode.SYS016.get());
        }
        entity.setId(id);
        if (entity.getUrlAddress() != null) {
            entity.setUrlAddress(entity.getUrlAddress().trim());
        }
        if (moduleService.isExistByFullName(entity, moduleUpForm.getCategory(), moduleUpForm.getSystemId())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        if (moduleService.isExistByEnCode(entity, moduleUpForm.getCategory(), moduleUpForm.getSystemId())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        if (moduleService.isExistByAddress(entity, moduleUpForm.getCategory(), moduleUpForm.getSystemId())) {
            return ActionResult.fail(MsgCode.EXIST104.get());
        }
        boolean flag = moduleService.update(id, entity);
        if (!flag) {
            return ActionResult.fail(MsgCode.FA002.get());
        }
        return ActionResult.success(MsgCode.SU004.get());
    }
 
    @Operation(summary = "删除系统功能")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission(value = {"permission.menu", "appConfig.appMenu"}, mode = SaMode.OR)
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        ModuleEntity entity = moduleService.getInfo(id);
        if (entity != null) {
            List<ModuleEntity> list = moduleService.getList(false, new ArrayList<>(), new ArrayList<>()).stream().filter(t -> t.getParentId().equals(entity.getId())).collect(Collectors.toList());
            if (list.size() > 0) {
                return ActionResult.fail(MsgCode.SYS017.get());
            }
            moduleService.delete(entity);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }
 
    @Operation(summary = "获取菜单信息")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission(value = {"permission.menu", "appConfig.appMenu"}, mode = SaMode.OR)
    @GetMapping("/{id}")
    public ActionResult<ModuleInfoVO> info(@PathVariable("id") String id) throws DataException {
        ModuleEntity entity = moduleService.getInfo(id);
        ModuleInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, ModuleInfoVO.class);
        return ActionResult.success(vo);
    }
    //+++++++++++++++++++++++++++增删改查end+++++++++++++++++++++++++++++++++++++++++++++++
 
    /**
     * 获取菜单列表(下拉框)
     *
     * @param category 分类
     * @param id       主键
     * @return ignore
     */
    @Operation(summary = "获取菜单列表(下拉框)")
    @Parameters({
            @Parameter(name = "category", description = "分类"),
            @Parameter(name = "id", description = "主键", required = true)
    })
    @GetMapping("/Selector/{id}")
    public ActionResult<ListVO<MenuSelectVO>> treeView(String category, @PathVariable("id") String id) {
        //应用编码
        String appCode = RequestContext.getAppCode();
        MenuListModel param = getParam(appCode, category, null, 1, null, null, false);
        List<ModuleEntity> data = moduleService.getList(param);
        if (!"0".equals(id)) {
            data.remove(moduleService.getInfo(id));
        }
        List<UserMenuModel> list = JsonUtil.getJsonToList(data, UserMenuModel.class);
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDotFilter(list);
        List<MenuSelectVO> menuvo = JsonUtil.getJsonToList(menuList, MenuSelectVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
    /**
     * 获取开发平台菜单
     *
     * @return ignore
     */
    @Operation(summary = "获取开发平台菜单")
    @GetMapping("/SystemSelector")
    public ActionResult<ListVO<MenuSelectVO>> mainSystemSelector() {
        SystemEntity mainSystem = systemService.getInfoByEnCode(JnpfConst.MAIN_SYSTEM_CODE);
        MenuListModel param = getParam(mainSystem.getId(), null, null, null, 1, null, false);
        List<ModuleEntity> data = moduleService.getList(param);
        List<UserMenuModel> list = JsonUtil.getJsonToList(data, UserMenuModel.class);
        list.forEach(t -> {
            if ("-1".equals(t.getParentId())) {
                t.setParentId(t.getSystemId());
            }
        });
        UserMenuModel userMenuModel = JsonUtil.getJsonToBean(mainSystem, UserMenuModel.class);
        userMenuModel.setType(0);
        userMenuModel.setParentId("-1");
        list.add(userMenuModel);
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDotFilter(list);
        List<MenuSelectVO> menuvo = JsonUtil.getJsonToList(menuList, MenuSelectVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
    /**
     * 通过系统id获取菜单列表(下拉框)
     *
     * @param category 分类
     * @param id       主键
     * @return ignore
     */
    @Operation(summary = "通过系统id获取菜单列表(下拉框)")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "systemId", description = "系统主键", required = true),
            @Parameter(name = "enabledMark", description = "查询类型:null-全部,0-禁用,1-启用", required = false)
    })
    @GetMapping("/Selector/{id}/{systemId}")
    public ActionResult<ListVO<MenuSelectAllVO>> treeView(@PathVariable("id") String id,
                                                          @PathVariable("systemId") String systemId,
                                                          @RequestParam("category") String category,
                                                          @RequestParam(value = "enabledMark", required = false) Integer enabledMark) {
        MenuListModel param = getParam(systemId, category, null, 1, enabledMark, null, true);
        List<ModuleEntity> data = moduleService.getList(param);
        if (!"0".equals(id)) {
            data.remove(moduleService.getInfo(id));
        }
        List<UserMenuModel> list = JsonUtil.getJsonToList(data, UserMenuModel.class);
        if ("0".equals(systemId)) {
            List<String> moduleAuthorize = new ArrayList<>();
            if (configValueUtil.isMultiTenancy()) {
                TenantAuthorizeModel tenantAuthorizeModel = TenantDataSourceUtil.getCacheModuleAuthorize(UserProvider.getUser().getTenantId());
                moduleAuthorize = tenantAuthorizeModel.getModuleIdList();
            }
            List<SystemEntity> list1 = systemService.getList(null, true, false, true, false, moduleAuthorize);
            list.forEach(t -> {
                if ("-1".equals(t.getParentId())) {
                    t.setParentId(t.getSystemId());
                }
            });
            List<UserMenuModel> jsonToList = JsonUtil.getJsonToList(list1, UserMenuModel.class);
            jsonToList.forEach(t -> {
                t.setType(0);
                t.setParentId("-1");
            });
            list.addAll(jsonToList);
        }
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDotFilter(list);
        List<MenuSelectAllVO> menuvo = JsonUtil.getJsonToList(menuList, MenuSelectAllVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
    /**
     * 通过系统id获取菜单列表(下拉框)
     *
     * @param category 分类
     * @return ignore
     */
    @Operation(summary = "通过系统id获取菜单列表(下拉框)")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "systemId", description = "系统主键", required = true)
    })
    @GetMapping("/SelectorFilter/{visualId}")
    public ActionResult<ListVO<MenuSelectAllVO>> SelectorFilter(String category, @PathVariable("visualId") String visualId) {
        String appCode = RequestContext.getAppCode();
        SystemEntity systemEntity = systemService.getInfoByEnCode(appCode);
        MenuListModel param = getParam(appCode, category, null, 1, 1, null, true);
        List<ModuleEntity> data = moduleService.getList(param);
        List<ModuleEntity> moduleList = moduleService.getModuleList(visualId);
        List<String> moduleIds = new ArrayList<>();
        List<String> systemIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(moduleList)) {
            for (ModuleEntity item : moduleList) {
                if ("-1".equals(item.getParentId())) {
                    systemIds.add(item.getSystemId());
                } else {
                    moduleIds.add(item.getParentId());
                }
            }
        }
        List<UserMenuModel> list = JsonUtil.getJsonToList(data, UserMenuModel.class);
 
        List<SystemEntity> list1 = new ArrayList<>();
        list1.add(systemEntity);
        list.forEach(t -> {
            if ("-1".equals(t.getParentId())) {
                t.setParentId(t.getSystemId());
            }
        });
        List<UserMenuModel> jsonToList = JsonUtil.getJsonToList(list1, UserMenuModel.class);
        jsonToList.forEach(t -> {
            t.setType(0);
            t.setParentId("-1");
        });
        list.addAll(jsonToList);
        for (UserMenuModel userMenuModel : list) {
            if (moduleIds.contains(userMenuModel.getId()) || systemIds.contains(userMenuModel.getId())) {
                userMenuModel.setDisabled(true);
            }
        }
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDotFilter(list);
        List<MenuSelectAllVO> menuvo = JsonUtil.getJsonToList(menuList, MenuSelectAllVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
    /**
     * 获取菜单列表(下拉框)
     *
     * @param category 分类
     * @return ignore
     */
    @Operation(summary = "获取菜单列表下拉框")
    @GetMapping("/Selector/All")
    public ActionResult<ListVO<MenuSelectAllVO>> menuSelect(String category) {
        MenuListModel param = getParam(RequestContext.getAppCode(), category, null, null, 1, null, false);
        List<ModuleEntity> data = moduleService.getList(param);
        List<UserMenuModel> list = JsonUtil.getJsonToList(data, UserMenuModel.class);
        List<String> moduleAuthorize = new ArrayList<>();
        if (configValueUtil.isMultiTenancy()) {
            TenantAuthorizeModel tenantAuthorizeModel = TenantDataSourceUtil.getCacheModuleAuthorize(UserProvider.getUser().getTenantId());
            moduleAuthorize = tenantAuthorizeModel.getModuleIdList();
        }
        List<SystemEntity> list1 = systemService.getList(null, true, false, false, false, moduleAuthorize);
        list.forEach(t -> {
            t.setHasModule(!"1".equals(String.valueOf(t.getType())));
            if ("-1".equals(t.getParentId())) {
                t.setParentId(t.getSystemId());
            }
        });
        List<UserMenuModel> jsonToList = JsonUtil.getJsonToList(list1, UserMenuModel.class);
        jsonToList.forEach(t -> {
            t.setType(0);
            t.setHasModule(false);
            t.setParentId("-1");
        });
        list.addAll(jsonToList);
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDotFilter(list);
        List<MenuSelectAllVO> menuvo = JsonUtil.getJsonToList(menuList, MenuSelectAllVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
 
    /**
     * 系统功能类别树形
     *
     * @return ignore
     */
    @Operation(summary = "系统功能类别树形")
    @GetMapping("/{systemId}/TreeView")
    public ActionResult treeView(@PathVariable("systemId") String systemId) {
        MenuListModel param = getParam(systemId, null, null, null, null, "0", true);
        List<ModuleEntity> moduleList = moduleService.getList(param);
        List<TreeViewModel> treeList = new ArrayList<>();
        TreeViewModel treeViewModel = new TreeViewModel();
        treeViewModel.setId("apply");
        treeViewModel.setText("软件开发平台");
        treeViewModel.setParentId("0");
        treeViewModel.setImg("fa fa-windows apply");
        treeList.add(treeViewModel);
        for (ModuleEntity entity : moduleList) {
            TreeViewModel treeModel = new TreeViewModel();
            treeModel.setId(entity.getId());
            treeModel.setText(entity.getFullName());
            treeModel.setParentId("apply");
            treeModel.setImg("fa fa-tags");
            treeList.add(treeModel);
        }
        return ActionResult.success(ListToTreeUtil.toTreeView(treeList));
    }
 
    /**
     * 更新菜单状态
     *
     * @param id 主键值
     * @return ignore
     */
    @Operation(summary = "更新菜单状态")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission("permission.menu")
    @PutMapping("/{id}/Actions/State")
    public ActionResult upState(@PathVariable("id") String id) {
        ModuleEntity entity = moduleService.getInfo(id);
        if (entity != null) {
            if (entity.getEnabledMark() == null || "1".equals(String.valueOf(entity.getEnabledMark()))) {
                entity.setEnabledMark(0);
            } else {
                entity.setEnabledMark(1);
            }
            moduleService.update(id, entity);
            //清除redis权限
            String cacheKey = cacheKeyUtil.getUserAuthorize() + UserProvider.getUser().getUserId();
            if (redisUtil.exists(cacheKey)) {
                redisUtil.remove(cacheKey);
            }
            return ActionResult.success(MsgCode.SU004.get());
        }
        return ActionResult.fail(MsgCode.FA002.get());
    }
 
    /**
     * 系统菜单导出功能
     *
     * @param id 接口id
     * @return ignore
     */
    @Operation(summary = "导出系统菜单数据")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission(value = {"permission.menu", "appConfig.appMenu"}, mode = SaMode.OR)
    @GetMapping("/{id}/Actions/Export")
    public ActionResult exportFile(@PathVariable("id") String id) {
        DownloadVO downloadVO = moduleService.exportData(id);
        return ActionResult.success(downloadVO);
    }
 
    /**
     * 系统菜单导入功能
     *
     * @param multipartFile 文件
     * @param parentId      父级id
     * @param category      分类
     * @return ignore
     */
    @Operation(summary = "系统菜单导入功能")
    @Parameters({
            @Parameter(name = "systemId", description = "系统id", required = true),
    })
    @SaCheckPermission(value = {"permission.menu", "appConfig.appMenu"}, mode = SaMode.OR)
    @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ActionResult importFile(@RequestPart("file") MultipartFile multipartFile,
                                   @RequestParam("parentId") String parentId,
                                   @RequestParam("category") String category,
                                   @RequestParam("type") Integer type) throws DataException {
        SystemEntity systemEntity = systemService.getInfoByEnCode(RequestContext.getAppCode());
        //判断是否为.bm结尾
        if (FileUtil.existsSuffix(multipartFile, ModuleTypeEnum.SYSTEM_MODULE.getTableName())) {
            return ActionResult.fail(MsgCode.IMP002.get());
        }
        try {
            //读取文件内容
            String fileContent = FileUtil.getFileContent(multipartFile);
            //转model后导入
            ModuleExportModel exportModel = JsonUtil.getJsonToBean(fileContent, ModuleExportModel.class);
            ModuleEntity moduleEntity = exportModel.getModuleEntity();
            if (!category.equals(moduleEntity.getCategory())) {
                return ActionResult.fail(MsgCode.SYS018.get(category.toUpperCase()));
            }
            if (JnpfConst.APP.equals(moduleEntity.getCategory()) && "-1".equals(parentId)) {
                return ActionResult.fail(MsgCode.SYS019.get());
            }
            // 设置系统id然后重新赋值
            if (!systemEntity.getId().equals(moduleEntity.getSystemId())) {
                moduleEntity.setId(RandomUtil.uuId());
                moduleEntity.setEnCode(null);
                moduleService.setAutoEnCode(moduleEntity);
                moduleEntity.setSystemId(systemEntity.getId());
            }
            moduleEntity.setParentId(parentId);
            //清空同步菜单记录 避免重复
            moduleEntity.setModuleId(null);
            moduleEntity.setCreatorTime(new Date());
//            String enCode = moduleEntity.getEnCode();
//            moduleEntity.setEnCode(moduleEntity.getEnCode() + "_" + RandomUtil.getRandomCode());
//            if (moduleEntity.getType() == 3) {
//                moduleEntity.setUrlAddress(moduleEntity.getUrlAddress().replace(enCode, moduleEntity.getEnCode()));
//            }
            exportModel.setModuleEntity(moduleEntity);
            return moduleService.importData(exportModel, type);
        } catch (Exception e) {
            throw new DataException(MsgCode.IMP004.get());
        }
    }
 
    // ------------------多租户调用
 
    /**
     * 通过租户id获取菜单
     *
     * @param tenantMenuModel 模型
     * @return ignore
     */
    @Operation(summary = "通过租户id获取菜单")
    @Parameters({
            @Parameter(name = "tenantMenuModel", description = "模型", required = true)
    })
    @NoDataSourceBind
    @PostMapping("/Tenant/Menu")
    public TenantMenuVO menu(@RequestBody TenantMenuModel tenantMenuModel) throws DataException {
        if (configValueUtil.isMultiTenancy()) {
            TenantDataSourceUtil.switchTenant(tenantMenuModel.getTenantId());
        }
        List<SystemEntity> systemEntityList = systemService.getList();
        List<String> ids = new ArrayList<>();
        if (Objects.nonNull(tenantMenuModel.getIds())) {
            ids = tenantMenuModel.getIds();
        }
        List<String> urlAddressList = new ArrayList<>();
        if (Objects.nonNull(tenantMenuModel.getIds())) {
            urlAddressList = tenantMenuModel.getUrlAddressList();
        }
        List<ModuleEntity> moduleEntityList = moduleService.getList(true, new ArrayList<>(), new ArrayList<>());
        TenantMenuVO module = module(systemEntityList, moduleEntityList, ids, urlAddressList);
        return module;
    }
 
    /**
     * 功能权限
     *
     * @param moduleEntityList 所有菜单
     * @param systemEntityList 所有应用
     * @return
     */
    private TenantMenuVO module(List<SystemEntity> systemEntityList, List<ModuleEntity> moduleEntityList, List<String> ids, List<String> urlAddressList) {
        TenantMenuVO vo = new TenantMenuVO();
        // 转树前所有数据
        List<TenantMenuTreeModel> moduleAllList = new ArrayList<>();
        List<TenantMenuTreeModel> systemList = JsonUtil.getJsonToList(systemEntityList, TenantMenuTreeModel.class);
        systemList.forEach(t -> t.setParentId("-1"));
        moduleAllList.addAll(systemList);
        Map<String, List<ModuleEntity>> moduleMap = moduleEntityList.stream().collect(Collectors.groupingBy(t -> {
            if (JnpfConst.WEB.equals(t.getCategory())) {
                return JnpfConst.WEB;
            } else {
                return JnpfConst.APP;
            }
        }));
        List<ModuleEntity> webModuleList = moduleMap.get(JnpfConst.WEB) == null ? new ArrayList<>() : moduleMap.get(JnpfConst.WEB);
        List<ModuleEntity> appModuleList = moduleMap.get(JnpfConst.APP) == null ? new ArrayList<>() : moduleMap.get(JnpfConst.APP);
        Map<String, ModuleEntity> appModuleMap = appModuleList.stream().collect(Collectors.toMap(ModuleEntity::getId, Function.identity()));
        Map<String, String> webIds = new HashMap<>(16);
        List<ModuleEntity> temWebList = webModuleList.stream().filter(t -> "-1".equals(t.getParentId())).collect(Collectors.toList());
        temWebList.stream().filter(t -> "-1".equals(t.getParentId())).forEach(t -> {
            if (!webIds.containsKey(t.getSystemId())) {
                ModuleEntity webData = new ModuleEntity();
                webData.setId(t.getSystemId() + "1");
                t.setParentId(webData.getId());
                webData.setFullName("WEB菜单");
                webData.setIcon(PermissionConst.PC_ICON);
                webData.setParentId(t.getSystemId());
                webData.setSystemId(t.getSystemId());
                webModuleList.add(webData);
                webIds.put(t.getSystemId(), webData.getId());
            } else {
                t.setParentId(webIds.get(t.getSystemId()) + "");
            }
        });
        List<TenantMenuTreeModel> webReturnModuleList = JsonUtil.getJsonToList(webModuleList, TenantMenuTreeModel.class);
        moduleAllList.addAll(webReturnModuleList);
        // 处理App菜单
        List<ModuleEntity> temList = appModuleList.stream().filter(t -> "-1".equals(t.getParentId()) && !JnpfConst.MAIN_SYSTEM_CODE.equals(t.getEnCode())).collect(Collectors.toList());
        Map<String, String> appIds = new HashMap<>(16);
        for (ModuleEntity appModuleEntity : temList) {
            if (StringUtil.isEmpty(appIds.get(appModuleEntity.getSystemId()))) {
                ModuleEntity appData = new ModuleEntity();
                appData.setId(appModuleEntity.getSystemId() + appModuleEntity.getSystemId() + "2");
                appModuleEntity.setParentId(appData.getId());
                appData.setFullName("APP菜单");
                appData.setIcon(PermissionConst.APP_ICON);
                appData.setParentId(appModuleEntity.getSystemId());
                appData.setSystemId(appModuleEntity.getSystemId());
                appModuleList.add(appData);
                appIds.put(appModuleEntity.getSystemId(), appData.getId());
            } else {
                appModuleList.remove(appModuleEntity);
                ModuleEntity entity = appModuleMap.get(appModuleEntity.getId());
                entity.setParentId(appIds.get(appModuleEntity.getSystemId()));
                appModuleList.add(entity);
            }
        }
        List<TenantMenuTreeModel> appReturnModuleList = JsonUtil.getJsonToList(appModuleList, TenantMenuTreeModel.class);
        moduleAllList.addAll(appReturnModuleList);
        List<SumTree<TenantMenuTreeModel>> sumTrees = TreeDotUtils.convertListToTreeDot(moduleAllList);
        List<TenantMenuTreeReturnModel> data = new ArrayList<>();
        TenantMenuTreeReturnModel workFlowEnabled = new TenantMenuTreeReturnModel();
        workFlowEnabled.setId("-999");
        workFlowEnabled.setFullName("协同办公");
        data.add(workFlowEnabled);
        data.addAll(JsonUtil.getJsonToList(sumTrees, TenantMenuTreeReturnModel.class));
        vo.setList(data);
        List<String> allId = moduleAllList.stream().map(TenantMenuTreeModel::getId).collect(Collectors.toList());
        allId.add(workFlowEnabled.getId());
        vo.setAll(allId);
        List<String> ids0 = moduleService.getListByUrlAddress(ids, urlAddressList).stream().map(ModuleEntity::getId).collect(Collectors.toList());
        List<String> selectorIds = allId.stream().filter(t -> !ids0.contains(t)).collect(Collectors.toList());
        if (ids.contains("-999")) {
            selectorIds.remove("-999");
        }
        vo.setIds(selectorIds);
        return vo;
    }
 
    /**
     * 通过租户id及菜单id获取菜单
     *
     * @param tenantMenuModel 模型
     * @return ignore
     */
    @Operation(summary = "通过租户id及菜单id获取菜单")
    @Parameters({
            @Parameter(name = "tenantMenuModel", description = "模型", required = true)
    })
    @NoDataSourceBind
    @PostMapping("/Tenant/MenuByIds")
    public Map MenuByIds(@RequestBody TenantMenuModel tenantMenuModel) throws DataException {
        if (configValueUtil.isMultiTenancy()) {
            TenantDataSourceUtil.switchTenant(tenantMenuModel.getTenantId());
        }
        List<ModuleEntity> list = moduleService.getListTenant();
        return list.stream().collect(Collectors.toMap(ModuleEntity::getId, ModuleEntity::getUrlAddress));
    }
 
    /**
     * 获取在线开发“表单”和“流程”类型的菜单数据
     */
    @Operation(summary = "菜单获取表单列表")
    @GetMapping("/Selector/Form")
    @Parameters({
            @Parameter(name = "systemId", description = "系统id", required = false),
    })
    public ActionResult getFormList(ModulePagination pagination) {
        List<ModuleSelectorVo> list = moduleService.getFormMenuList(pagination);
        list.stream().forEach(t -> {
            t.setTypeName(Objects.equals(t.getType(), 3) ? "表单" : "流程");
            PropertyJsonModel model = JsonUtil.getJsonToBean(t.getPropertyJson(), PropertyJsonModel.class);
            if (Objects.equals(t.getType(), 3)) {
                t.setFormId(model.getModuleId());
            } else {
                t.setFlowId(model.getModuleId());
                t.setFormId(templateApi.getFormByFlowId(model.getModuleId()));
            }
        });
        PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(list, paginationVO);
    }
 
 
    @Operation(summary = "根据type获取菜单列表")
    @GetMapping("/Selector/Page")
    public ActionResult getPageList(ModulePagination pagination) {
        SystemEntity info = systemService.getInfoByEnCode(RequestContext.getAppCode());
        pagination.setSystemId(info.getId());
        List<ModuleSelectorVo> list = moduleService.getPageList(pagination);
//        PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(list, null);
    }
 
    /**
     * 报表发布菜单
     *
     * @return ignore
     */
    @Operation(summary = "报表发布菜单")
    @Parameters({
            @Parameter(name = "menuModel", description = "模型", required = true)
    })
    @PostMapping("/saveReportMenu")
    public ActionResult saveReportMenu(@RequestBody VisualMenuModel menuModel) {
        try {
            pubulishUtil.publishMenu(menuModel);
            return ActionResult.success();
        } catch (WorkFlowException e) {
            return ActionResult.fail(e.getMessage());
        }
    }
 
    /**
     * 报表发布菜单
     *
     * @return ignore
     */
    @Operation(summary = "获取报表发布菜单")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @PostMapping("/getReportMenu")
    public ActionResult getReportMenu(@RequestBody VisualMenuModel menuModel) {
        ModuleNameVO moduleNameVO = moduleService.getModuleNameList(menuModel.getId());
        return ActionResult.success(moduleNameVO);
    }
 
    /**
     * 获取系统菜单列表(下拉树形)
     */
    @Operation(summary = "获取系统菜单列表(下拉树形)")
    @GetMapping("/getSystemMenu")
    public ActionResult<List<MenuSelectAllVO>> getSystemMenu() {
        List<MenuSelectAllVO> systemMenu = moduleService.getSystemMenu(3, Arrays.asList(2, 4), Arrays.asList("web"));
        return ActionResult.success(systemMenu);
    }
 
    @Operation(summary = "资源管理菜单")
    @Parameters({
            @Parameter(name = "systemId", description = "系统id", required = true)
    })
    @GetMapping("/ResourceManage")
    public ActionResult<ListVO<MenuListVO>> resourceManage(PaginationMenu paginationMenu) {
        String appCode = RequestContext.getAppCode();
        MenuListModel param = getParam(appCode, null, paginationMenu.getKeyword(), null, 1, null, false);
        List<ModuleEntity> data = moduleService.getList(param);
        // 递归查上级
        Map<String, ModuleEntity> moduleEntityMap = data.stream().collect(Collectors.toMap(ModuleEntity::getId, Function.identity()));
        if (StringUtil.isNotEmpty(paginationMenu.getKeyword())) {
            moduleService.getParentModule(data, moduleEntityMap);
        }
        List<UserMenuModel> list = JsonUtil.getJsonToList(moduleEntityMap.values(), UserMenuModel.class);
        list = list.stream().sorted(Comparator.comparing(UserMenuModel::getSortCode, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(UserMenuModel::getCreatorTime, Comparator.nullsLast(Comparator.reverseOrder()))).collect(Collectors.toList());
        List<SumTree<UserMenuModel>> menuList = TreeDotUtils.convertListToTreeDot(list);
        List<MenuListVO> menuvo = JsonUtil.getJsonToList(menuList, MenuListVO.class);
        ListVO vo = new ListVO();
        vo.setList(menuvo);
        return ActionResult.success(vo);
    }
 
 
    public MenuListModel getParam(String appCode, String category, String keyword, Integer type, Integer enabledMark, String parentId, boolean release){
        MenuListModel model = new MenuListModel();
        model.setAppCode(appCode);
        model.setCategory(category);
        model.setKeyword(keyword);
        model.setType(type);
        model.setEnabledMark(enabledMark);
        model.setParentId(parentId);
        model.setRelease(release);
        AuthorizeVO authorize = authorizeApi.getAuthorize(!Objects.equals(enabledMark, 1), appCode, null);
        model.setModuleList(authorize.getModuleList());
        return model;
    }
 
    /**
     * 列表
     *
     * @param model
     * @return
     */
    @Override
    @PostMapping("/getList")
    public List<ModuleEntity> getList(@RequestBody ModuleApiModel model) {
        List<ModuleEntity> list = moduleService.getList(model.getFilterFlowWork(), model.getModuleAuthorize(), model.getModuleUrlAddressAuthorize());
        return list;
    }
 
    /**
     * 全部菜单
     *
     * @return
     */
    @Override
    @GetMapping("/getListAll")
    public List<ModuleEntity> getList() {
        return moduleService.getList();
    }
 
    @Override
    @GetMapping("/getListByParentId/{id}")
    public List<ModuleEntity> getListByParentId(@PathVariable("id") String id) {
        List<ModuleEntity> list = moduleService.getListByParentId(id);
        return list;
    }
 
    @Override
    @GetMapping("/getListByModuleId/{ModuleId}")
    public List<ModuleEntity> getModuleList(@PathVariable("ModuleId") String ModuleId) {
        return moduleService.getModuleList(ModuleId);
    }
 
    @Override
    @GetMapping("/getInfoById")
    public ModuleEntity getModuleByList(@RequestParam("ModuleId") String moduleId) {
        return moduleService.getInfo(moduleId);
    }
 
    @Override
    @PostMapping("/pubulishToSys")
    @GlobalTransactional
    public ActionResult pubulish(@RequestBody(required = false) VisualMenuModel visualMenuModel) {
        try {
            pubulishUtil.publishMenu(visualMenuModel);
        } catch (WorkFlowException e) {
            return ActionResult.fail(e.getMessage());
        }
        return ActionResult.success();
    }
 
    @Override
    @PostMapping("/getModuleByIds")
    public List<ModuleEntity> getModuleByIds(@RequestBody ModuleApiByIdsModel model) {
        return moduleService.getModuleByIds(model.getIds());
    }
 
    @Override
    @PostMapping("/getModuleBySystemIds")
    public List<ModuleEntity> getModuleBySystemIds(@RequestBody ModuleApiByIdsModel model) {
        return moduleService.getModuleBySystemIds(model.getIds(), model.getModuleAuthorize(), model.getModuleUrlAddressAuthorize(), model.getStand());
    }
 
    @Override
    @PostMapping("/getListByEnCode")
    public List<ModuleEntity> getListByEnCode(@RequestBody List<String> enCodeList) {
        return moduleService.getListByEnCode(enCodeList);
    }
 
    @Override
    @PostMapping("/findModuleAdmin")
    public List<ModuleEntity> findModuleAdmin(@RequestBody ModuleApiByIdAndMarkModel model) {
        return moduleService.findModuleAdmin(model.getMark(), model.getId(), model.getModuleAuthorize(), model.getModuleUrlAddressAuthorize());
    }
 
    /**
     * 功能发布app pc菜单名称列表
     *
     * @return
     */
    @GetMapping("/getModuleNameList")
    @Override
    public ModuleNameVO getModuleNameList(@RequestParam("visualId") String visualId) {
        return moduleService.getModuleNameList(visualId);
    }
}