Browse Source

feat: [系统管理] [角色管理] 导出角色权限

master
memorylkf 1 week ago
parent
commit
41d49aa307
8 changed files with 175 additions and 3 deletions
  1. +0
    -1
      hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java
  2. +6
    -0
      hxhq-common/hxhq-common-core/pom.xml
  3. +96
    -2
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/controller/SysRoleController.java
  4. +44
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/dto/RoleMenuExportDto.java
  5. +9
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysRoleMapper.java
  6. +8
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/ISysRoleService.java
  7. +5
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java
  8. +7
    -0
      hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysRoleMapper.xml

+ 0
- 1
hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java View File

@ -30,7 +30,6 @@ public class SysRole extends BaseEntity
private String roleName; private String roleName;
/** 角色权限 */ /** 角色权限 */
@Excel(name = "角色编码",nameEn = "Role ID",sort = 2)
@Compare(name = "角色编码",nameEn = "Role ID") @Compare(name = "角色编码",nameEn = "Role ID")
private String roleKey; private String roleKey;

+ 6
- 0
hxhq-common/hxhq-common-core/pom.xml View File

@ -112,6 +112,12 @@
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

+ 96
- 2
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/controller/SysRoleController.java View File

@ -1,10 +1,13 @@
package com.hxhq.system.controller; package com.hxhq.system.controller;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.hxhq.business.domain.RoleChange; import com.hxhq.business.domain.RoleChange;
import com.hxhq.business.domain.SystemLog; import com.hxhq.business.domain.SystemLog;
import com.hxhq.business.form.common.SignForm; import com.hxhq.business.form.common.SignForm;
@ -13,12 +16,18 @@ import com.hxhq.business.service.ISystemLogService;
import com.hxhq.common.core.exception.ServiceException; import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.ServletUtils; import com.hxhq.common.core.utils.ServletUtils;
import com.hxhq.common.core.utils.StringUtils; import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.system.domain.SysMenu;
import com.hxhq.system.domain.SysRoleMenu;
import com.hxhq.system.domain.SysUserRole; import com.hxhq.system.domain.SysUserRole;
import com.hxhq.system.domain.vo.TreeSelect;
import com.hxhq.system.dto.RoleMenuExportDto;
import com.hxhq.system.form.RoleSaveForm; import com.hxhq.system.form.RoleSaveForm;
import com.hxhq.system.form.RoleUserSaveForm; import com.hxhq.system.form.RoleUserSaveForm;
import com.hxhq.system.service.ISysDeptService; import com.hxhq.system.service.ISysDeptService;
import com.hxhq.system.service.ISysMenuService;
import com.hxhq.system.service.ISysRoleService; import com.hxhq.system.service.ISysRoleService;
import com.hxhq.system.service.ISysUserService; import com.hxhq.system.service.ISysUserService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -66,6 +75,9 @@ public class SysRoleController extends BaseController
@Autowired @Autowired
private IRoleChangeService roleChangeService; private IRoleChangeService roleChangeService;
@Autowired
private ISysMenuService menuService;
@RequiresPermissions("system:role:list") @RequiresPermissions("system:role:list")
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo list(SysRole role) public TableDataInfo list(SysRole role)
@ -115,6 +127,9 @@ public class SysRoleController extends BaseController
{ {
return error("新增角色'" + role.getRoleName() + "'失败,角色编码已存在"); return error("新增角色'" + role.getRoleName() + "'失败,角色编码已存在");
} }
if(StringUtils.isBlank(role.getRoleKey())){
role.setRoleKey(UUID.randomUUID().toString());
}
role.setCreateBy(SecurityUtils.getUsername()); role.setCreateBy(SecurityUtils.getUsername());
return toAjax(roleService.insertRole(form)); return toAjax(roleService.insertRole(form));
@ -342,4 +357,83 @@ public class SysRoleController extends BaseController
ajax.put("depts", deptService.selectDeptTreeList(new SysDept())); ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
return ajax; return ajax;
} }
@RequiresPermissions("system:role:export")
@PostMapping("/exportMenu")
public void exportMenu(HttpServletResponse response, SysRole role) throws IOException {
//角色列表
List<SysRole> roleList = roleService.selectRoleList(role);
if(roleList.size()==0){
throw new ServiceException("没有数据");
}
//角色菜单列表
List<SysRoleMenu> roleMenuList = roleService.selectRoleMenuByRoleIdList(roleList.stream().map(o->o.getRoleId()).collect(Collectors.toList()));
SysMenu sysMenu = new SysMenu();
List<TreeSelect> menuTree = menuService.buildMenuTreeSelect(menuService.selectMenuList(sysMenu, SecurityUtils.getUserId()));
List<RoleMenuExportDto> menuList = new ArrayList<>();
transferTreeToList(menuList,menuTree,1);
Integer maxLevel = Collections.max(menuList,
Comparator.comparingInt(RoleMenuExportDto::getLevel)).getLevel();
// 设置响应
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("角色权限", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
// 动态表头假设我们有一个动态的表头列表这里用字符串列表表示
List<String> headList = new ArrayList<>();
for(int i=0;i<maxLevel;i++){
headList.add("菜单");
}
for(SysRole info : roleList){
headList.add(info.getRoleName());
}
// 但是EasyExcel的多级表头是List<List<String>>所以我们需要转换
List<List<String>> head = new ArrayList<>();
for (String h : headList) {
List<String> hh = new ArrayList<>();
hh.add(h);
head.add(hh);
}
// 数据部分每一行是一个List<Object>顺序与表头对应
List<List<Object>> data = new ArrayList<>();
for(RoleMenuExportDto dto : menuList){
List<Object> row = new ArrayList<>();
for(Integer i=1;i<=maxLevel;i++){
row.add(dto.getLevel().equals(i)?dto.getMenuName():"");
}
for(SysRole info : roleList){
row.add(roleMenuList.stream().filter(o->o.getMenuId().equals(dto.getMenuId()) && o.getRoleId().equals(info.getRoleId())).count()>0?"✓":"");
}
data.add(row);
}
EasyExcel.write(response.getOutputStream())
// 设置表头
.head(head)
// 设置sheet名称
.sheet("Sheet1")
// 写入数据
.doWrite(data);
}
void transferTreeToList(List<RoleMenuExportDto> list,List<TreeSelect> tree,Integer level){
for(TreeSelect t : tree){
RoleMenuExportDto item = new RoleMenuExportDto();
item.setMenuId(t.getId());
item.setMenuName(t.getLabel());
item.setLevel(level);
list.add(item);
if(t.getChildren()!=null && t.getChildren().size()>0){
transferTreeToList(list,t.getChildren(),level+1);
}
}
}
} }

+ 44
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/dto/RoleMenuExportDto.java View File

@ -0,0 +1,44 @@
package com.hxhq.system.dto;
/**
* @author memory
*/
public class RoleMenuExportDto {
/**
* 菜单id
*/
private Long menuId;
/**
* 菜单名称
*/
private String menuName;
/**
* 菜单级别
*/
private Integer level;
public Long getMenuId() {
return menuId;
}
public void setMenuId(Long menuId) {
this.menuId = menuId;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
}

+ 9
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysRoleMapper.java View File

@ -2,6 +2,8 @@ package com.hxhq.system.mapper;
import java.util.List; import java.util.List;
import com.hxhq.system.api.domain.SysRole; import com.hxhq.system.api.domain.SysRole;
import com.hxhq.system.domain.SysRoleMenu;
import org.apache.ibatis.annotations.Param;
/** /**
* 角色表 数据层 * 角色表 数据层
@ -111,4 +113,11 @@ public interface SysRoleMapper
* @return 结果 * @return 结果
*/ */
public int deleteRoleByIds(Long[] roleIds); public int deleteRoleByIds(Long[] roleIds);
/**
* 获取角色的菜单列表
* @param roleIdList
* @return
*/
List<SysRoleMenu> selectRoleMenuByRoleIdList(@Param("roleIdList") List<Long> roleIdList);
} }

+ 8
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/ISysRoleService.java View File

@ -3,6 +3,7 @@ package com.hxhq.system.service;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.hxhq.system.api.domain.SysRole; import com.hxhq.system.api.domain.SysRole;
import com.hxhq.system.domain.SysRoleMenu;
import com.hxhq.system.domain.SysUserRole; import com.hxhq.system.domain.SysUserRole;
import com.hxhq.system.form.RoleSaveForm; import com.hxhq.system.form.RoleSaveForm;
@ -179,4 +180,11 @@ public interface ISysRoleService
* @return 结果 * @return 结果
*/ */
public int insertAuthUsers(Long roleId, Long[] userIds); public int insertAuthUsers(Long roleId, Long[] userIds);
/**
* 获取角色的菜单列表
* @param roleList
* @return
*/
List<SysRoleMenu> selectRoleMenuByRoleIdList(List<Long> roleIdList);
} }

+ 5
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java View File

@ -565,4 +565,9 @@ public class SysRoleServiceImpl implements ISysRoleService
log.setQmrMcEn(SecurityUtils.getUsername()); log.setQmrMcEn(SecurityUtils.getUsername());
return log; return log;
} }
@Override
public List<SysRoleMenu> selectRoleMenuByRoleIdList(List<Long> roleIdList) {
return roleMapper.selectRoleMenuByRoleIdList(roleIdList);
}
} }

+ 7
- 0
hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysRoleMapper.xml View File

@ -156,5 +156,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{roleId} #{roleId}
</foreach> </foreach>
</delete> </delete>
<select id="selectRoleMenuByRoleIdList" parameterType="Long" resultType="com.hxhq.system.domain.SysRoleMenu">
SELECT role_id,menu_id FROM `sys_role_menu` WHERE role_id IN
<foreach collection="roleIdList" item="roleId" open="(" separator="," close=")">
#{roleId}
</foreach>
</select>
</mapper> </mapper>

Loading…
Cancel
Save