package com.iailab.module.system.api.app; import cn.hutool.core.collection.CollUtil; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.module.system.api.app.dto.AppMenuRespDTO; import com.iailab.module.system.api.app.dto.AppRespDTO; import com.iailab.module.system.controller.admin.app.vo.AppRespVO; import com.iailab.module.system.convert.app.AppConvert; import com.iailab.module.system.dal.dataobject.app.AppDO; import com.iailab.module.system.dal.dataobject.app.AppMenuDO; import com.iailab.module.system.service.app.AppMenuService; import com.iailab.module.system.service.app.AppService; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.*; import static com.iailab.framework.common.pojo.CommonResult.success; import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet; @RestController // 提供 RESTful API 接口,给 Feign 调用 @Validated public class AppMenuApiImpl implements AppMenuApi { @Resource private AppMenuService appMenuService; @Resource private AppService appService; @Override public CommonResult> getAppMenuList(Long id) { List children = new LinkedList<>(); // 遍历每一层 Collection parentIds = Collections.singleton(id); for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 // 查询当前层,所有的子应用菜单 List menus = appMenuService.selectListByParentId(parentIds); // 1. 如果没有子菜单,则结束遍历 if (CollUtil.isEmpty(menus)) { break; } // 2. 如果有子应用菜单,继续遍历 children.addAll(menus); parentIds = convertSet(menus, AppMenuDO::getId); } // children = appMenuService.filterDisableMenus(children); return success(AppConvert.INSTANCE.buildMenuTree(id, children)); } @Override public CommonResult> getAppList() { List list = appService.getList(); list.sort(Comparator.comparing(AppDO::getOrderNum)); return success(BeanUtils.toBean(list, AppRespDTO.class)); } }