提交 | 用户 | 时间
|
5f25e3
|
1 |
package com.iailab.module.system.service.app; |
潘 |
2 |
|
818a01
|
3 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
H |
4 |
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
5f25e3
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
潘 |
6 |
import com.iailab.framework.common.util.object.BeanUtils; |
818a01
|
7 |
import com.iailab.framework.mybatis.core.query.LambdaQueryWrapperX; |
H |
8 |
import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
|
9 |
import com.iailab.framework.tenant.core.aop.TenantIgnore; |
5f25e3
|
10 |
import com.iailab.module.system.controller.admin.app.vo.AppPageReqVO; |
潘 |
11 |
import com.iailab.module.system.controller.admin.app.vo.AppSaveReqVO; |
|
12 |
import com.iailab.module.system.dal.dataobject.app.AppDO; |
818a01
|
13 |
import com.iailab.module.system.dal.dataobject.permission.MenuDO; |
H |
14 |
import com.iailab.module.system.dal.dataobject.permission.RoleDO; |
|
15 |
import com.iailab.module.system.dal.dataobject.permission.RoleMenuDO; |
|
16 |
import com.iailab.module.system.dal.dataobject.tenant.TenantDO; |
|
17 |
import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO; |
5f25e3
|
18 |
import com.iailab.module.system.dal.mysql.app.AppMapper; |
818a01
|
19 |
import com.iailab.module.system.dal.mysql.permission.MenuMapper; |
H |
20 |
import com.iailab.module.system.dal.mysql.permission.RoleMapper; |
|
21 |
import com.iailab.module.system.dal.mysql.permission.RoleMenuMapper; |
|
22 |
import com.iailab.module.system.dal.mysql.tenant.TenantMapper; |
|
23 |
import com.iailab.module.system.dal.mysql.tenant.TenantPackageMapper; |
|
24 |
import com.iailab.module.system.enums.permission.MenuTypeEnum; |
|
25 |
import com.iailab.module.system.service.permission.PermissionService; |
5f25e3
|
26 |
import lombok.extern.slf4j.Slf4j; |
818a01
|
27 |
import org.apache.commons.lang3.ObjectUtils; |
5f25e3
|
28 |
import org.springframework.stereotype.Service; |
818a01
|
29 |
import org.springframework.transaction.annotation.Transactional; |
5f25e3
|
30 |
|
潘 |
31 |
import javax.annotation.Resource; |
818a01
|
32 |
import java.util.*; |
H |
33 |
|
|
34 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
35 |
import static com.iailab.framework.tenant.core.context.TenantContextHolder.getTenantId; |
|
36 |
import static com.iailab.module.system.enums.ErrorCodeConstants.MENU_EXISTS_CHILDREN; |
|
37 |
import static com.iailab.module.system.enums.ErrorCodeConstants.MENU_NOT_EXISTS; |
5f25e3
|
38 |
|
潘 |
39 |
/** |
|
40 |
* @author PanZhibao |
|
41 |
* @Description |
|
42 |
* @createTime 2024年08月17日 |
|
43 |
*/ |
|
44 |
@Service |
|
45 |
@Slf4j |
|
46 |
public class AppServiceImpl implements AppService { |
|
47 |
|
|
48 |
@Resource |
|
49 |
private AppMapper appMapper; |
|
50 |
|
818a01
|
51 |
@Resource |
H |
52 |
private MenuMapper menuMapper; |
|
53 |
|
|
54 |
@Resource |
|
55 |
private RoleMapper roleMapper; |
|
56 |
|
|
57 |
@Resource |
|
58 |
private RoleMenuMapper roleMenuMapper; |
|
59 |
|
|
60 |
@Resource |
|
61 |
private PermissionService permissionService; |
|
62 |
|
|
63 |
@Resource |
|
64 |
private TenantPackageMapper tenantPackageMapper; |
|
65 |
|
|
66 |
@Resource |
|
67 |
private TenantMapper tenantMapper; |
|
68 |
|
5f25e3
|
69 |
|
潘 |
70 |
@Override |
818a01
|
71 |
@Transactional(rollbackFor = Exception.class) |
H |
72 |
@TenantIgnore |
5f25e3
|
73 |
public Long create(AppSaveReqVO createReqVO) { |
潘 |
74 |
AppDO app = BeanUtils.toBean(createReqVO, AppDO.class); |
|
75 |
appMapper.insert(app); |
818a01
|
76 |
// //为应用创建默认菜单并授权 |
H |
77 |
dealAppMenu(1, app); |
5f25e3
|
78 |
return app.getId(); |
潘 |
79 |
} |
|
80 |
|
|
81 |
@Override |
818a01
|
82 |
@Transactional(rollbackFor = Exception.class) |
H |
83 |
@TenantIgnore |
5f25e3
|
84 |
public Long update(AppSaveReqVO createReqVO) { |
潘 |
85 |
AppDO app = BeanUtils.toBean(createReqVO, AppDO.class); |
|
86 |
appMapper.updateById(app); |
818a01
|
87 |
// //修改默认菜单并授权 |
H |
88 |
dealAppMenu(2, app); |
5f25e3
|
89 |
return app.getId(); |
潘 |
90 |
} |
|
91 |
|
|
92 |
@Override |
818a01
|
93 |
@Transactional(rollbackFor = Exception.class) |
H |
94 |
@TenantIgnore |
5f25e3
|
95 |
public void delete(Long id) { |
818a01
|
96 |
AppDO appDO = new AppDO(); |
H |
97 |
appDO.setId(id); |
|
98 |
dealAppMenu(3, appDO); |
5f25e3
|
99 |
appMapper.deleteById(id); |
潘 |
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public AppDO getInfo(Long id) { |
|
104 |
return appMapper.selectById(id); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public PageResult<AppDO> getPage(AppPageReqVO pageReqVO) { |
|
109 |
return appMapper.selectPage(pageReqVO); |
|
110 |
} |
|
111 |
|
818a01
|
112 |
@Override |
H |
113 |
public List<AppDO> getList() { |
|
114 |
Long tenantId = getTenantId(); |
|
115 |
LambdaQueryWrapperX<MenuDO> menuDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>(); |
|
116 |
menuDOLambdaQueryWrapperX.eq(MenuDO::getParentId, 0) |
|
117 |
.eq(MenuDO::getAppId, 0); |
|
118 |
if (tenantId != 1) { |
|
119 |
//非系统租户查租户套餐 |
|
120 |
TenantDO tenantDO = tenantMapper.selectById(tenantId); |
|
121 |
TenantPackageDO tenantPackageDO = tenantPackageMapper.selectById(tenantDO.getPackageId()); |
|
122 |
menuDOLambdaQueryWrapperX.in(MenuDO::getId, tenantPackageDO.getMenuIds()); |
|
123 |
} |
|
124 |
//查询系统应用菜单 |
|
125 |
// List<MenuDO> menuDOS = menuMapper.selectList(menuDOLambdaQueryWrapperX); |
|
126 |
// List<AppDO> systemApps = convertMenuToApp(menuDOS); |
|
127 |
//创建一个系统管理应用菜单 |
|
128 |
AppDO aDo = new AppDO(); |
|
129 |
aDo.setAppType(1); |
|
130 |
aDo.setAppName("系统管理"); |
|
131 |
aDo.setOrderNum(0); |
|
132 |
List<AppDO> systemApps = new ArrayList<>(); |
|
133 |
systemApps.add(aDo); |
|
134 |
List<AppDO> appDOS = appMapper.selectList(); |
|
135 |
//暂时先遍历处理应用菜单和应用类型 |
|
136 |
appDOS.stream().forEach(appDO -> { |
|
137 |
List<MenuDO> menuDOS = menuMapper.selectList(new LambdaQueryWrapper<MenuDO>().eq(MenuDO::getParentId, 0) |
|
138 |
.eq(MenuDO::getAppId, appDO.getId())); |
|
139 |
appDO.setAppMenuId(menuDOS.get(0).getId()); |
|
140 |
appDO.setAppType(2); |
|
141 |
}); |
|
142 |
systemApps.addAll(appDOS); |
|
143 |
return systemApps; |
|
144 |
} |
|
145 |
|
|
146 |
@Override |
|
147 |
public AppDO getAppByTenantId(Long tenantId) { |
|
148 |
//暂时支持一个租户对应一个应用 |
|
149 |
List<AppDO> appDOS = appMapper.selectList(new LambdaQueryWrapper<AppDO>().eq(AppDO::getTenantId, tenantId)); |
|
150 |
if(ObjectUtils.isNotEmpty(appDOS)) { |
|
151 |
return appDOS.get(0); |
|
152 |
} else { |
|
153 |
AppDO appDO = new AppDO(); |
|
154 |
appDO.setTenantId(tenantId); |
|
155 |
appDO.setId(0L); |
|
156 |
return appDO; |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
// @Override |
|
161 |
// public List<MenuRespDTO> getAppMenu(Long id) { |
|
162 |
// |
|
163 |
// List<MenuDO> children = new LinkedList<>(); |
|
164 |
// // 遍历每一层 |
|
165 |
// Collection<Long> parentIds = Collections.singleton(id); |
|
166 |
// for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 |
|
167 |
// // 查询当前层,所有的子应用菜单 |
|
168 |
// List<MenuDO> menus = menuMapper.selectListByParentId(parentIds); |
|
169 |
// // 1. 如果没有子菜单,则结束遍历 |
|
170 |
// if (CollUtil.isEmpty(menus)) { |
|
171 |
// break; |
|
172 |
// } |
|
173 |
// // 2. 如果有子应用菜单,继续遍历 |
|
174 |
// children.addAll(menus); |
|
175 |
// parentIds = convertSet(menus, MenuDO::getId); |
|
176 |
// } |
|
177 |
// children = menuService.filterDisableMenus(children); |
|
178 |
// return AuthConvert.INSTANCE.buildMenuTree(id, children); |
|
179 |
// |
|
180 |
// } |
|
181 |
|
|
182 |
private void dealAppMenu(Integer type, AppDO app){ |
|
183 |
String loginUserNickname = SecurityFrameworkUtils.getLoginUserNickname(); |
|
184 |
MenuDO menuDO = new MenuDO(); |
|
185 |
menuDO.setAppId(app.getId()); |
|
186 |
menuDO.setName(app.getAppName()); |
|
187 |
menuDO.setType(MenuTypeEnum.DIR.getType()); |
|
188 |
menuDO.setSort(app.getOrderNum()); |
|
189 |
menuDO.setPath("/" + app.getAppCode()); |
|
190 |
menuDO.setTenantId(app.getTenantId()); |
|
191 |
if(type == 1){ |
|
192 |
menuDO.setCreator(loginUserNickname); |
|
193 |
menuDO.setCreateTime(app.getCreateTime()); |
|
194 |
menuMapper.insert(menuDO); |
|
195 |
//内置租户角色分配菜单 |
|
196 |
assignRoleMenu(menuDO.getId(), app.getTenantId()); |
|
197 |
} else if(type == 2){ |
|
198 |
LambdaUpdateWrapper<MenuDO> updateWrapper = new LambdaUpdateWrapper<>(); |
|
199 |
updateWrapper.eq(MenuDO::getAppId, app.getId()); |
|
200 |
updateWrapper.eq(MenuDO::getParentId, 0L); |
|
201 |
List<MenuDO> menuDOS = menuMapper.selectList(updateWrapper); |
|
202 |
if(menuDOS.size() > 0){ |
|
203 |
menuDO.setUpdater(loginUserNickname); |
|
204 |
menuDO.setUpdateTime(app.getUpdateTime()); |
|
205 |
menuMapper.update(menuDO, updateWrapper); |
|
206 |
} else { |
|
207 |
menuDO.setCreator(loginUserNickname); |
|
208 |
menuDO.setCreateTime(app.getCreateTime()); |
|
209 |
menuMapper.insert(menuDO); |
|
210 |
//内置租户角色分配菜单 |
|
211 |
assignRoleMenu(menuDO.getId(), app.getTenantId()); |
|
212 |
} |
|
213 |
} else if(type == 3){ |
|
214 |
//删除租户、角色权限 |
|
215 |
app = appMapper.selectById(app.getId()); |
|
216 |
LambdaQueryWrapperX<MenuDO> menuWrapper = new LambdaQueryWrapperX<>(); |
|
217 |
menuWrapper.eq(MenuDO::getAppId, app.getId()); |
|
218 |
menuWrapper.eq(MenuDO::getType, MenuTypeEnum.DIR.getType()); |
|
219 |
MenuDO menu = menuMapper.selectOne(menuWrapper); |
|
220 |
TenantDO tenantDO = tenantMapper.selectById(app.getTenantId()); |
|
221 |
TenantPackageDO tenantPackageDO = tenantPackageMapper.selectById(tenantDO.getPackageId()); |
|
222 |
Set<Long> menuIds = tenantPackageDO.getMenuIds(); |
|
223 |
menuIds.remove(menu.getId()); |
|
224 |
// 校验是否还有子菜单 |
|
225 |
if (menuMapper.selectCountByParentId(menu.getId()) > 0) { |
|
226 |
throw exception(MENU_EXISTS_CHILDREN); |
|
227 |
} |
|
228 |
// 校验删除的菜单是否存在 |
|
229 |
if (menuMapper.selectById(menu.getId()) == null) { |
|
230 |
throw exception(MENU_NOT_EXISTS); |
|
231 |
} |
|
232 |
// 标记删除 |
|
233 |
menuMapper.deleteById(menu.getId()); |
|
234 |
// 删除授予给角色的权限 |
|
235 |
permissionService.processMenuDeleted(menu.getId()); |
|
236 |
//删除菜单 |
|
237 |
menuMapper.delete(menuWrapper); |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
private void assignRoleMenu(Long menuId, Long tenantId) { |
|
242 |
//查询内置租户管理员 |
|
243 |
LambdaQueryWrapperX<RoleDO> roleQueryWrapper = new LambdaQueryWrapperX<>(); |
|
244 |
roleQueryWrapper.eq(RoleDO::getCode, "tenant_admin"); |
|
245 |
roleQueryWrapper.eq(RoleDO::getTenantId, tenantId); |
|
246 |
RoleDO roleDO = roleMapper.selectOne(roleQueryWrapper); |
|
247 |
RoleMenuDO entity = new RoleMenuDO(); |
|
248 |
entity.setRoleId(roleDO.getId()); |
|
249 |
entity.setMenuId(menuId); |
|
250 |
entity.setTenantId(tenantId); |
|
251 |
roleMenuMapper.insert(entity); |
|
252 |
TenantDO tenantDO = tenantMapper.selectById(tenantId); |
|
253 |
TenantPackageDO tenantPackageDO = tenantPackageMapper.selectById(tenantDO.getPackageId()); |
|
254 |
Set<Long> menuIds = tenantPackageDO.getMenuIds(); |
|
255 |
menuIds.add(menuId); |
|
256 |
tenantPackageMapper.updateById(tenantPackageDO); |
|
257 |
} |
|
258 |
|
|
259 |
private List<AppDO> convertMenuToApp(List<MenuDO> menuDOS) { |
|
260 |
List<AppDO> appDOS = new ArrayList<>(); |
|
261 |
menuDOS.stream().forEach(menuDO -> { |
|
262 |
AppDO appDO = new AppDO(); |
|
263 |
appDO.setAppName(menuDO.getName()); |
|
264 |
appDO.setOrderNum(menuDO.getSort()); |
|
265 |
appDO.setAppMenuId(menuDO.getId()); |
|
266 |
appDO.setAppType(1); |
|
267 |
appDOS.add(appDO); |
|
268 |
}); |
|
269 |
return appDOS; |
|
270 |
} |
|
271 |
|
5f25e3
|
272 |
} |