| | |
| | | package com.ruoyi.system.controller; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.chargingPile.api.feignClient.SiteClient; |
| | | import com.ruoyi.chargingPile.api.model.Site; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.web.controller.BaseController; |
| | | import com.ruoyi.common.core.web.domain.AjaxResult; |
| | | import com.ruoyi.common.core.web.page.BasePage; |
| | | import com.ruoyi.common.core.web.page.PageInfo; |
| | | import com.ruoyi.common.core.web.page.TableDataInfo; |
| | | import com.ruoyi.common.log.annotation.Log; |
| | | import com.ruoyi.common.log.enums.BusinessType; |
| | | import com.ruoyi.common.security.utils.SecurityUtils; |
| | | import com.ruoyi.other.api.domain.TRoleSite; |
| | | import com.ruoyi.other.api.feignClient.RoleSiteClient; |
| | | import com.ruoyi.system.api.domain.SysDept; |
| | | import com.ruoyi.system.api.domain.SysRole; |
| | | import com.ruoyi.system.api.domain.SysUser; |
| | | import com.ruoyi.system.api.model.GetSysRoleByIds; |
| | | import com.ruoyi.system.domain.SysMenus; |
| | | import com.ruoyi.system.domain.SysRoleMenu; |
| | | import com.ruoyi.system.domain.SysUserRole; |
| | | import com.ruoyi.system.domain.dto.RoleAddDto; |
| | | import com.ruoyi.system.domain.dto.RoleUpdateDto; |
| | | import com.ruoyi.system.domain.vo.RoleInfoVo; |
| | | import com.ruoyi.system.mapper.SysMenuMapper; |
| | | import com.ruoyi.system.mapper.SysRoleMenuMapper; |
| | | import com.ruoyi.system.service.ISysDeptService; |
| | | import com.ruoyi.system.service.ISysRoleService; |
| | | import com.ruoyi.system.service.ISysUserRoleService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import io.seata.spring.annotation.GlobalTransactional; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | | * 前端控制器 |
| | | * </p> |
| | | * 角色信息 |
| | | * |
| | | * @author luodangjia |
| | | * @since 2024-11-21 |
| | | * @author ruoyi |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/sys-role") |
| | | public class SysRoleController { |
| | | |
| | | @RequestMapping("/role") |
| | | @Api(tags = "角色模块") |
| | | public class SysRoleController extends BaseController { |
| | | @Resource |
| | | private ISysRoleService roleService; |
| | | |
| | | @Resource |
| | | private ISysUserService userService; |
| | | |
| | | @Resource |
| | | private ISysDeptService deptService; |
| | | |
| | | @Resource |
| | | private SysRoleMenuMapper sysRoleMenuMapper; |
| | | |
| | | @Resource |
| | | private ISysUserRoleService sysUserRoleService; |
| | | |
| | | @Resource |
| | | private SysMenuMapper menuMapper; |
| | | |
| | | @Resource |
| | | private RoleSiteClient roleSiteClient; |
| | | |
| | | @Resource |
| | | private SiteClient siteClient; |
| | | |
| | | |
| | | @ApiOperation(value = "获取角色列表", tags = {"管理后台-系统用户管理"}) |
| | | @GetMapping("/list") |
| | | public AjaxResult list() { |
| | | List<SysRole> list = roleService.list(new LambdaQueryWrapper<SysRole>().eq(SysRole::getDelFlag, 0).eq(SysRole::getStatus, 0)); |
| | | return AjaxResult.success(list); |
| | | } |
| | | |
| | | |
| | | @ResponseBody |
| | | @GetMapping("/listPage") |
| | | @ApiOperation(value = "获取角色列表", tags = {"管理后台-角色管理"}) |
| | | public AjaxResult listPage(String name, BasePage basePage) { |
| | | PageInfo<SysRole> pageInfo = new PageInfo<>(basePage.getPageCurr(), basePage.getPageSize()); |
| | | LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<SysRole>().eq(SysRole::getStatus, 0).eq(SysRole::getDelFlag, 0); |
| | | if (StringUtils.isNotEmpty(name)) { |
| | | wrapper.like(SysRole::getRoleName, name); |
| | | } |
| | | |
| | | PageInfo<SysRole> page = roleService.page(pageInfo, wrapper.orderByDesc(SysRole::getCreateTime)); |
| | | for (SysRole record : page.getRecords()) { |
| | | List<Integer> data = roleSiteClient.getSiteIds(record.getRoleId()).getData(); |
| | | List<Site> sites = siteClient.getSiteByIds(data).getData(); |
| | | if (null != sites) { |
| | | List<String> collect = sites.stream().map(Site::getName).collect(Collectors.toList()); |
| | | record.setSiteNames(collect); |
| | | } |
| | | long count = sysUserRoleService.count(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, record.getRoleId())); |
| | | record.setNumber(count); |
| | | } |
| | | return AjaxResult.success(page); |
| | | } |
| | | |
| | | |
| | | @ResponseBody |
| | | @PostMapping("/roleAdd") |
| | | @ApiOperation(value = "添加角色", tags = {"管理后台-角色管理"}) |
| | | @GlobalTransactional(rollbackFor = Exception.class)//分布式事务 |
| | | public AjaxResult roleAdd(@Validated @RequestBody RoleAddDto dto) { |
| | | SysRole role = new SysRole(); |
| | | role.setRoleName(dto.getRoleName()); |
| | | long count = roleService.count(Wrappers.lambdaQuery(SysRole.class) |
| | | .eq(SysRole::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); |
| | | //添加站点权限 |
| | | List<TRoleSite> roleSites = new ArrayList<>(); |
| | | for (Integer siteId : dto.getSiteIds()) { |
| | | TRoleSite roleSite = new TRoleSite(); |
| | | roleSite.setRoleId(role.getRoleId().intValue()); |
| | | roleSite.setSiteId(siteId); |
| | | roleSites.add(roleSite); |
| | | } |
| | | roleSiteClient.addRoleSite(roleSites); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | @GetMapping("/roleInfo") |
| | | @ApiOperation(value = "角色详情", tags = {"管理后台-角色管理"}) |
| | | public AjaxResult roleInfo(@RequestParam Long id) { |
| | | SysRole role = roleService.selectRoleById(id); |
| | | RoleInfoVo roleInfoVo = new RoleInfoVo(); |
| | | roleInfoVo.setRoleId(role.getRoleId()); |
| | | 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()); |
| | | List<Integer> siteIds = roleSiteClient.getSiteIds(role.getRoleId()).getData(); |
| | | if (null != siteIds && siteIds.size() > 0) { |
| | | List<Site> sites = siteClient.getSiteByIds(siteIds).getData(); |
| | | List<String> siteNames = sites.stream().map(Site::getName).collect(Collectors.toList()); |
| | | roleInfoVo.setSiteNames(siteNames); |
| | | roleInfoVo.setSiteIds(siteIds); |
| | | } |
| | | return AjaxResult.success(roleInfoVo); |
| | | } |
| | | |
| | | |
| | | @PostMapping("/roleUpdate") |
| | | @ApiOperation(value = "编辑角色", tags = {"管理后台-角色管理"}) |
| | | @GlobalTransactional(rollbackFor = Exception.class)//分布式事务 |
| | | public AjaxResult roleUpdate(@Validated @RequestBody RoleUpdateDto dto) { |
| | | SysRole role = new SysRole(); |
| | | role.setRoleName(dto.getRoleName()); |
| | | SysRole one = roleService.getOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, dto.getRoleName())); |
| | | if (null != one && !one.getRoleId().equals(dto.getRoleId())) { |
| | | return AjaxResult.error("角色已存在,请重新输入"); |
| | | } |
| | | role.setRemark(dto.getRemark()); |
| | | role.setUpdateBy(SecurityUtils.getUsername()); |
| | | role.setUpdateTime(new Date()); |
| | | role.setRoleId(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.getRoleId()); |
| | | sysRoleMenus.add(sysRoleMenu); |
| | | } |
| | | sysRoleMenuMapper.batchRoleMenu(sysRoleMenus); |
| | | //删除旧站点数据 |
| | | roleSiteClient.delRoleSite(dto.getRoleId()); |
| | | //添加站点权限 |
| | | List<TRoleSite> roleSites = new ArrayList<>(); |
| | | if(null != dto.getSiteIds()){ |
| | | for (Integer siteId : dto.getSiteIds()) { |
| | | TRoleSite roleSite = new TRoleSite(); |
| | | roleSite.setRoleId(role.getRoleId().intValue()); |
| | | roleSite.setSiteId(siteId); |
| | | roleSites.add(roleSite); |
| | | } |
| | | } |
| | | roleSiteClient.addRoleSite(roleSites); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 删除角色 |
| | | */ |
| | | @Log(title = "角色管理", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{roleIds}") |
| | | @ApiOperation(value = "删除角色", tags = {"管理后台-角色管理"}) |
| | | public AjaxResult remove(@PathVariable Long[] roleIds) { |
| | | return toAjax(roleService.deleteRoleByIds(roleIds)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 查询已分配用户角色列表 |
| | | */ |
| | | @GetMapping("/authUser/allocatedList") |
| | | public TableDataInfo allocatedList(SysUser user) { |
| | | startPage(); |
| | | List<SysUser> list = userService.selectAllocatedList(user); |
| | | return getDataTable(list); |
| | | } |
| | | |
| | | /** |
| | | * 查询未分配用户角色列表 |
| | | */ |
| | | @GetMapping("/authUser/unallocatedList") |
| | | public TableDataInfo unallocatedList(SysUser user) { |
| | | startPage(); |
| | | List<SysUser> list = userService.selectUnallocatedList(user); |
| | | return getDataTable(list); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取对应角色部门树列表 |
| | | */ |
| | | @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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据id集合获取数据 |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @ResponseBody |
| | | @PostMapping(value = "/getSysRoleByIds") |
| | | public R<List<SysRole>> getSysRoleByIds(@RequestBody GetSysRoleByIds ids) { |
| | | List<SysRole> sysRoleByIds = roleService.getSysRoleByIds(ids.getIds()); |
| | | return R.ok(sysRoleByIds); |
| | | } |
| | | } |
| | | |