houzhongjian
2024-10-16 7da8f196dee8e3c526c009a4bc7f5983ece6bb97
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
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<List<AppMenuRespDTO>> getAppMenuList(Long id) {
        List<AppMenuDO> children = new LinkedList<>();
        // 遍历每一层
        Collection<Long> parentIds = Collections.singleton(id);
        for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环
            // 查询当前层,所有的子应用菜单
            List<AppMenuDO> 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<List<AppRespDTO>> getAppList() {
        List<AppDO> list = appService.getList();
        list.sort(Comparator.comparing(AppDO::getOrderNum));
        return success(BeanUtils.toBean(list, AppRespDTO.class));
    }
}