2021与蓝度共同重构项目,服务端
liuhaonan
2022-02-28 ee1effceeac41cae441f98208d3a538de340c390
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sandu.common.enums.RoleLevelStatus;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.execption.EntityExistException;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.param.MenuParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.bo.MenuNode;
import com.sandu.ximon.dao.domain.AdminRoleRelation;
import com.sandu.ximon.dao.domain.Menu;
import com.sandu.ximon.dao.domain.Role;
import com.sandu.ximon.dao.domain.RoleMenuRelation;
import com.sandu.ximon.dao.mapper.AdminRoleRelationMapper;
import com.sandu.ximon.dao.mapper.MenuMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @author chenjiantian
 * @date 2021/11/24 11:15
 */
@Service
@AllArgsConstructor
public class MenuService extends BaseServiceImpl<MenuMapper, Menu> {
 
    private final RoleMenuRelationService roleMenuRelationService;
    /**
     * 获取角色下的菜单
     */
    public List<Menu> listByRoleIds(List<Long> roleIdList) {
        List<Long> menuIdList = roleMenuRelationService.list(Wrappers.lambdaQuery(RoleMenuRelation.class).in(RoleMenuRelation::getRoleId, roleIdList).select(RoleMenuRelation::getMenuId))
                .stream().map(RoleMenuRelation::getMenuId).distinct().collect(Collectors.toList());
        if (CollectionUtil.isEmpty(menuIdList)) {
            return null;
        }
        return listByIds(menuIdList);
    }
 
    public boolean addMenu(MenuParam param) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(param,menu);
        return save(menu);
    }
 
//    public Set<Menu> listMenuByRoles(Set<Role> roles) {
//        List<Long> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
//        Set<Menu> menus = menuDao.listMenuByRoleIds(roleIds);
//        return menus;
//    }
//
//    /**
//     * 把菜单转成树形结构
//     */
//    public List<MenuNode> buildTree(List<Menu> menuList) {
//        return menuList.stream()
//                .filter(menu -> menu.getPid().equals(0L))
//                .map(menu -> covertMenuNode(menu, menuList)).collect(Collectors.toList());
//    }
 
    private MenuNode covertMenuNode(Menu menu, List<Menu> menuList) {
        MenuNode node = new MenuNode();
        BeanUtils.copyProperties(menu, node);
        List<MenuNode> children = menuList.stream()
                .filter(subMenu -> subMenu.getPid().equals(menu.getId()))
                .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
 
    public boolean updateMenu(Long menuId,MenuParam param) {
        Menu one = getById(menuId);
        if (one == null) {
            throw new BusinessException("找不到菜单信息");
        }
 
        List<Role> roles = SpringContextHolder.getBean(RoleService.class).listByAdminId(SecurityUtils.getUserId());
        Integer roleLevel = roles.stream().map(Role::getLevel).min(Integer::compareTo).orElse(RoleLevelStatus.COMMON.getCode());
        if(!RoleLevelStatus.SUPER.getCode().equals(roleLevel)){
            throw new BusinessException("超级管理员才可以编辑菜单");
        }
 
        Menu menu = new Menu();
        BeanUtils.copyProperties(param,menu);
        menu.setId(menuId);
        return updateById(menu);
    }
 
    public List<MenuNode> treeList() {
        List<Menu> list = list();
        return list.stream()
                .filter(menu -> menu.getPid().equals(0L))
                .map(menu -> covertMenuNode(menu, list)).collect(Collectors.toList());
    }
 
    public boolean delMenu(Long menuId) {
        Menu one = getById(menuId);
        if (one == null) {
            throw new BusinessException("找不到菜单信息");
        }
 
        List<Role> roles = SpringContextHolder.getBean(RoleService.class).listByAdminId(SecurityUtils.getUserId());
        Integer roleLevel = roles.stream().map(Role::getLevel).min(Integer::compareTo).orElse(RoleLevelStatus.COMMON.getCode());
        if(!RoleLevelStatus.SUPER.getCode().equals(roleLevel)){
            throw new BusinessException("超级管理员才可以删除菜单");
        }
        return removeById(menuId);
    }
}