提交 | 用户 | 时间
|
818a01
|
1 |
package com.iailab.module.system.service.app; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.ObjUtil; |
|
5 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
6 |
import com.google.common.annotations.VisibleForTesting; |
|
7 |
import com.google.common.collect.Lists; |
|
8 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
9 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
10 |
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
|
11 |
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuSaveVO; |
|
12 |
import com.iailab.module.system.dal.dataobject.app.AppMenuDO; |
|
13 |
import com.iailab.module.system.dal.mysql.app.AppMenuMapper; |
|
14 |
import com.iailab.module.system.dal.redis.RedisKeyConstants; |
|
15 |
import com.iailab.module.system.enums.permission.MenuTypeEnum; |
|
16 |
import com.iailab.module.system.service.tenant.TenantService; |
|
17 |
import lombok.extern.slf4j.Slf4j; |
|
18 |
import org.springframework.cache.annotation.CacheEvict; |
|
19 |
import org.springframework.cache.annotation.Cacheable; |
|
20 |
import org.springframework.context.annotation.Lazy; |
|
21 |
import org.springframework.stereotype.Service; |
|
22 |
import org.springframework.transaction.annotation.Transactional; |
|
23 |
|
|
24 |
import javax.annotation.Resource; |
|
25 |
import java.util.*; |
|
26 |
|
|
27 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
28 |
import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; |
|
29 |
import static com.iailab.framework.common.util.collection.CollectionUtils.convertMap; |
|
30 |
import static com.iailab.module.system.dal.dataobject.app.AppMenuDO.ID_ROOT; |
|
31 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
32 |
|
|
33 |
|
|
34 |
/** |
|
35 |
* 菜单 Service 实现 |
|
36 |
* |
|
37 |
* @author iailab |
|
38 |
*/ |
|
39 |
@Service |
|
40 |
@Slf4j |
|
41 |
public class AppMenuServiceImpl implements AppMenuService { |
|
42 |
|
|
43 |
@Resource |
|
44 |
private AppMenuMapper appMenuMapper; |
|
45 |
@Resource |
|
46 |
@Lazy // 延迟,避免循环依赖报错 |
|
47 |
private TenantService tenantService; |
|
48 |
|
|
49 |
@Override |
|
50 |
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#createReqVO.permission", |
|
51 |
condition = "#createReqVO.permission != null") |
|
52 |
public Long createMenu(MenuSaveVO createReqVO) { |
|
53 |
// 校验父菜单存在 |
|
54 |
validateParentMenu(createReqVO.getParentId(), null); |
|
55 |
// 校验菜单(自己) |
|
56 |
validateMenu(createReqVO.getParentId(), createReqVO.getName(), null); |
|
57 |
|
|
58 |
// 插入数据库 |
|
59 |
AppMenuDO menu = BeanUtils.toBean(createReqVO, AppMenuDO.class); |
|
60 |
initMenuProperty(menu); |
|
61 |
appMenuMapper.insert(menu); |
|
62 |
// 返回 |
|
63 |
return menu.getId(); |
|
64 |
} |
|
65 |
|
|
66 |
@Override |
|
67 |
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, |
|
68 |
allEntries = true) // allEntries 清空所有缓存,因为 permission 如果变更,涉及到新老两个 permission。直接清理,简单有效 |
|
69 |
public void updateMenu(MenuSaveVO updateReqVO) { |
|
70 |
// 校验更新的菜单是否存在 |
|
71 |
if (appMenuMapper.selectById(updateReqVO.getId()) == null) { |
|
72 |
throw exception(MENU_NOT_EXISTS); |
|
73 |
} |
|
74 |
// 校验父菜单存在 |
|
75 |
validateParentMenu(updateReqVO.getParentId(), updateReqVO.getId()); |
|
76 |
// 校验菜单(自己) |
|
77 |
validateMenu(updateReqVO.getParentId(), updateReqVO.getName(), updateReqVO.getId()); |
|
78 |
|
|
79 |
// 更新到数据库 |
|
80 |
AppMenuDO updateObj = BeanUtils.toBean(updateReqVO, AppMenuDO.class); |
|
81 |
initMenuProperty(updateObj); |
|
82 |
appMenuMapper.updateById(updateObj); |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
@Transactional(rollbackFor = Exception.class) |
|
87 |
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, |
|
88 |
allEntries = true) // allEntries 清空所有缓存,因为此时不知道 id 对应的 permission 是多少。直接清理,简单有效 |
|
89 |
public void deleteMenu(Long id) { |
|
90 |
// 校验是否还有子菜单 |
|
91 |
if (appMenuMapper.selectCountByParentId(id) > 0) { |
|
92 |
throw exception(MENU_EXISTS_CHILDREN); |
|
93 |
} |
|
94 |
// 校验删除的菜单是否存在 |
|
95 |
if (appMenuMapper.selectById(id) == null) { |
|
96 |
throw exception(MENU_NOT_EXISTS); |
|
97 |
} |
|
98 |
// 标记删除 |
|
99 |
appMenuMapper.deleteById(id); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public List<AppMenuDO> getMenuList() { |
|
104 |
return appMenuMapper.selectList(); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public List<AppMenuDO> getMenuList(Integer type) { |
|
109 |
LambdaQueryWrapper<AppMenuDO> queryWrapper = new LambdaQueryWrapper<>(); |
|
110 |
if (type == 1) { |
|
111 |
queryWrapper.eq(AppMenuDO::getType, type); |
|
112 |
} |
|
113 |
return appMenuMapper.selectList(queryWrapper); |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public List<AppMenuDO> getMenuListByTenant(Integer type) { |
|
118 |
// 查询所有菜单,并过滤掉关闭的节点 |
|
119 |
List<AppMenuDO> menus = getMenuList(type); |
|
120 |
// 开启多租户的情况下,需要过滤掉未开通的菜单 |
|
121 |
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
|
122 |
return menus; |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public List<AppMenuDO> selectListByParentId(Collection<Long> parentIds) { |
|
127 |
return appMenuMapper.selectListByParentId(parentIds); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public List<AppMenuDO> getMenuListByTenant(MenuListReqVO reqVO) { |
|
132 |
// 查询所有菜单,并过滤掉关闭的节点 |
|
133 |
List<AppMenuDO> menus = getMenuList(reqVO); |
|
134 |
// 开启多租户的情况下,需要过滤掉未开通的菜单 |
|
135 |
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
|
136 |
return menus; |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public List<AppMenuDO> filterDisableMenus(List<AppMenuDO> menuList) { |
|
141 |
if (CollUtil.isEmpty(menuList)){ |
|
142 |
return Collections.emptyList(); |
|
143 |
} |
|
144 |
Map<Long, AppMenuDO> menuMap = convertMap(menuList, AppMenuDO::getId); |
|
145 |
|
|
146 |
// 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果 |
|
147 |
List<AppMenuDO> enabledMenus = new ArrayList<>(); |
|
148 |
Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索 |
|
149 |
for (AppMenuDO menu : menuList) { |
|
150 |
if (isMenuDisabled(menu, menuMap, disabledMenuCache)) { |
|
151 |
continue; |
|
152 |
} |
|
153 |
enabledMenus.add(menu); |
|
154 |
} |
|
155 |
return enabledMenus; |
|
156 |
} |
|
157 |
|
|
158 |
private boolean isMenuDisabled(AppMenuDO node, Map<Long, AppMenuDO> menuMap, Set<Long> disabledMenuCache) { |
|
159 |
// 如果已经判定是禁用的节点,直接结束 |
|
160 |
if (disabledMenuCache.contains(node.getId())) { |
|
161 |
return true; |
|
162 |
} |
|
163 |
|
|
164 |
// 1. 遍历到 parentId 为根节点,则无需判断 |
|
165 |
Long parentId = node.getParentId(); |
|
166 |
if (ObjUtil.equal(parentId, ID_ROOT)) { |
|
167 |
if (CommonStatusEnum.isDisable(node.getStatus())) { |
|
168 |
disabledMenuCache.add(node.getId()); |
|
169 |
return true; |
|
170 |
} |
|
171 |
return false; |
|
172 |
} |
|
173 |
|
|
174 |
// 2. 继续遍历 parent 节点 |
|
175 |
AppMenuDO parent = menuMap.get(parentId); |
|
176 |
if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) { |
|
177 |
disabledMenuCache.add(node.getId()); |
|
178 |
return true; |
|
179 |
} |
|
180 |
return false; |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public List<AppMenuDO> getMenuList(MenuListReqVO reqVO) { |
|
185 |
return appMenuMapper.selectList(reqVO); |
|
186 |
} |
|
187 |
|
|
188 |
@Override |
|
189 |
@Cacheable(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#permission") |
|
190 |
public List<Long> getMenuIdListByPermissionFromCache(String permission) { |
|
191 |
List<AppMenuDO> menus = appMenuMapper.selectListByPermission(permission); |
|
192 |
return convertList(menus, AppMenuDO::getId); |
|
193 |
} |
|
194 |
|
|
195 |
@Override |
|
196 |
public AppMenuDO getMenu(Long id) { |
|
197 |
return appMenuMapper.selectById(id); |
|
198 |
} |
|
199 |
|
|
200 |
@Override |
|
201 |
public List<AppMenuDO> getMenuList(Collection<Long> ids) { |
|
202 |
// 当 ids 为空时,返回一个空的实例对象 |
|
203 |
if (CollUtil.isEmpty(ids)) { |
|
204 |
return Lists.newArrayList(); |
|
205 |
} |
|
206 |
return appMenuMapper.selectBatchIds(ids); |
|
207 |
} |
|
208 |
|
|
209 |
/** |
|
210 |
* 校验父菜单是否合法 |
|
211 |
* <p> |
|
212 |
* 1. 不能设置自己为父菜单 |
|
213 |
* 2. 父菜单不存在 |
|
214 |
* 3. 父菜单必须是 {@link MenuTypeEnum#MENU} 菜单类型 |
|
215 |
* |
|
216 |
* @param parentId 父菜单编号 |
|
217 |
* @param childId 当前菜单编号 |
|
218 |
*/ |
|
219 |
@VisibleForTesting |
|
220 |
void validateParentMenu(Long parentId, Long childId) { |
|
221 |
if (parentId == null || ID_ROOT.equals(parentId)) { |
|
222 |
return; |
|
223 |
} |
|
224 |
// 不能设置自己为父菜单 |
|
225 |
if (parentId.equals(childId)) { |
|
226 |
throw exception(MENU_PARENT_ERROR); |
|
227 |
} |
|
228 |
AppMenuDO menu = appMenuMapper.selectById(parentId); |
|
229 |
// 父菜单不存在 |
|
230 |
if (menu == null) { |
|
231 |
throw exception(MENU_PARENT_NOT_EXISTS); |
|
232 |
} |
|
233 |
// 父菜单必须是目录或者菜单类型 |
|
234 |
if (!MenuTypeEnum.DIR.getType().equals(menu.getType()) |
|
235 |
&& !MenuTypeEnum.MENU.getType().equals(menu.getType())) { |
|
236 |
throw exception(MENU_PARENT_NOT_DIR_OR_MENU); |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* 校验菜单是否合法 |
|
242 |
* <p> |
|
243 |
* 1. 校验相同父菜单编号下,是否存在相同的菜单名 |
|
244 |
* |
|
245 |
* @param name 菜单名字 |
|
246 |
* @param parentId 父菜单编号 |
|
247 |
* @param id 菜单编号 |
|
248 |
*/ |
|
249 |
@VisibleForTesting |
|
250 |
void validateMenu(Long parentId, String name, Long id) { |
|
251 |
AppMenuDO menu = appMenuMapper.selectByParentIdAndName(parentId, name); |
|
252 |
if (menu == null) { |
|
253 |
return; |
|
254 |
} |
|
255 |
// 如果 id 为空,说明不用比较是否为相同 id 的菜单 |
|
256 |
if (id == null) { |
|
257 |
throw exception(MENU_NAME_DUPLICATE); |
|
258 |
} |
|
259 |
if (!menu.getId().equals(id)) { |
|
260 |
throw exception(MENU_NAME_DUPLICATE); |
|
261 |
} |
|
262 |
} |
|
263 |
|
|
264 |
/** |
|
265 |
* 初始化菜单的通用属性。 |
|
266 |
* <p> |
|
267 |
* 例如说,只有目录或者菜单类型的菜单,才设置 icon |
|
268 |
* |
|
269 |
* @param menu 菜单 |
|
270 |
*/ |
|
271 |
private void initMenuProperty(AppMenuDO menu) { |
|
272 |
// 菜单为按钮类型时,无需 component、icon、path 属性,进行置空 |
|
273 |
if (MenuTypeEnum.BUTTON.getType().equals(menu.getType())) { |
|
274 |
menu.setComponent(""); |
|
275 |
menu.setComponentName(""); |
|
276 |
menu.setIcon(""); |
|
277 |
menu.setPath(""); |
|
278 |
} |
|
279 |
} |
|
280 |
|
|
281 |
} |