|
@@ -1,13 +1,17 @@
|
|
|
package com.pj.project.re_role_menu;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.pj.project.re_role_menu.dto.AssignPermissionsDto;
|
|
|
import com.pj.utils.so.SoMap;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import com.pj.utils.sg.*;
|
|
|
|
|
|
/**
|
|
|
* Service: re_role_menu -- app用户菜单中间表
|
|
@@ -20,6 +24,9 @@ public class ReRoleMenuService extends ServiceImpl<ReRoleMenuMapper, ReRoleMenu>
|
|
|
/** 底层 Mapper 对象 */
|
|
|
@Autowired
|
|
|
ReRoleMenuMapper reRoleMenuMapper;
|
|
|
+ /** 方法抽取,优化代码 */
|
|
|
+ @Autowired
|
|
|
+ private MethodRoleMenuService methodRoleMenuService;
|
|
|
|
|
|
/** 增 */
|
|
|
void add(ReRoleMenu r){
|
|
@@ -47,6 +54,37 @@ public class ReRoleMenuService extends ServiceImpl<ReRoleMenuMapper, ReRoleMenu>
|
|
|
so.set("app_role_id",appRoleId);
|
|
|
return reRoleMenuMapper.getList(so);
|
|
|
}
|
|
|
+
|
|
|
+ /** 给app角色分配权限 */
|
|
|
+ boolean assignPermissions(AssignPermissionsDto assignPermissionsDto){
|
|
|
+ //取出数据
|
|
|
+ Long roleId = assignPermissionsDto.getRoleId();
|
|
|
+ List<Long> permissionsDtoList = assignPermissionsDto.getList().stream().distinct().collect(Collectors.toList());
|
|
|
+ //情况1:传进来的集合为空,默认删除该职务下属的所有权限
|
|
|
+ if(permissionsDtoList.size() == 0){
|
|
|
+ //删除所有权限
|
|
|
+ List<Long> perList = methodRoleMenuService.getPerList(roleId);
|
|
|
+ //清除
|
|
|
+ int i = reRoleMenuMapper.deleteBatchIds(perList);
|
|
|
+ return i == perList.size();
|
|
|
+ }
|
|
|
+ //情况2: 传进来的集合不为空
|
|
|
+ //2.1 先清除该职务的权限
|
|
|
+ List<Long> perList = methodRoleMenuService.getPerList(roleId);
|
|
|
+ //执行清除
|
|
|
+ int i = reRoleMenuMapper.deleteBatchIds(perList);
|
|
|
+ if(i != perList.size())throw new RuntimeException("权限分配失败Ⅰ!");
|
|
|
+ //2.2 再添加新的权限给到该职务
|
|
|
+ //定义计数器
|
|
|
+ int count = 0;
|
|
|
+ for(Long item : permissionsDtoList){
|
|
|
+ int insert = reRoleMenuMapper.insert(new ReRoleMenu(roleId, item));
|
|
|
+ count += insert;
|
|
|
+ }
|
|
|
+ if(count != permissionsDtoList.size())throw new RuntimeException("权限分配失败Ⅱ!");
|
|
|
+
|
|
|
+ return count == permissionsDtoList.size();
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|