南昌市物联网技防平台-学校版后台
zengh
2021-06-03 723946c6b272e5aa4c7ec110ff1395f6c2b23e89
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.system.dto.MenuDTO;
import org.springblade.system.entity.Menu;
import org.springblade.system.entity.RoleMenu;
import org.springblade.system.entity.RoleScope;
import org.springblade.system.entity.TopMenuSetting;
import org.springblade.system.mapper.MenuMapper;
import org.springblade.system.service.IMenuService;
import org.springblade.system.service.IRoleMenuService;
import org.springblade.system.service.IRoleScopeService;
import org.springblade.system.service.ITopMenuSettingService;
import org.springblade.system.vo.MenuVO;
import org.springblade.system.wrapper.MenuWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
import static org.springblade.common.constant.CommonConstant.API_SCOPE_CATEGORY;
import static org.springblade.common.constant.CommonConstant.DATA_SCOPE_CATEGORY;
import static org.springblade.core.cache.constant.CacheConstant.MENU_CACHE;
 
/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
@AllArgsConstructor
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
 
    private final IRoleMenuService roleMenuService;
    private final IRoleScopeService roleScopeService;
    private final ITopMenuSettingService topMenuSettingService;
    private final static String PARENT_ID = "parentId";
    private final static Integer MENU_CATEGORY = 1;
 
    @Override
    public List<MenuVO> lazyList(Long parentId, Map<String, Object> param) {
        if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) {
            parentId = null;
        }
        return baseMapper.lazyList(parentId, param);
    }
 
    @Override
    public List<MenuVO> lazyMenuList(Long parentId, Map<String, Object> param) {
        if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) {
            parentId = null;
        }
        return baseMapper.lazyMenuList(parentId, param);
    }
 
 
    @Override
    public List<MenuVO> routes(String roleId, Long topMenuId) {
        if (StringUtil.isBlank(roleId)) {
            return null;
        }
        List<Menu> allMenus = baseMapper.allMenu();
        List<Menu> roleMenus = (AuthUtil.isAdministrator() && Func.isEmpty(topMenuId)) ? allMenus : baseMapper.roleMenu(Func.toLongList(roleId), topMenuId);
        return buildRoutes(allMenus, roleMenus);
    }
 
    private List<MenuVO> buildRoutes(List<Menu> allMenus, List<Menu> roleMenus) {
        List<Menu> routes = new LinkedList<>(roleMenus);
        roleMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu));
        routes.sort(Comparator.comparing(Menu::getSort));
        MenuWrapper menuWrapper = new MenuWrapper();
        List<Menu> collect = routes.stream().filter(x -> Func.equals(x.getCategory(), 1)).collect(Collectors.toList());
        return menuWrapper.listNodeVO(collect);
    }
 
    private void recursion(List<Menu> allMenus, List<Menu> routes, Menu roleMenu) {
        Optional<Menu> menu = allMenus.stream().filter(x -> Func.equals(x.getId(), roleMenu.getParentId())).findFirst();
        if (menu.isPresent() && !routes.contains(menu.get())) {
            routes.add(menu.get());
            recursion(allMenus, routes, menu.get());
        }
    }
 
    @Override
    public List<MenuVO> buttons(String roleId) {
        List<Menu> buttons = (AuthUtil.isAdministrator()) ? baseMapper.allButtons() : baseMapper.buttons(Func.toLongList(roleId));
        MenuWrapper menuWrapper = new MenuWrapper();
        return menuWrapper.listNodeVO(buttons);
    }
 
    @Override
    public List<MenuVO> tree() {
        return ForestNodeMerger.merge(baseMapper.tree());
    }
 
    @Override
    public List<MenuVO> grantTree(BladeUser user) {
        return ForestNodeMerger.merge(user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantTree() : baseMapper.grantTreeByRole(Func.toLongList(user.getRoleId())));
    }
 
    @Override
    public List<MenuVO> grantTopTree(BladeUser user) {
        return ForestNodeMerger.merge(user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantTopTree() : baseMapper.grantTopTreeByRole(Func.toLongList(user.getRoleId())));
    }
 
    @Override
    public List<MenuVO> grantDataScopeTree(BladeUser user) {
        return ForestNodeMerger.merge(user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantDataScopeTree() : baseMapper.grantDataScopeTreeByRole(Func.toLongList(user.getRoleId())));
    }
 
    @Override
    public List<MenuVO> grantApiScopeTree(BladeUser user) {
        return ForestNodeMerger.merge(user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantApiScopeTree() : baseMapper.grantApiScopeTreeByRole(Func.toLongList(user.getRoleId())));
    }
 
    @Override
    public List<String> roleTreeKeys(String roleIds) {
        List<RoleMenu> roleMenus = roleMenuService.list(Wrappers.<RoleMenu>query().lambda().in(RoleMenu::getRoleId, Func.toLongList(roleIds)));
        return roleMenus.stream().map(roleMenu -> Func.toStr(roleMenu.getMenuId())).collect(Collectors.toList());
    }
 
    @Override
    public List<String> topTreeKeys(String topMenuIds) {
        List<TopMenuSetting> settings = topMenuSettingService.list(Wrappers.<TopMenuSetting>query().lambda().in(TopMenuSetting::getTopMenuId, Func.toLongList(topMenuIds)));
        return settings.stream().map(setting -> Func.toStr(setting.getMenuId())).collect(Collectors.toList());
    }
 
    @Override
    public List<String> dataScopeTreeKeys(String roleIds) {
        List<RoleScope> roleScopes = roleScopeService.list(Wrappers.<RoleScope>query().lambda().eq(RoleScope::getScopeCategory, DATA_SCOPE_CATEGORY).in(RoleScope::getRoleId, Func.toLongList(roleIds)));
        return roleScopes.stream().map(roleScope -> Func.toStr(roleScope.getScopeId())).collect(Collectors.toList());
    }
 
    @Override
    public List<String> apiScopeTreeKeys(String roleIds) {
        List<RoleScope> roleScopes = roleScopeService.list(Wrappers.<RoleScope>query().lambda().eq(RoleScope::getScopeCategory, API_SCOPE_CATEGORY).in(RoleScope::getRoleId, Func.toLongList(roleIds)));
        return roleScopes.stream().map(roleScope -> Func.toStr(roleScope.getScopeId())).collect(Collectors.toList());
    }
 
    @Override
    @Cacheable(cacheNames = MENU_CACHE, key = "'auth:routes:' + #user.roleId")
    public List<Kv> authRoutes(BladeUser user) {
        List<MenuDTO> routes = baseMapper.authRoutes(Func.toLongList(user.getRoleId()));
        List<Kv> list = new ArrayList<>();
        routes.forEach(route -> list.add(Kv.create().set(route.getPath(), Kv.create().set("authority", Func.toStrArray(route.getAlias())))));
        return list;
    }
 
    @Override
    public boolean removeMenu(String ids) {
        Integer cnt = baseMapper.selectCount(Wrappers.<Menu>query().lambda().in(Menu::getParentId, Func.toLongList(ids)));
        if (cnt > 0) {
            throw new ServiceException("请先删除子节点!");
        }
        return removeByIds(Func.toLongList(ids));
    }
 
    @Override
    public boolean submit(Menu menu) {
        LambdaQueryWrapper<Menu> menuQueryWrapper = Wrappers.lambdaQuery();
        if (menu.getId() == null) {
            menuQueryWrapper.eq(Menu::getCode, menu.getCode()).or(
                wrapper -> wrapper.eq(Menu::getName, menu.getName()).eq(Menu::getCategory, MENU_CATEGORY)
            );
        } else {
            menuQueryWrapper.ne(Menu::getId, menu.getId()).and(
                wrapper -> wrapper.eq(Menu::getCode, menu.getCode()).or(
                    o -> o.eq(Menu::getName, menu.getName()).eq(Menu::getCategory, MENU_CATEGORY)
                )
            );
        }
        Integer cnt = baseMapper.selectCount(menuQueryWrapper);
        if (cnt > 0) {
            throw new ServiceException("菜单名或编号已存在!");
        }
        if (menu.getParentId() == null && menu.getId() == null) {
            menu.setParentId(BladeConstant.TOP_PARENT_ID);
        }
        menu.setIsDeleted(BladeConstant.DB_NOT_DELETED);
        return saveOrUpdate(menu);
    }
 
}