diff --git a/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java b/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java index 7e05d0d..4a448fc 100644 --- a/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java +++ b/hxhq-api/hxhq-api-system/src/main/java/com/hxhq/system/api/domain/SysRole.java @@ -30,7 +30,6 @@ public class SysRole extends BaseEntity private String roleName; /** 角色权限 */ - @Excel(name = "角色编码",nameEn = "Role ID",sort = 2) @Compare(name = "角色编码",nameEn = "Role ID") private String roleKey; diff --git a/hxhq-common/hxhq-common-core/pom.xml b/hxhq-common/hxhq-common-core/pom.xml index 69d0b65..bba72ed 100644 --- a/hxhq-common/hxhq-common-core/pom.xml +++ b/hxhq-common/hxhq-common-core/pom.xml @@ -112,6 +112,12 @@ mybatis-plus-boot-starter + + com.alibaba + easyexcel + 3.3.2 + + diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/controller/SysRoleController.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/controller/SysRoleController.java index b647b78..ff193d6 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/controller/SysRoleController.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/controller/SysRoleController.java @@ -1,10 +1,13 @@ 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 javax.servlet.http.HttpServletResponse; +import com.alibaba.excel.EasyExcel; import com.hxhq.business.domain.RoleChange; import com.hxhq.business.domain.SystemLog; 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.utils.ServletUtils; 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.vo.TreeSelect; +import com.hxhq.system.dto.RoleMenuExportDto; import com.hxhq.system.form.RoleSaveForm; import com.hxhq.system.form.RoleUserSaveForm; import com.hxhq.system.service.ISysDeptService; +import com.hxhq.system.service.ISysMenuService; import com.hxhq.system.service.ISysRoleService; import com.hxhq.system.service.ISysUserService; +import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; @@ -66,6 +75,9 @@ public class SysRoleController extends BaseController @Autowired private IRoleChangeService roleChangeService; + @Autowired + private ISysMenuService menuService; + @RequiresPermissions("system:role:list") @GetMapping("/list") public TableDataInfo list(SysRole role) @@ -115,6 +127,9 @@ public class SysRoleController extends BaseController { return error("新增角色'" + role.getRoleName() + "'失败,角色编码已存在"); } + if(StringUtils.isBlank(role.getRoleKey())){ + role.setRoleKey(UUID.randomUUID().toString()); + } role.setCreateBy(SecurityUtils.getUsername()); return toAjax(roleService.insertRole(form)); @@ -342,4 +357,83 @@ public class SysRoleController extends BaseController ajax.put("depts", deptService.selectDeptTreeList(new SysDept())); return ajax; } + + @RequiresPermissions("system:role:export") + @PostMapping("/exportMenu") + public void exportMenu(HttpServletResponse response, SysRole role) throws IOException { + //角色列表 + List roleList = roleService.selectRoleList(role); + if(roleList.size()==0){ + throw new ServiceException("没有数据"); + } + //角色菜单列表 + List roleMenuList = roleService.selectRoleMenuByRoleIdList(roleList.stream().map(o->o.getRoleId()).collect(Collectors.toList())); + + SysMenu sysMenu = new SysMenu(); + List menuTree = menuService.buildMenuTreeSelect(menuService.selectMenuList(sysMenu, SecurityUtils.getUserId())); + List 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 headList = new ArrayList<>(); + for(int i=0;i>,所以我们需要转换 + List> head = new ArrayList<>(); + for (String h : headList) { + List hh = new ArrayList<>(); + hh.add(h); + head.add(hh); + } + + // 数据部分:每一行是一个List,顺序与表头对应 + List> data = new ArrayList<>(); + for(RoleMenuExportDto dto : menuList){ + List 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 list,List 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); + } + } + } + } diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/dto/RoleMenuExportDto.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/dto/RoleMenuExportDto.java new file mode 100644 index 0000000..bd8eee1 --- /dev/null +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/dto/RoleMenuExportDto.java @@ -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; + } +} diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysRoleMapper.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysRoleMapper.java index f26a792..8d0773b 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysRoleMapper.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/mapper/SysRoleMapper.java @@ -2,6 +2,8 @@ package com.hxhq.system.mapper; import java.util.List; 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 结果 */ public int deleteRoleByIds(Long[] roleIds); + + /** + * 获取角色的菜单列表 + * @param roleIdList + * @return + */ + List selectRoleMenuByRoleIdList(@Param("roleIdList") List roleIdList); } diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/ISysRoleService.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/ISysRoleService.java index 3dedf5e..c10eceb 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/ISysRoleService.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/ISysRoleService.java @@ -3,6 +3,7 @@ package com.hxhq.system.service; import java.util.List; import java.util.Set; import com.hxhq.system.api.domain.SysRole; +import com.hxhq.system.domain.SysRoleMenu; import com.hxhq.system.domain.SysUserRole; import com.hxhq.system.form.RoleSaveForm; @@ -179,4 +180,11 @@ public interface ISysRoleService * @return 结果 */ public int insertAuthUsers(Long roleId, Long[] userIds); + + /** + * 获取角色的菜单列表 + * @param roleList + * @return + */ + List selectRoleMenuByRoleIdList(List roleIdList); } diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java index 5a10773..05d639a 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/system/service/impl/SysRoleServiceImpl.java @@ -565,4 +565,9 @@ public class SysRoleServiceImpl implements ISysRoleService log.setQmrMcEn(SecurityUtils.getUsername()); return log; } + + @Override + public List selectRoleMenuByRoleIdList(List roleIdList) { + return roleMapper.selectRoleMenuByRoleIdList(roleIdList); + } } diff --git a/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysRoleMapper.xml b/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysRoleMapper.xml index 3903068..937dc0b 100644 --- a/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysRoleMapper.xml +++ b/hxhq-modules/hxhq-system/src/main/resources/mapper/system/SysRoleMapper.xml @@ -156,5 +156,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{roleId} + + \ No newline at end of file