| | |
| | | package com.xiaopeng.account.controller; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.xiaopeng.common.core.utils.StringUtils; |
| | | import com.xiaopeng.common.core.web.page.BasePage; |
| | | import com.xiaopeng.account.model.dto.SysRoleAddDTO; |
| | | import com.xiaopeng.account.model.dto.SysRoleEditDTO; |
| | | import com.xiaopeng.account.model.query.SysRolePageQuery; |
| | | import com.xiaopeng.account.model.vo.SysRoleDetailVO; |
| | | import com.xiaopeng.account.model.vo.SysRolePageVO; |
| | | import com.xiaopeng.account.service.ISysRoleService; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.web.page.PageInfo; |
| | | import com.xiaopeng.common.security.annotation.Logical; |
| | | import com.xiaopeng.account.mapper.SysRoleMenuMapper; |
| | | import com.xiaopeng.account.service.ISysDeptService; |
| | | import com.xiaopeng.account.service.ISysRoleBakService; |
| | | import com.xiaopeng.account.service.ISysUserRoleService; |
| | | import com.xiaopeng.account.service.ISysUserService; |
| | | import com.xiaopeng.account.vo.RoleAddDto; |
| | | import com.xiaopeng.account.vo.RoleInfoVo; |
| | | import com.xiaopeng.account.vo.RoleUpdateDto;import com.xiaopeng.api.model.account.*; |
| | | import com.xiaopeng.api.model.operation.SysDept; |
| | | import com.xiaopeng.api.model.operation.SysMenus;import io.seata.spring.annotation.GlobalTransactional; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.util.CollectionUtils; |
| | | import io.swagger.annotations.ApiParam; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import com.xiaopeng.common.core.web.controller.BaseController; |
| | | import com.xiaopeng.common.core.web.domain.AjaxResult; |
| | | import com.xiaopeng.common.core.web.page.TableDataInfo; |
| | | import com.xiaopeng.common.log.annotation.Log; |
| | | import com.xiaopeng.common.log.enums.BusinessType; |
| | | import com.xiaopeng.common.security.annotation.RequiresPermissions; |
| | | import com.xiaopeng.common.security.utils.SecurityUtils; |
| | | |
| | | /** |
| | | * 角色信息 |
| | | * <p> |
| | | * 角色管理 前端控制器 |
| | | * </p> |
| | | * |
| | | * @author ruoyi |
| | | * @author mitao |
| | | * @since 2026-02-03 |
| | | */ |
| | | @Api(tags = "角色管理") |
| | | @RequiredArgsConstructor(onConstructor_ = @Lazy) |
| | | @RestController |
| | | @RequestMapping("/role") |
| | | @Api(tags = "角色模块") |
| | | public class SysRoleController extends BaseController { |
| | | @Resource |
| | | private ISysRoleBakService roleService; |
| | | |
| | | @Resource |
| | | private ISysUserService userService; |
| | | |
| | | @Resource |
| | | private ISysDeptService deptService; |
| | | |
| | | @Resource |
| | | private SysRoleMenuMapper sysRoleMenuMapper; |
| | | |
| | | @Resource |
| | | private ISysUserRoleService sysUserRoleService; |
| | | public class SysRoleController { |
| | | private final ISysRoleService sysRoleService; |
| | | |
| | | |
| | | @ApiOperation("新增角色") |
| | | @PostMapping("/api/v1/role/add") |
| | | public R<?> add(@Validated @RequestBody SysRoleAddDTO dto) { |
| | | sysRoleService.addRole(dto); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | @RequiresPermissions(value = {"/systemUserManagement", "/systemUserManagement/add", "/systemUserManagement/update", "/roleManagement", "/partners", |
| | | "/partners/add", "/partners/seleete", "/partners/update"}, logical = Logical.OR) |
| | | @ApiOperation(value = "获取角色列表", tags = {"管理后台-系统用户管理"}) |
| | | @GetMapping("/list") |
| | | public AjaxResult list() { |
| | | List<SysRoleBak> list = roleService.list(new LambdaQueryWrapper<SysRoleBak>().eq(SysRoleBak::getDelFlag, 0).eq(SysRoleBak::getStatus, 0)); |
| | | return AjaxResult.success(list); |
| | | } |
| | | |
| | | |
| | | @RequiresPermissions(value = {"/roleManagement"}, logical = Logical.OR) |
| | | @ResponseBody |
| | | @GetMapping("/listPage") |
| | | @ApiOperation(value = "获取角色列表", tags = {"管理后台-角色管理"}) |
| | | public AjaxResult listPage(String name, BasePage basePage) { |
| | | PageInfo<SysRoleBak> pageInfo = new PageInfo<>(basePage.getPageCurr(), basePage.getPageSize()); |
| | | LambdaQueryWrapper<SysRoleBak> wrapper = new LambdaQueryWrapper<SysRoleBak>().eq(SysRoleBak::getStatus, 0).eq(SysRoleBak::getDelFlag, 0); |
| | | if (StringUtils.isNotEmpty(name)) { |
| | | wrapper.like(SysRoleBak::getRoleName, name); |
| | | } |
| | | |
| | | PageInfo<SysRoleBak> page = roleService.page(pageInfo, wrapper.orderByDesc(SysRoleBak::getCreateTime)); |
| | | for (SysRoleBak record : page.getRecords()) { |
| | | long count = sysUserRoleService.count(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, record.getId())); |
| | | record.setNumber(count); |
| | | } |
| | | return AjaxResult.success(page); |
| | | } |
| | | |
| | | |
| | | @RequiresPermissions(value = {"/roleManagement/add"}, logical = Logical.OR) |
| | | @ResponseBody |
| | | @PostMapping("/roleAdd") |
| | | @ApiOperation(value = "添加角色", tags = {"管理后台-角色管理"}) |
| | | @GlobalTransactional(rollbackFor = Exception.class)//分布式事务 |
| | | public AjaxResult roleAdd(@Validated @RequestBody RoleAddDto dto) { |
| | | SysRoleBak role = new SysRoleBak(); |
| | | role.setRoleName(dto.getRoleName()); |
| | | long count = roleService.count(Wrappers.lambdaQuery(SysRoleBak.class) |
| | | .eq(SysRoleBak::getRoleName, dto.getRoleName())); |
| | | if (count > 0) { |
| | | return AjaxResult.error("角色已存在,请重新输入"); |
| | | } |
| | | List<Long> menuIds1 = dto.getMenuIds(); |
| | | if (CollectionUtils.isEmpty(menuIds1)) { |
| | | return AjaxResult.error("菜单id不能为空"); |
| | | } |
| | | role.setMenuIds(dto.getMenuIds().toArray((new Long[dto.getMenuIds().size()]))); |
| | | // 添加角色 |
| | | role.setRemark(dto.getRemark()); |
| | | role.setCreateBy(SecurityUtils.getUsername()); |
| | | role.setCreateTime(new Date()); |
| | | roleService.insertRole(role); |
| | | |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | @RequiresPermissions(value = {"/roleManagement/info", "/roleManagement/update"}, logical = Logical.OR) |
| | | @GetMapping("/roleInfo") |
| | | @ApiOperation(value = "角色详情", tags = {"管理后台-角色管理"}) |
| | | public AjaxResult roleInfo(@RequestParam Long id) { |
| | | SysRoleBak role = roleService.selectRoleById(id); |
| | | RoleInfoVo roleInfoVo = new RoleInfoVo(); |
| | | roleInfoVo.setRoleId(role.getId()); |
| | | roleInfoVo.setRoleName(role.getRoleName()); |
| | | // 获取当前角色的菜单id |
| | | List<Long> menusId = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()); |
| | | if (menusId.size() == 0) { |
| | | return AjaxResult.success(new ArrayList<>()); |
| | | } |
| | | //获取当前的权限菜单 |
| | | List<SysMenus> all = menuMapper.getAllInIds(menusId); |
| | | // 第三级 |
| | | List<SysMenus> s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList()); |
| | | // 第二级 |
| | | List<SysMenus> s2 = all.stream().filter(e -> e.getMenuType().equals("C")).collect(Collectors.toList()); |
| | | // 第一级 |
| | | List<SysMenus> s1 = all.stream().filter(e -> e.getMenuType().equals("M")).collect(Collectors.toList()); |
| | | |
| | | for (SysMenus menus : s2) { |
| | | List<SysMenus> collect = s3.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList()); |
| | | menus.setChildren(collect); |
| | | } |
| | | |
| | | for (SysMenus menus : s1) { |
| | | List<SysMenus> collect = s2.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList()); |
| | | menus.setChildren(collect); |
| | | } |
| | | roleInfoVo.setMenus(menusId); |
| | | roleInfoVo.setRemark(role.getRemark()); |
| | | return AjaxResult.success(roleInfoVo); |
| | | } |
| | | |
| | | |
| | | @RequiresPermissions(value = {"/roleManagement/update"}, logical = Logical.OR) |
| | | @PostMapping("/roleUpdate") |
| | | @ApiOperation(value = "编辑角色", tags = {"管理后台-角色管理"}) |
| | | @GlobalTransactional(rollbackFor = Exception.class)//分布式事务 |
| | | public AjaxResult roleUpdate(@Validated @RequestBody RoleUpdateDto dto) { |
| | | SysRoleBak role = new SysRoleBak(); |
| | | role.setRoleName(dto.getRoleName()); |
| | | SysRoleBak one = roleService.getOne(new LambdaQueryWrapper<SysRoleBak>().eq(SysRoleBak::getRoleName, dto.getRoleName())); |
| | | if (null != one && !one.getId().equals(dto.getRoleId())) { |
| | | return AjaxResult.error("角色已存在,请重新输入"); |
| | | } |
| | | role.setRemark(dto.getRemark()); |
| | | role.setUpdateBy(SecurityUtils.getUsername()); |
| | | role.setUpdateTime(new Date()); |
| | | role.setId(dto.getRoleId()); |
| | | roleService.updateRole(role); |
| | | ArrayList<SysRoleMenu> sysRoleMenus = new ArrayList<>(); |
| | | List<Long> menuIds = dto.getMenuIds(); |
| | | // 移除原来的权限菜单 |
| | | if (menuIds.contains(1061L)) { |
| | | sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>() |
| | | .eq(SysRoleMenu::getRoleId, dto.getRoleId())); |
| | | } else { |
| | | sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>() |
| | | .eq(SysRoleMenu::getRoleId, dto.getRoleId()) |
| | | .ne(SysRoleMenu::getMenuId, 1061L) |
| | | .ne(SysRoleMenu::getMenuId, 1062L) |
| | | .ne(SysRoleMenu::getMenuId, 1065L) |
| | | .ne(SysRoleMenu::getMenuId, 1073L) |
| | | .ne(SysRoleMenu::getMenuId, 1161L) |
| | | .ne(SysRoleMenu::getMenuId, 1203L) |
| | | ); |
| | | } |
| | | for (Long menuId : menuIds) { |
| | | SysRoleMenu sysRoleMenu = new SysRoleMenu(); |
| | | sysRoleMenu.setMenuId(menuId); |
| | | sysRoleMenu.setRoleId(role.getId()); |
| | | sysRoleMenus.add(sysRoleMenu); |
| | | } |
| | | sysRoleMenuMapper.batchRoleMenu(sysRoleMenus); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 删除角色 |
| | | */ |
| | | @RequiresPermissions(value = {"/roleManagement/del"}, logical = Logical.OR) |
| | | @Log(title = "角色管理", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{roleIds}") |
| | | @ApiOperation(value = "删除角色", tags = {"管理后台-角色管理"}) |
| | | public AjaxResult remove(@PathVariable Long[] roleIds) { |
| | | return toAjax(roleService.deleteRoleByIds(roleIds)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 查询已分配用户角色列表 |
| | | */ |
| | | @RequiresPermissions("/roleManagement") |
| | | @GetMapping("/authUser/allocatedList") |
| | | public TableDataInfo allocatedList(SysUser user) { |
| | | startPage(); |
| | | List<SysUser> list = userService.selectAllocatedList(user); |
| | | return getDataTable(list); |
| | | } |
| | | |
| | | /** |
| | | * 查询未分配用户角色列表 |
| | | */ |
| | | @RequiresPermissions("/roleManagement") |
| | | @GetMapping("/authUser/unallocatedList") |
| | | public TableDataInfo unallocatedList(SysUser user) { |
| | | startPage(); |
| | | List<SysUser> list = userService.selectUnallocatedList(user); |
| | | return getDataTable(list); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取对应角色部门树列表 |
| | | */ |
| | | @RequiresPermissions("") |
| | | @GetMapping(value = "/deptTree/{roleId}") |
| | | public AjaxResult deptTree(@PathVariable("roleId") Long roleId) { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId)); |
| | | ajax.put("depts", deptService.selectDeptTreeList(new SysDept())); |
| | | return ajax; |
| | | } |
| | | |
| | | @ApiOperation("编辑角色") |
| | | @PostMapping("/api/v1/role/edit") |
| | | public R<?> edit(@Validated @RequestBody SysRoleEditDTO dto) { |
| | | sysRoleService.editRole(dto); |
| | | return R.ok(); |
| | | } |
| | | |
| | | @ApiOperation("角色详情") |
| | | @GetMapping("/api/v1/role/queryById") |
| | | public R<SysRoleDetailVO> queryById(@ApiParam(value = "角色id", required = true) @RequestParam Long id) { |
| | | SysRoleDetailVO detail = sysRoleService.getRoleDetail(id); |
| | | return R.ok(detail); |
| | | } |
| | | |
| | | @ApiOperation("角色分页列表") |
| | | @PostMapping("/api/v1/role/pageList") |
| | | public R<PageInfo<SysRolePageVO>> pageList(@Validated @RequestBody SysRolePageQuery query) { |
| | | PageInfo<SysRolePageVO> pageInfo = sysRoleService.pageList(query); |
| | | return R.ok(pageInfo); |
| | | } |
| | | |
| | | @ApiOperation("删除角色") |
| | | @DeleteMapping("/api/v1/role/delete/{id}") |
| | | public R<?> delete(@ApiParam(value = "角色id", required = true) @PathVariable Long id) { |
| | | sysRoleService.deleteRole(id); |
| | | return R.ok(); |
| | | } |
| | | } |