1
1
package cn .exrick .xboot .modules .base .controller .manage ;
2
2
3
3
import cn .exrick .xboot .common .constant .CommonConstant ;
4
+ import cn .exrick .xboot .common .exception .XbootException ;
4
5
import cn .exrick .xboot .common .redis .RedisTemplateHelper ;
6
+ import cn .exrick .xboot .common .utils .CommonUtil ;
5
7
import cn .exrick .xboot .common .utils .ResultUtil ;
6
8
import cn .exrick .xboot .common .utils .SecurityUtil ;
7
9
import cn .exrick .xboot .common .vo .Result ;
25
27
import org .springframework .cache .annotation .CacheEvict ;
26
28
import org .springframework .cache .annotation .Cacheable ;
27
29
import org .springframework .transaction .annotation .Transactional ;
28
- import org .springframework .web .bind .annotation .RequestMapping ;
29
- import org .springframework .web .bind .annotation .RequestMethod ;
30
- import org .springframework .web .bind .annotation .RequestParam ;
31
- import org .springframework .web .bind .annotation .RestController ;
30
+ import org .springframework .web .bind .annotation .*;
32
31
33
32
import java .util .Comparator ;
34
33
import java .util .List ;
@@ -134,55 +133,90 @@ private void getAllByRecursion(List<Permission> curr, List<Permission> list) {
134
133
List <Permission > children = list .stream ().filter (p -> (e .getId ()).equals (p .getParentId ()))
135
134
.sorted (Comparator .comparing (Permission ::getSortOrder )).collect (Collectors .toList ());
136
135
e .setChildren (children );
136
+ setInfo (e );
137
137
if (e .getLevel () < 3 ) {
138
138
getAllByRecursion (children , list );
139
139
}
140
140
});
141
141
}
142
142
143
+ @ RequestMapping (value = "/getByParentId/{parentId}" , method = RequestMethod .GET )
144
+ @ ApiOperation (value = "通过id获取" )
145
+ @ Cacheable (key = "#parentId" )
146
+ public Result <List <Permission >> getByParentId (@ PathVariable String parentId ) {
147
+
148
+ List <Permission > list = permissionService .findByParentIdOrderBySortOrder (parentId );
149
+ list .forEach (e -> setInfo (e ));
150
+ return ResultUtil .data (list );
151
+ }
152
+
143
153
@ RequestMapping (value = "/add" , method = RequestMethod .POST )
144
154
@ ApiOperation (value = "添加" )
145
155
@ CacheEvict (key = "'menuList'" )
146
156
public Result <Permission > add (Permission permission ) {
147
157
158
+ if (permission .getId ().equals (permission .getParentId ())) {
159
+ return ResultUtil .error ("上级节点不能为自己" );
160
+ }
148
161
// 判断拦截请求的操作权限按钮名是否已存在
149
162
if (CommonConstant .PERMISSION_OPERATION .equals (permission .getType ())) {
150
163
List <Permission > list = permissionService .findByTitle (permission .getTitle ());
151
164
if (list != null && list .size () > 0 ) {
152
165
return new ResultUtil <Permission >().setErrorMsg ("名称已存在" );
153
166
}
154
167
}
168
+ // 如果不是添加的一级 判断设置上级为父节点标识
169
+ if (!CommonConstant .PARENT_ID .equals (permission .getParentId ())) {
170
+ Permission parent = permissionService .get (permission .getParentId ());
171
+ if (parent .getIsParent () == null || !parent .getIsParent ()) {
172
+ parent .setIsParent (true );
173
+ permissionService .update (parent );
174
+ }
175
+ }
155
176
Permission u = permissionService .save (permission );
156
- //重新加载权限
177
+ // 重新加载权限
157
178
mySecurityMetadataSource .loadResourceDefine ();
158
- //手动删除缓存
159
- redisTemplate .delete ("permission::allList " );
179
+ // 手动删除缓存
180
+ redisTemplate .deleteByPattern ("permission:* " );
160
181
return new ResultUtil <Permission >().setData (u );
161
182
}
162
183
163
184
@ RequestMapping (value = "/edit" , method = RequestMethod .POST )
164
185
@ ApiOperation (value = "编辑" )
165
186
public Result <Permission > edit (Permission permission ) {
166
187
188
+ if (permission .getId ().equals (permission .getParentId ())) {
189
+ return ResultUtil .error ("上级节点不能为自己" );
190
+ }
167
191
// 判断拦截请求的操作权限按钮名是否已存在
168
192
if (CommonConstant .PERMISSION_OPERATION .equals (permission .getType ())) {
169
193
// 若名称修改
170
194
Permission p = permissionService .get (permission .getId ());
171
195
if (!p .getTitle ().equals (permission .getTitle ())) {
172
196
List <Permission > list = permissionService .findByTitle (permission .getTitle ());
173
197
if (list != null && list .size () > 0 ) {
174
- return new ResultUtil < Permission >(). setErrorMsg ("名称已存在" );
198
+ return ResultUtil . error ("名称已存在" );
175
199
}
176
200
}
177
201
}
202
+ Permission old = permissionService .get (permission .getId ());
203
+ String oldParentId = old .getParentId ();
178
204
Permission u = permissionService .update (permission );
205
+ // 如果该节点不是一级节点 且修改了级别 判断上级还有无子节点
206
+ if (!CommonConstant .PARENT_ID .equals (oldParentId ) && !oldParentId .equals (permission .getParentId ())) {
207
+ Permission parent = permissionService .get (oldParentId );
208
+ List <Permission > children = permissionService .findByParentIdOrderBySortOrder (parent .getId ());
209
+ if (parent != null && (children == null || children .isEmpty ())) {
210
+ parent .setIsParent (false );
211
+ permissionService .update (parent );
212
+ }
213
+ }
179
214
// 重新加载权限
180
215
mySecurityMetadataSource .loadResourceDefine ();
181
216
// 手动批量删除缓存
182
- redisTemplate .deleteByPattern ("user:" + "*" );
183
- redisTemplate .deleteByPattern ("permission::userMenuList:*" );
184
- redisTemplate .delete ("permission::allList" );
185
- return new ResultUtil <Permission >().setData (u );
217
+ redisTemplate .deleteByPattern ("user:*" );
218
+ redisTemplate .deleteByPattern ("permission:*" );
219
+ return ResultUtil .data (u );
186
220
}
187
221
188
222
@ RequestMapping (value = "/delByIds" , method = RequestMethod .POST )
@@ -191,26 +225,60 @@ public Result<Permission> edit(Permission permission) {
191
225
public Result <Object > delByIds (@ RequestParam String [] ids ) {
192
226
193
227
for (String id : ids ) {
194
- List <RolePermission > list = rolePermissionService .findByPermissionId (id );
195
- if (list != null && list .size () > 0 ) {
196
- return ResultUtil .error ("删除失败,包含正被角色使用关联的菜单或权限" );
197
- }
198
- }
199
- for (String id : ids ) {
200
- permissionService .delete (id );
228
+ deleteRecursion (id , ids );
201
229
}
202
230
// 重新加载权限
203
231
mySecurityMetadataSource .loadResourceDefine ();
204
232
// 手动删除缓存
205
- redisTemplate .delete ("permission::allList " );
233
+ redisTemplate .deleteByPattern ("permission:* " );
206
234
return ResultUtil .success ("批量通过id删除数据成功" );
207
235
}
208
236
237
+ public void deleteRecursion (String id , String [] ids ) {
238
+
239
+ List <RolePermission > list = rolePermissionService .findByPermissionId (id );
240
+ if (list != null && list .size () > 0 ) {
241
+ throw new XbootException ("删除失败,包含正被用户使用关联的部门" );
242
+ }
243
+ // 获得其父节点
244
+ Permission p = permissionService .get (id );
245
+ Permission parent = null ;
246
+ if (p != null && StrUtil .isNotBlank (p .getParentId ())) {
247
+ parent = permissionService .get (p .getParentId ());
248
+ }
249
+ permissionService .delete (id );
250
+ // 判断父节点是否还有子节点
251
+ if (parent != null ) {
252
+ List <Permission > children = permissionService .findByParentIdOrderBySortOrder (parent .getId ());
253
+ if (children == null || children .isEmpty ()) {
254
+ parent .setIsParent (false );
255
+ permissionService .update (parent );
256
+ }
257
+ }
258
+ // 递归删除
259
+ List <Permission > permissions = permissionService .findByParentIdOrderBySortOrder (id );
260
+ for (Permission pe : permissions ) {
261
+ if (!CommonUtil .judgeIds (pe .getId (), ids )) {
262
+ deleteRecursion (pe .getId (), ids );
263
+ }
264
+ }
265
+ }
266
+
209
267
@ RequestMapping (value = "/search" , method = RequestMethod .GET )
210
268
@ ApiOperation (value = "搜索菜单" )
211
269
public Result <List <Permission >> searchPermissionList (@ RequestParam String title ) {
212
270
213
271
List <Permission > list = permissionService .findByTitleLikeOrderBySortOrder ("%" + title + "%" );
214
272
return new ResultUtil <List <Permission >>().setData (list );
215
273
}
274
+
275
+ public void setInfo (Permission permission ) {
276
+
277
+ if (!CommonConstant .PARENT_ID .equals (permission .getParentId ())) {
278
+ Permission parent = permissionService .get (permission .getParentId ());
279
+ permission .setParentTitle (parent .getTitle ());
280
+ } else {
281
+ permission .setParentTitle ("一级菜单" );
282
+ }
283
+ }
216
284
}
0 commit comments