Commit e4613a7a264c71896cd92f2d7ee99d43fff2b827
1 parent
f5c2dda9
新增角色管理功能,包括角色的新增、编辑、禁用、启用、删除及相关接口
Showing
8 changed files
with
266 additions
and
27 deletions
src/main/java/com/diligrp/rider/controller/PlatformSystemRoleController.java
| ... | ... | @@ -2,7 +2,9 @@ package com.diligrp.rider.controller; |
| 2 | 2 | |
| 3 | 3 | import com.diligrp.rider.common.result.Result; |
| 4 | 4 | import com.diligrp.rider.dto.AdminRoleMenuAssignDTO; |
| 5 | +import com.diligrp.rider.dto.AdminRoleSaveDTO; | |
| 5 | 6 | import com.diligrp.rider.service.SystemRoleMenuService; |
| 7 | +import com.diligrp.rider.service.SystemRoleService; | |
| 6 | 8 | import com.diligrp.rider.vo.AdminRoleMenuTreeVO; |
| 7 | 9 | import com.diligrp.rider.vo.AdminRoleVO; |
| 8 | 10 | import jakarta.validation.Valid; |
| ... | ... | @@ -16,11 +18,42 @@ import java.util.List; |
| 16 | 18 | @RequiredArgsConstructor |
| 17 | 19 | public class PlatformSystemRoleController { |
| 18 | 20 | |
| 21 | + private final SystemRoleService systemRoleService; | |
| 19 | 22 | private final SystemRoleMenuService systemRoleMenuService; |
| 20 | 23 | |
| 21 | 24 | @GetMapping("/list") |
| 22 | - public Result<List<AdminRoleVO>> list() { | |
| 23 | - return Result.success(systemRoleMenuService.listRoles()); | |
| 25 | + public Result<List<AdminRoleVO>> list(@RequestParam(defaultValue = "false") boolean includeDisabled) { | |
| 26 | + return Result.success(systemRoleService.list(includeDisabled)); | |
| 27 | + } | |
| 28 | + | |
| 29 | + @PostMapping("/add") | |
| 30 | + public Result<Void> add(@Valid @RequestBody AdminRoleSaveDTO dto) { | |
| 31 | + systemRoleService.add(dto); | |
| 32 | + return Result.success(); | |
| 33 | + } | |
| 34 | + | |
| 35 | + @PutMapping("/edit") | |
| 36 | + public Result<Void> edit(@Valid @RequestBody AdminRoleSaveDTO dto) { | |
| 37 | + systemRoleService.edit(dto); | |
| 38 | + return Result.success(); | |
| 39 | + } | |
| 40 | + | |
| 41 | + @PostMapping("/ban") | |
| 42 | + public Result<Void> ban(@RequestParam Long id) { | |
| 43 | + systemRoleService.ban(id); | |
| 44 | + return Result.success(); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @PostMapping("/cancelBan") | |
| 48 | + public Result<Void> cancelBan(@RequestParam Long id) { | |
| 49 | + systemRoleService.cancelBan(id); | |
| 50 | + return Result.success(); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @DeleteMapping("/del") | |
| 54 | + public Result<Void> del(@RequestParam Long id) { | |
| 55 | + systemRoleService.del(id); | |
| 56 | + return Result.success(); | |
| 24 | 57 | } |
| 25 | 58 | |
| 26 | 59 | @GetMapping("/{roleId}/menu-tree") | ... | ... |
src/main/java/com/diligrp/rider/dto/AdminRoleSaveDTO.java
0 → 100644
| 1 | +package com.diligrp.rider.dto; | |
| 2 | + | |
| 3 | +import jakarta.validation.constraints.NotBlank; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +@Data | |
| 7 | +public class AdminRoleSaveDTO { | |
| 8 | + private Long id; | |
| 9 | + | |
| 10 | + @NotBlank(message = "角色编码不能为空") | |
| 11 | + private String code; | |
| 12 | + | |
| 13 | + @NotBlank(message = "角色名称不能为空") | |
| 14 | + private String name; | |
| 15 | + | |
| 16 | + @NotBlank(message = "角色范围不能为空") | |
| 17 | + private String roleScope; | |
| 18 | +} | ... | ... |
src/main/java/com/diligrp/rider/service/SystemRoleMenuService.java
| ... | ... | @@ -2,13 +2,9 @@ package com.diligrp.rider.service; |
| 2 | 2 | |
| 3 | 3 | import com.diligrp.rider.dto.AdminRoleMenuAssignDTO; |
| 4 | 4 | import com.diligrp.rider.vo.AdminRoleMenuTreeVO; |
| 5 | -import com.diligrp.rider.vo.AdminRoleVO; | |
| 6 | - | |
| 7 | 5 | import java.util.List; |
| 8 | 6 | |
| 9 | 7 | public interface SystemRoleMenuService { |
| 10 | - List<AdminRoleVO> listRoles(); | |
| 11 | - | |
| 12 | 8 | List<AdminRoleMenuTreeVO> getRoleMenuTree(Long roleId); |
| 13 | 9 | |
| 14 | 10 | void assignMenus(Long roleId, AdminRoleMenuAssignDTO dto); | ... | ... |
src/main/java/com/diligrp/rider/service/SystemRoleService.java
0 → 100644
| 1 | +package com.diligrp.rider.service; | |
| 2 | + | |
| 3 | +import com.diligrp.rider.dto.AdminRoleSaveDTO; | |
| 4 | +import com.diligrp.rider.vo.AdminRoleVO; | |
| 5 | + | |
| 6 | +import java.util.List; | |
| 7 | + | |
| 8 | +public interface SystemRoleService { | |
| 9 | + List<AdminRoleVO> list(boolean includeDisabled); | |
| 10 | + | |
| 11 | + void add(AdminRoleSaveDTO dto); | |
| 12 | + | |
| 13 | + void edit(AdminRoleSaveDTO dto); | |
| 14 | + | |
| 15 | + void ban(Long id); | |
| 16 | + | |
| 17 | + void cancelBan(Long id); | |
| 18 | + | |
| 19 | + void del(Long id); | |
| 20 | +} | ... | ... |
src/main/java/com/diligrp/rider/service/impl/MenuBootstrapServiceImpl.java
| ... | ... | @@ -77,8 +77,9 @@ public class MenuBootstrapServiceImpl implements MenuBootstrapService { |
| 77 | 77 | defaults.add(menu("open.mock_delivery", "模拟推单", "MENU", "/open/mock-delivery", "", 0L, MenuScopeEnum.PLATFORM, 92)); |
| 78 | 78 | defaults.add(menu("system.root", "系统管理", "DIR", "", "ControlOutlined", 0L, MenuScopeEnum.PLATFORM, 100)); |
| 79 | 79 | defaults.add(menu("system.menu", "菜单管理", "MENU", "/system/menu", "", 0L, MenuScopeEnum.PLATFORM, 101)); |
| 80 | - defaults.add(menu("system.role_menu", "角色菜单", "MENU", "/system/role-menu", "", 0L, MenuScopeEnum.PLATFORM, 102)); | |
| 81 | - defaults.add(menu("admin.user", "平台账号", "MENU", "/admin-user", "", 0L, MenuScopeEnum.PLATFORM, 103)); | |
| 80 | + defaults.add(menu("system.role", "角色管理", "MENU", "/system/role", "", 0L, MenuScopeEnum.PLATFORM, 102)); | |
| 81 | + defaults.add(menu("system.role_menu", "角色菜单", "MENU", "/system/role-menu", "", 0L, MenuScopeEnum.PLATFORM, 103)); | |
| 82 | + defaults.add(menu("admin.user", "平台账号", "MENU", "/admin-user", "", 0L, MenuScopeEnum.PLATFORM, 104)); | |
| 82 | 83 | |
| 83 | 84 | Map<String, SysMenu> persisted = new LinkedHashMap<>(); |
| 84 | 85 | for (SysMenu menu : defaults) { |
| ... | ... | @@ -103,8 +104,13 @@ public class MenuBootstrapServiceImpl implements MenuBootstrapService { |
| 103 | 104 | persisted.get("open.app").setParentId(persisted.get("open.root").getId()); |
| 104 | 105 | persisted.get("open.mock_delivery").setParentId(persisted.get("open.root").getId()); |
| 105 | 106 | persisted.get("system.menu").setParentId(persisted.get("system.root").getId()); |
| 107 | + persisted.get("system.menu").setListOrder(101); | |
| 108 | + persisted.get("system.role").setParentId(persisted.get("system.root").getId()); | |
| 109 | + persisted.get("system.role").setListOrder(102); | |
| 106 | 110 | persisted.get("system.role_menu").setParentId(persisted.get("system.root").getId()); |
| 111 | + persisted.get("system.role_menu").setListOrder(103); | |
| 107 | 112 | persisted.get("admin.user").setParentId(persisted.get("system.root").getId()); |
| 113 | + persisted.get("admin.user").setListOrder(104); | |
| 108 | 114 | |
| 109 | 115 | for (SysMenu menu : persisted.values()) { |
| 110 | 116 | sysMenuMapper.updateById(menu); | ... | ... |
src/main/java/com/diligrp/rider/service/impl/SystemRoleMenuServiceImpl.java
| ... | ... | @@ -12,12 +12,10 @@ import com.diligrp.rider.mapper.SysRoleMapper; |
| 12 | 12 | import com.diligrp.rider.mapper.SysRoleMenuMapper; |
| 13 | 13 | import com.diligrp.rider.service.SystemRoleMenuService; |
| 14 | 14 | import com.diligrp.rider.vo.AdminRoleMenuTreeVO; |
| 15 | -import com.diligrp.rider.vo.AdminRoleVO; | |
| 16 | 15 | import lombok.RequiredArgsConstructor; |
| 17 | 16 | import org.springframework.stereotype.Service; |
| 18 | 17 | import org.springframework.transaction.annotation.Transactional; |
| 19 | 18 | |
| 20 | -import java.util.ArrayList; | |
| 21 | 19 | import java.util.LinkedHashMap; |
| 22 | 20 | import java.util.List; |
| 23 | 21 | import java.util.Map; |
| ... | ... | @@ -33,23 +31,6 @@ public class SystemRoleMenuServiceImpl implements SystemRoleMenuService { |
| 33 | 31 | private final SystemMenuServiceImpl systemMenuService; |
| 34 | 32 | |
| 35 | 33 | @Override |
| 36 | - public List<AdminRoleVO> listRoles() { | |
| 37 | - List<SysRole> roles = sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>() | |
| 38 | - .eq(SysRole::getStatus, 1) | |
| 39 | - .orderByAsc(SysRole::getId)); | |
| 40 | - List<AdminRoleVO> result = new ArrayList<>(); | |
| 41 | - for (SysRole role : roles) { | |
| 42 | - AdminRoleVO vo = new AdminRoleVO(); | |
| 43 | - vo.setId(role.getId()); | |
| 44 | - vo.setCode(role.getCode()); | |
| 45 | - vo.setName(role.getName()); | |
| 46 | - vo.setRoleScope(role.getRoleScope()); | |
| 47 | - result.add(vo); | |
| 48 | - } | |
| 49 | - return result; | |
| 50 | - } | |
| 51 | - | |
| 52 | - @Override | |
| 53 | 34 | public List<AdminRoleMenuTreeVO> getRoleMenuTree(Long roleId) { |
| 54 | 35 | SysRole role = requireRole(roleId); |
| 55 | 36 | List<SysMenu> allowedMenus = filterMenusByScope(systemMenuService.listAllMenus(), role); | ... | ... |
src/main/java/com/diligrp/rider/service/impl/SystemRoleServiceImpl.java
0 → 100644
| 1 | +package com.diligrp.rider.service.impl; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 4 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | |
| 5 | +import com.diligrp.rider.common.enums.AdminRoleScopeEnum; | |
| 6 | +import com.diligrp.rider.common.exception.BizException; | |
| 7 | +import com.diligrp.rider.dto.AdminRoleSaveDTO; | |
| 8 | +import com.diligrp.rider.entity.AdminUser; | |
| 9 | +import com.diligrp.rider.entity.Substation; | |
| 10 | +import com.diligrp.rider.entity.SysRole; | |
| 11 | +import com.diligrp.rider.entity.SysRoleMenu; | |
| 12 | +import com.diligrp.rider.mapper.AdminUserMapper; | |
| 13 | +import com.diligrp.rider.mapper.SubstationMapper; | |
| 14 | +import com.diligrp.rider.mapper.SysRoleMapper; | |
| 15 | +import com.diligrp.rider.mapper.SysRoleMenuMapper; | |
| 16 | +import com.diligrp.rider.service.SystemRoleService; | |
| 17 | +import com.diligrp.rider.vo.AdminRoleVO; | |
| 18 | +import lombok.RequiredArgsConstructor; | |
| 19 | +import org.springframework.stereotype.Service; | |
| 20 | +import org.springframework.transaction.annotation.Transactional; | |
| 21 | + | |
| 22 | +import java.util.ArrayList; | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.Set; | |
| 25 | + | |
| 26 | +@Service | |
| 27 | +@RequiredArgsConstructor | |
| 28 | +public class SystemRoleServiceImpl implements SystemRoleService { | |
| 29 | + | |
| 30 | + private static final Set<String> BUILT_IN_CODES = Set.of("platform_admin", "substation_admin"); | |
| 31 | + | |
| 32 | + private final SysRoleMapper sysRoleMapper; | |
| 33 | + private final SysRoleMenuMapper sysRoleMenuMapper; | |
| 34 | + private final AdminUserMapper adminUserMapper; | |
| 35 | + private final SubstationMapper substationMapper; | |
| 36 | + | |
| 37 | + @Override | |
| 38 | + public List<AdminRoleVO> list(boolean includeDisabled) { | |
| 39 | + List<SysRole> roles = sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>() | |
| 40 | + .eq(!includeDisabled, SysRole::getStatus, 1) | |
| 41 | + .orderByAsc(SysRole::getId)); | |
| 42 | + List<AdminRoleVO> result = new ArrayList<>(); | |
| 43 | + for (SysRole role : roles) { | |
| 44 | + result.add(toVO(role)); | |
| 45 | + } | |
| 46 | + return result; | |
| 47 | + } | |
| 48 | + | |
| 49 | + @Override | |
| 50 | + public void add(AdminRoleSaveDTO dto) { | |
| 51 | + validateScope(dto.getRoleScope()); | |
| 52 | + ensureUniqueCode(dto.getCode(), null); | |
| 53 | + | |
| 54 | + SysRole role = new SysRole(); | |
| 55 | + role.setCode(dto.getCode().trim()); | |
| 56 | + role.setName(dto.getName().trim()); | |
| 57 | + role.setRoleScope(dto.getRoleScope()); | |
| 58 | + role.setStatus(1); | |
| 59 | + role.setCreateTime(System.currentTimeMillis() / 1000); | |
| 60 | + sysRoleMapper.insert(role); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public void edit(AdminRoleSaveDTO dto) { | |
| 65 | + if (dto.getId() == null || dto.getId() < 1) { | |
| 66 | + throw new BizException("角色ID不能为空"); | |
| 67 | + } | |
| 68 | + SysRole role = requireRole(dto.getId()); | |
| 69 | + if (isBuiltIn(role)) { | |
| 70 | + throw new BizException("内置角色不允许编辑"); | |
| 71 | + } | |
| 72 | + validateScope(dto.getRoleScope()); | |
| 73 | + if (!role.getRoleScope().equals(dto.getRoleScope())) { | |
| 74 | + throw new BizException("角色范围不允许修改"); | |
| 75 | + } | |
| 76 | + ensureUniqueCode(dto.getCode(), role.getId()); | |
| 77 | + role.setCode(dto.getCode().trim()); | |
| 78 | + role.setName(dto.getName().trim()); | |
| 79 | + sysRoleMapper.updateById(role); | |
| 80 | + } | |
| 81 | + | |
| 82 | + @Override | |
| 83 | + public void ban(Long id) { | |
| 84 | + SysRole role = requireRole(id); | |
| 85 | + if (isBuiltIn(role)) { | |
| 86 | + throw new BizException("内置角色不允许禁用"); | |
| 87 | + } | |
| 88 | + ensureNotBound(role.getId(), "当前角色已绑定账号,不能禁用"); | |
| 89 | + sysRoleMapper.update(null, new LambdaUpdateWrapper<SysRole>() | |
| 90 | + .eq(SysRole::getId, id) | |
| 91 | + .set(SysRole::getStatus, 0)); | |
| 92 | + } | |
| 93 | + | |
| 94 | + @Override | |
| 95 | + public void cancelBan(Long id) { | |
| 96 | + requireRole(id); | |
| 97 | + sysRoleMapper.update(null, new LambdaUpdateWrapper<SysRole>() | |
| 98 | + .eq(SysRole::getId, id) | |
| 99 | + .set(SysRole::getStatus, 1)); | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Override | |
| 103 | + @Transactional | |
| 104 | + public void del(Long id) { | |
| 105 | + SysRole role = requireRole(id); | |
| 106 | + if (isBuiltIn(role)) { | |
| 107 | + throw new BizException("内置角色不允许删除"); | |
| 108 | + } | |
| 109 | + ensureNotBound(role.getId(), "当前角色已绑定账号,不能删除"); | |
| 110 | + sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>() | |
| 111 | + .eq(SysRoleMenu::getRoleId, role.getId())); | |
| 112 | + sysRoleMapper.deleteById(role.getId()); | |
| 113 | + } | |
| 114 | + | |
| 115 | + private SysRole requireRole(Long id) { | |
| 116 | + if (id == null || id < 1) { | |
| 117 | + throw new BizException("角色ID不能为空"); | |
| 118 | + } | |
| 119 | + SysRole role = sysRoleMapper.selectById(id); | |
| 120 | + if (role == null) { | |
| 121 | + throw new BizException("角色不存在"); | |
| 122 | + } | |
| 123 | + return role; | |
| 124 | + } | |
| 125 | + | |
| 126 | + private void validateScope(String scope) { | |
| 127 | + for (AdminRoleScopeEnum value : AdminRoleScopeEnum.values()) { | |
| 128 | + if (value.name().equals(scope)) { | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + } | |
| 132 | + throw new BizException("角色范围不合法"); | |
| 133 | + } | |
| 134 | + | |
| 135 | + private void ensureUniqueCode(String code, Long excludeId) { | |
| 136 | + SysRole duplicate = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>() | |
| 137 | + .eq(SysRole::getCode, code.trim()) | |
| 138 | + .ne(excludeId != null, SysRole::getId, excludeId) | |
| 139 | + .last("LIMIT 1")); | |
| 140 | + if (duplicate != null) { | |
| 141 | + throw new BizException("角色编码已存在"); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + private void ensureNotBound(Long roleId, String message) { | |
| 146 | + if (countAdminUsers(roleId) > 0 || countSubstations(roleId) > 0) { | |
| 147 | + throw new BizException(message); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + private AdminRoleVO toVO(SysRole role) { | |
| 152 | + AdminRoleVO vo = new AdminRoleVO(); | |
| 153 | + vo.setId(role.getId()); | |
| 154 | + vo.setCode(role.getCode()); | |
| 155 | + vo.setName(role.getName()); | |
| 156 | + vo.setRoleScope(role.getRoleScope()); | |
| 157 | + vo.setStatus(role.getStatus()); | |
| 158 | + vo.setBuiltIn(isBuiltIn(role)); | |
| 159 | + vo.setAdminUserCount(countAdminUsers(role.getId())); | |
| 160 | + vo.setSubstationCount(countSubstations(role.getId())); | |
| 161 | + vo.setCreateTime(role.getCreateTime()); | |
| 162 | + return vo; | |
| 163 | + } | |
| 164 | + | |
| 165 | + private boolean isBuiltIn(SysRole role) { | |
| 166 | + return BUILT_IN_CODES.contains(role.getCode()); | |
| 167 | + } | |
| 168 | + | |
| 169 | + private long countAdminUsers(Long roleId) { | |
| 170 | + Long count = adminUserMapper.selectCount(new LambdaQueryWrapper<AdminUser>() | |
| 171 | + .eq(AdminUser::getRoleId, roleId)); | |
| 172 | + return count == null ? 0L : count; | |
| 173 | + } | |
| 174 | + | |
| 175 | + private long countSubstations(Long roleId) { | |
| 176 | + Long count = substationMapper.selectCount(new LambdaQueryWrapper<Substation>() | |
| 177 | + .eq(Substation::getRoleId, roleId)); | |
| 178 | + return count == null ? 0L : count; | |
| 179 | + } | |
| 180 | +} | ... | ... |
src/main/java/com/diligrp/rider/vo/AdminRoleVO.java
| ... | ... | @@ -8,4 +8,9 @@ public class AdminRoleVO { |
| 8 | 8 | private String code; |
| 9 | 9 | private String name; |
| 10 | 10 | private String roleScope; |
| 11 | + private Integer status; | |
| 12 | + private Boolean builtIn; | |
| 13 | + private Long adminUserCount; | |
| 14 | + private Long substationCount; | |
| 15 | + private Long createTime; | |
| 11 | 16 | } | ... | ... |