Commit e86c4edad4094db00bc7375fe27a74444c702ec6

Authored by 戈灵虎
1 parent cc15ba00

app模块

urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/controller/app/appmodule/AppAppModuleController.java
... ... @@ -15,8 +15,12 @@ import org.springframework.web.bind.annotation.GetMapping;
15 15 import org.springframework.web.bind.annotation.RequestMapping;
16 16 import org.springframework.web.bind.annotation.RestController;
17 17  
  18 +import java.util.ArrayList;
  19 +import java.util.Collections;
18 20 import java.util.Comparator;
  21 +import java.util.LinkedHashMap;
19 22 import java.util.List;
  23 +import java.util.Map;
20 24  
21 25 import static com.zteits.urbanops.framework.common.pojo.CommonResult.success;
22 26  
... ... @@ -47,13 +51,56 @@ public class AppAppModuleController {
47 51 // 填充统计数据(根据extra中的statisticKey配置)
48 52 // appModuleService.fillModuleStatistics(list, userId);
49 53  
50   - // 排序并转换
51   - list.sort(Comparator.comparing(AppModuleDO::getSort));
52   - List<AppModuleVO> result = AppModuleConvert.INSTANCE.convertAppList(list);
  54 + // 转换为 VO 列表并构建树
  55 + List<AppModuleVO> modules = AppModuleConvert.INSTANCE.convertAppList(list);
  56 + List<AppModuleVO> moduleTree = buildModuleTree(modules);
53 57  
54 58 // MapStruct 转换已通过 @AfterMapping 自动填充 statisticCount
55 59  
56   - return success(result);
  60 + return success(moduleTree);
  61 + }
  62 +
  63 + private List<AppModuleVO> buildModuleTree(List<AppModuleVO> modules) {
  64 + if (modules == null || modules.isEmpty()) {
  65 + return Collections.emptyList();
  66 + }
  67 +
  68 + Map<Long, AppModuleVO> moduleMap = new LinkedHashMap<>(modules.size());
  69 + for (AppModuleVO module : modules) {
  70 + module.setChildren(new ArrayList<>());
  71 + moduleMap.put(module.getId(), module);
  72 + }
  73 +
  74 + List<AppModuleVO> roots = new ArrayList<>();
  75 + for (AppModuleVO module : modules) {
  76 + Long parentId = module.getParentId();
  77 + if (parentId == null || AppModuleDO.ID_ROOT.equals(parentId)) {
  78 + roots.add(module);
  79 + continue;
  80 + }
  81 +
  82 + AppModuleVO parent = moduleMap.get(parentId);
  83 + if (parent == null) {
  84 + roots.add(module);
  85 + continue;
  86 + }
  87 + parent.getChildren().add(module);
  88 + }
  89 +
  90 + sortModules(roots);
  91 + return roots;
  92 + }
  93 +
  94 + private void sortModules(List<AppModuleVO> modules) {
  95 + if (modules == null || modules.isEmpty()) {
  96 + return;
  97 + }
  98 +
  99 + modules.sort(Comparator.comparing(AppModuleVO::getSort,
  100 + Comparator.nullsLast(Integer::compareTo)));
  101 + for (AppModuleVO module : modules) {
  102 + sortModules(module.getChildren());
  103 + }
57 104 }
58 105  
59 106 }
... ...
urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/controller/app/appmodule/vo/AppModuleVO.java
... ... @@ -3,6 +3,7 @@ package com.zteits.urbanops.module.system.controller.app.appmodule.vo;
3 3 import io.swagger.v3.oas.annotations.media.Schema;
4 4 import lombok.Data;
5 5  
  6 +import java.util.List;
6 7 import java.util.Map;
7 8  
8 9 @Schema(description = "用户 APP - App 功能模块 Response VO")
... ... @@ -51,4 +52,7 @@ public class AppModuleVO {
51 52 @Schema(description = "扩展字段 JSON", example = "{}")
52 53 private Map<String, Object> extra;
53 54  
  55 + @Schema(description = "子模块集合")
  56 + private List<AppModuleVO> children;
  57 +
54 58 }
... ...
urbanops-module-system/src/main/java/com/zteits/urbanops/module/system/service/appmodule/AppModuleServiceImpl.java
... ... @@ -143,6 +143,7 @@ public class AppModuleServiceImpl implements AppModuleService {
143 143 // 查询所有启用的模块
144 144 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
145 145 List<AppModuleDO> modules = getAppModuleList(reqVO);
  146 + modules = includeParentModules(modules);
146 147  
147 148 // 过滤掉关闭的节点
148 149 modules = filterDisableModules(modules);
... ... @@ -159,6 +160,46 @@ public class AppModuleServiceImpl implements AppModuleService {
159 160 return modules;
160 161 }
161 162  
  163 + private List<AppModuleDO> includeParentModules(List<AppModuleDO> modules) {
  164 + if (CollUtil.isEmpty(modules)) {
  165 + return modules;
  166 + }
  167 +
  168 + Map<Long, AppModuleDO> moduleMap = new LinkedHashMap<>();
  169 + for (AppModuleDO module : modules) {
  170 + moduleMap.put(module.getId(), module);
  171 + }
  172 +
  173 + Set<Long> pendingParentIds = new HashSet<>();
  174 + for (AppModuleDO module : modules) {
  175 + Long parentId = module.getParentId();
  176 + if (parentId == null || ObjUtil.equal(parentId, ID_ROOT) || moduleMap.containsKey(parentId)) {
  177 + continue;
  178 + }
  179 + pendingParentIds.add(parentId);
  180 + }
  181 +
  182 + while (CollUtil.isNotEmpty(pendingParentIds)) {
  183 + List<AppModuleDO> parents = appModuleMapper.selectByIds(pendingParentIds);
  184 + pendingParentIds = new HashSet<>();
  185 + for (AppModuleDO parent : parents) {
  186 + if (parent == null) {
  187 + continue;
  188 + }
  189 + if (!moduleMap.containsKey(parent.getId())) {
  190 + moduleMap.put(parent.getId(), parent);
  191 + }
  192 + Long ancestorId = parent.getParentId();
  193 + if (ancestorId != null && !ObjUtil.equal(ancestorId, ID_ROOT)
  194 + && !moduleMap.containsKey(ancestorId)) {
  195 + pendingParentIds.add(ancestorId);
  196 + }
  197 + }
  198 + }
  199 +
  200 + return new ArrayList<>(moduleMap.values());
  201 + }
  202 +
162 203 @Override
163 204 public List<AppModuleDO> filterDisableModules(List<AppModuleDO> moduleList) {
164 205 if (CollUtil.isEmpty(moduleList)) {
... ...