Browse Source

保存角色菜单数据

tags/v1.0.0^2
chengwang 1 year ago
parent
commit
2866b23af7
8 changed files with 170 additions and 6 deletions
  1. +13
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/RoleController.java
  2. +21
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/RoleMenuDto.java
  3. +8
    -1
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/IRoleMenuService.java
  4. +21
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/OpRolePermissionService.java
  5. +36
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/impl/OpRolePermissionServiceImpl.java
  6. +17
    -1
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/impl/RoleMenuServiceImpl.java
  7. +2
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/IRoleService.java
  8. +52
    -4
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/RoleServiceImpl.java

+ 13
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/RoleController.java View File

@@ -1,5 +1,6 @@
package com.tuoheng.admin.controller;

import com.tuoheng.admin.dto.RoleMenuDto;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
@@ -95,5 +96,17 @@ public class RoleController {
return roleService.updateStatus(entity);
}

/**
* 保存角色权限数据
*
* @param roleMenuDto 角色菜单Dto
* @return
*/
// @RequiresPermissions("sys:role:permission")
@PostMapping("/savePermission")
public JsonResult savePermission(@RequestBody RoleMenuDto roleMenuDto) {
return roleService.savePermission(roleMenuDto);
}


}

+ 21
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/RoleMenuDto.java View File

@@ -0,0 +1,21 @@
package com.tuoheng.admin.dto;

import lombok.Data;

/**
* 角色菜单Dto
*/
@Data
public class RoleMenuDto {

/**
* 角色ID
*/
private Integer roleId;

/**
* 菜单ID或者权限
*/
private String[] ids;

}

+ 8
- 1
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/IRoleMenuService.java View File

@@ -7,5 +7,12 @@ import com.tuoheng.common.core.common.IBaseService;
* @Author ChengWang
* @Date 2022/12/16
*/
public interface IRoleMenuService{
public interface IRoleMenuService {

/**
* 根据角色ID删除角色菜单关系数据
*
* @param roleId
*/
boolean deleteRoleMenus(Integer roleId);
}

+ 21
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/OpRolePermissionService.java View File

@@ -0,0 +1,21 @@
package com.tuoheng.admin.service;


import com.baomidou.mybatisplus.extension.service.IService;
import com.tuoheng.admin.entity.OpRolePermission;

/**
* @author 小影
* @description 针对表【op_role_permission(角色可操作的permission)】的数据库操作Service
* @createDate 2022-11-21 15:24:21
*/
public interface OpRolePermissionService {

/**
* 根据角色ID删除角色权限关系数据
*
* @param roleId
*/
boolean deleteRolePermissions(Integer roleId);

}

+ 36
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/impl/OpRolePermissionServiceImpl.java View File

@@ -0,0 +1,36 @@
package com.tuoheng.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.tuoheng.admin.entity.OpRolePermission;
import com.tuoheng.admin.mapper.OpRolePermissionMapper;
import com.tuoheng.admin.service.OpRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @Author ChengWang
* @Date 2022/12/26
*/
@Service
@Slf4j
public class OpRolePermissionServiceImpl implements OpRolePermissionService {

@Autowired
private OpRolePermissionMapper opRolePermissionMapper;
/**
* 根据角色ID删除角色菜单关系数据
*
* @param roleId 角色ID
* @return
*/
@Override
public boolean deleteRolePermissions(Integer roleId) {
QueryWrapper<OpRolePermission> queryWrapper = new QueryWrapper<>();
//queryWrapper.eq("tenant_id", ShiroUtils.getTenantId());
queryWrapper.eq("role_id", roleId);
boolean result = SqlHelper.retBool(opRolePermissionMapper.delete(queryWrapper));
return result;
}
}

+ 17
- 1
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/impl/RoleMenuServiceImpl.java View File

@@ -1,8 +1,12 @@
package com.tuoheng.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.tuoheng.admin.entity.RoleMenu;
import com.tuoheng.admin.mapper.RoleMenuMapper;
import com.tuoheng.admin.service.IRoleMenuService;
import com.tuoheng.common.core.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
@@ -10,5 +14,17 @@ import org.springframework.stereotype.Service;
* @Date 2022/12/16
*/
@Service
public class RoleMenuServiceImpl implements IRoleMenuService {
public class RoleMenuServiceImpl implements IRoleMenuService{

@Autowired
private RoleMenuMapper roleMenuMapper;

@Override
public boolean deleteRoleMenus(Integer roleId) {
QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
//queryWrapper.eq("tenant_id", ShiroUtils.getTenantId());
queryWrapper.eq("role_id", roleId);
boolean result = SqlHelper.retBool(roleMenuMapper.delete(queryWrapper));
return result;
}
}

+ 2
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/IRoleService.java View File

@@ -1,5 +1,6 @@
package com.tuoheng.admin.service.role;

import com.tuoheng.admin.dto.RoleMenuDto;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
@@ -24,4 +25,5 @@ public interface IRoleService {

JsonResult updateStatus(Role entity);

JsonResult savePermission(RoleMenuDto roleMenuDto);
}

+ 52
- 4
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/role/RoleServiceImpl.java View File

@@ -7,16 +7,15 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.dto.MenuVo;
import com.tuoheng.admin.dto.OpPermissionsVo;
import com.tuoheng.admin.dto.RoleMenuPermissionDto;
import com.tuoheng.admin.dto.UserRoleDto;
import com.tuoheng.admin.dto.*;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.RoleEnum;
import com.tuoheng.admin.enums.code.role.RoleDeleteEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
import com.tuoheng.admin.service.IRoleMenuService;
import com.tuoheng.admin.service.OpRolePermissionService;
import com.tuoheng.admin.service.menu.IMenuService;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.RoleListVo;
@@ -29,8 +28,10 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
@@ -55,12 +56,19 @@ public class RoleServiceImpl implements IRoleService {
@Autowired
private IMenuService menuService;

@Autowired
private IRoleMenuService roleMenuService;

@Autowired
private MenuMapper menuMapper;

@Autowired
private OpPermissionsMapper opPermissionsMapper;

@Autowired
private OpRolePermissionService opRolePermissionService;




@Override
@@ -276,5 +284,45 @@ public class RoleServiceImpl implements IRoleService {
return JsonResult.success();
}

@Override
public JsonResult savePermission(RoleMenuDto roleMenuDto) {
// 角色ID校验
if (StringUtils.isNull(roleMenuDto.getRoleId())) {
return JsonResult.error("角色ID不能位空");
}
// 同步删除角色菜单,权限关系数据
roleMenuService.deleteRoleMenus(roleMenuDto.getRoleId());
opRolePermissionService.deleteRolePermissions(roleMenuDto.getRoleId());

// 插入新的角色菜单关系数据and权限
List<RoleMenu> roleMenuList = new ArrayList<>();
List<OpRolePermission> permissions = new ArrayList<>();
if (roleMenuDto.getIds().length > 0) {
for (String id : roleMenuDto.getIds()) {
if (id.contains("_")) {
id = id.split("_")[1];
OpRolePermission opRolePermission = new OpRolePermission();
opRolePermission.setRoleId(roleMenuDto.getRoleId());
opRolePermission.setPermissionId(Integer.parseInt(id));
permissions.add(opRolePermission);
} else {
RoleMenu roleMenu = new RoleMenu();
//roleMenu.setTenantId(ShiroUtils.getTenantId());
roleMenu.setRoleId(roleMenuDto.getRoleId());
roleMenu.setMenuId(Integer.parseInt(id));
roleMenuList.add(roleMenu);
}
}
}
// 批量插入角色菜单关系数据
roleMenuList.forEach(item->{
roleMenuMapper.insert(item);
});
permissions.forEach(item->{
opRolePermissionMapper.insert(item);
});
return JsonResult.success("权限保存成功");
}


}

Loading…
Cancel
Save