package com.ruoyi.system.controller; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Resource; import javax.management.relation.Role; import javax.servlet.http.HttpServletResponse; import com.baomidou.mybatisplus.core.conditions.Wrapper; 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.page.BasePage; import com.ruoyi.common.core.web.page.PageInfo; import com.ruoyi.common.security.annotation.Logical; import com.ruoyi.other.api.domain.TRoleSite; import com.ruoyi.other.api.feignClient.RoleSiteClient; 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.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import com.ruoyi.common.core.utils.poi.ExcelUtil; import com.ruoyi.common.core.web.controller.BaseController; import com.ruoyi.common.core.web.domain.AjaxResult; 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.annotation.RequiresPermissions; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.api.domain.SysDept; import com.ruoyi.system.api.domain.SysRole; import com.ruoyi.system.api.domain.SysUser; /** * 角色信息 * * @author ruoyi */ @RestController @RequestMapping("/role") @Api(tags = "角色模块") public class SysRoleController extends BaseController { private static final String INDEPENDENT_PARTNER_ROLE_NAME = "独立合作商"; private static final String INDEPENDENT_PARTNER_ROLE_KEY = "independent_partner"; private static final String SITE_OWNER_ROLE_NAME = "场地方"; private static final String SITE_OWNER_ROLE_KEY = "site_owner"; private static final String SERVICE_PROVIDER_ROLE_NAME = "服务商"; private static final String SERVICE_PROVIDER_ROLE_KEY = "service_provider"; private static final String COMPANY_ROLE_NAME = "公司"; private static final String COMPANY_ROLE_KEY = "company"; private static final String OWN_SITE_NAME = "自有电站"; private static final String SYSTEM_ROLE_REMARK = "系统内置角色"; @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; private void ensureBuiltinRoles() { ensureSystemRole(INDEPENDENT_PARTNER_ROLE_NAME, INDEPENDENT_PARTNER_ROLE_KEY, getIndependentPartnerMenuIds()); ensureSystemRole(SITE_OWNER_ROLE_NAME, SITE_OWNER_ROLE_KEY, new HashSet<>()); ensureSystemRole(SERVICE_PROVIDER_ROLE_NAME, SERVICE_PROVIDER_ROLE_KEY, new HashSet<>()); ensureSystemRole(COMPANY_ROLE_NAME, COMPANY_ROLE_KEY, new HashSet<>()); } private void ensureSystemRole(String roleName, String roleKey, Set menuIds) { SysRole role = roleService.getOne(new LambdaQueryWrapper() .eq(SysRole::getRoleKey, roleKey) .eq(SysRole::getDelFlag, 0) .last("LIMIT 1")); if (role == null) { role = roleService.getOne(new LambdaQueryWrapper() .eq(SysRole::getRoleName, roleName) .eq(SysRole::getDelFlag, 0) .last("LIMIT 1")); } if (role != null) { if (!roleKey.equals(role.getRoleKey()) || !"0".equals(role.getStatus()) || !SYSTEM_ROLE_REMARK.equals(role.getRemark())) { role.setRoleKey(roleKey); role.setStatus("0"); role.setRemark(SYSTEM_ROLE_REMARK); role.setUpdateBy(SecurityUtils.getUsername()); role.setUpdateTime(new Date()); roleService.updateById(role); } return; } SysRole newRole = new SysRole(); newRole.setRoleName(roleName); newRole.setRoleKey(roleKey); newRole.setDataScope("5"); newRole.setStatus("0"); newRole.setDelFlag("0"); newRole.setRemark(SYSTEM_ROLE_REMARK); newRole.setCreateBy(SecurityUtils.getUsername()); newRole.setCreateTime(new Date()); newRole.setMenuIds(menuIds.toArray(new Long[0])); roleService.insertRole(newRole); } private Set getIndependentPartnerMenuIds() { List menus = menuMapper.getAll(); Set menuIds = new HashSet<>(); Set rootIds = new HashSet<>(); for (SysMenus menu : menus) { String path = menu.getPath() == null ? "" : menu.getPath(); String menuName = menu.getMenuName() == null ? "" : menu.getMenuName(); if ("standingPile".equals(path) || "order".equals(path) || menuName.contains("站桩管理") || menuName.contains("订单管理")) { rootIds.add(menu.getMenuId()); } } for (Long rootId : rootIds) { collectMenuTreeIds(rootId, menus, menuIds); } return menuIds; } private void collectMenuTreeIds(Long menuId, List menus, Set menuIds) { if (menuId == null || menuIds.contains(menuId)) { return; } menuIds.add(menuId); for (SysMenus menu : menus) { if (menuId.equals(menu.getParentId())) { collectMenuTreeIds(menu.getMenuId(), menus, menuIds); } } } private boolean isIndependentPartnerRole(SysRole role) { return role != null && (INDEPENDENT_PARTNER_ROLE_KEY.equals(role.getRoleKey()) || INDEPENDENT_PARTNER_ROLE_NAME.equals(role.getRoleName())); } private boolean isPartnerBuiltinRole(SysRole role) { return role != null && (isIndependentPartnerRole(role) || SITE_OWNER_ROLE_KEY.equals(role.getRoleKey()) || SITE_OWNER_ROLE_NAME.equals(role.getRoleName()) || SERVICE_PROVIDER_ROLE_KEY.equals(role.getRoleKey()) || SERVICE_PROVIDER_ROLE_NAME.equals(role.getRoleName())); } private boolean isCompanyBuiltinRole(SysRole role) { return role != null && (COMPANY_ROLE_KEY.equals(role.getRoleKey()) || COMPANY_ROLE_NAME.equals(role.getRoleName())); } private boolean isSystemBuiltinRole(SysRole role) { return role != null && (role.isAdmin() || isPartnerBuiltinRole(role) || isCompanyBuiltinRole(role)); } private void fillRoleV2Info(SysRole role) { if (role == null) { return; } boolean partnerRole = isPartnerBuiltinRole(role); role.setPartnerRole(partnerRole); role.setSystemRole(isSystemBuiltinRole(role)); if (partnerRole) { role.setSiteNames(Arrays.asList(OWN_SITE_NAME)); if (StringUtils.isEmpty(role.getRemark())) { role.setRemark(SYSTEM_ROLE_REMARK); } } } @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 list = roleService.list(new LambdaQueryWrapper().eq(SysRole::getDelFlag, 0).eq(SysRole::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) { ensureBuiltinRoles(); PageInfo pageInfo = new PageInfo<>(basePage.getPageCurr(), basePage.getPageSize()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper().eq(SysRole::getStatus, 0).eq(SysRole::getDelFlag, 0); if (StringUtils.isNotEmpty(name)) { wrapper.like(SysRole::getRoleName, name); } PageInfo page = roleService.page(pageInfo, wrapper.orderByDesc(SysRole::getCreateTime)); for (SysRole record : page.getRecords()) { List data = roleSiteClient.getSiteIds(record.getRoleId()).getData(); List sites = siteClient.getSiteByIds(data).getData(); if (null != sites) { List collect = sites.stream().map(Site::getName).collect(Collectors.toList()); record.setSiteNames(collect); } long count = sysUserRoleService.count(new LambdaQueryWrapper().eq(SysUserRole::getRoleId, record.getRoleId())); record.setNumber(count); fillRoleV2Info(record); } return AjaxResult.success(page); } @RequiresPermissions(value = {"/roleManagement/add"}, logical = Logical.OR) @ResponseBody @PostMapping("/roleAdd") @ApiOperation(value = "添加角色", tags = {"管理后台-角色管理"}) 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 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); //添加站点权限 if(null != dto.getSiteIds()){ List 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(); } @RequiresPermissions(value = {"/roleManagement/info", "/roleManagement/update"}, logical = Logical.OR) @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()); roleInfoVo.setRoleKey(role.getRoleKey()); roleInfoVo.setPartnerRole(isIndependentPartnerRole(role)); roleInfoVo.setSystemRole(isSystemBuiltinRole(role)); // 获取当前角色的菜单id List menusId = sysRoleMenuMapper.selectList(new LambdaQueryWrapper().eq(SysRoleMenu::getRoleId, id)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()); if (menusId.size() == 0) { return AjaxResult.success(new ArrayList<>()); } //获取当前的权限菜单 List all = menuMapper.getAllInIds(menusId); // 第三级 List s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList()); // 第二级 List s2 = all.stream().filter(e -> e.getMenuType().equals("C")).collect(Collectors.toList()); // 第一级 List s1 = all.stream().filter(e -> e.getMenuType().equals("M")).collect(Collectors.toList()); for (SysMenus menus : s2) { List collect = s3.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList()); menus.setChildren(collect); } for (SysMenus menus : s1) { List collect = s2.stream().filter(e -> e.getParentId().equals(menus.getMenuId())).collect(Collectors.toList()); menus.setChildren(collect); } roleInfoVo.setMenus(menusId); roleInfoVo.setRemark(role.getRemark()); if (isIndependentPartnerRole(role)) { roleInfoVo.setSiteNames(Arrays.asList(OWN_SITE_NAME)); roleInfoVo.setSiteIds(new ArrayList<>()); if (StringUtils.isEmpty(roleInfoVo.getRemark())) { roleInfoVo.setRemark(SYSTEM_ROLE_REMARK); } } else { List siteIds = roleSiteClient.getSiteIds(role.getRoleId()).getData(); if (null != siteIds && siteIds.size() > 0) { List sites = siteClient.getSiteByIds(siteIds).getData(); List siteNames = sites.stream().map(Site::getName).collect(Collectors.toList()); roleInfoVo.setSiteNames(siteNames); roleInfoVo.setSiteIds(siteIds); } } return AjaxResult.success(roleInfoVo); } @RequiresPermissions(value = {"/roleManagement/update"}, logical = Logical.OR) @PostMapping("/roleUpdate") @ApiOperation(value = "编辑角色", tags = {"管理后台-角色管理"}) public AjaxResult roleUpdate(@Validated @RequestBody RoleUpdateDto dto) { SysRole oldRole = roleService.selectRoleById(dto.getRoleId()); if (oldRole != null && oldRole.isAdmin()) { return AjaxResult.error("超级管理员角色不可编辑"); } SysRole role = new SysRole(); role.setRoleName(dto.getRoleName()); SysRole one = roleService.getOne(new LambdaQueryWrapper().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 sysRoleMenus = new ArrayList<>(); List menuIds = dto.getMenuIds(); // 移除原来的权限菜单 if (menuIds.contains(1061L)) { sysRoleMenuMapper.delete(new LambdaQueryWrapper() .eq(SysRoleMenu::getRoleId, dto.getRoleId())); } else { sysRoleMenuMapper.delete(new LambdaQueryWrapper() .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 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(); } /** * 删除角色 */ @RequiresPermissions(value = {"/roleManagement/del"}, logical = Logical.OR) @Log(title = "角色管理", businessType = BusinessType.DELETE) @DeleteMapping("/{roleIds}") @ApiOperation(value = "删除角色", tags = {"管理后台-角色管理"}) public AjaxResult remove(@PathVariable Long[] roleIds) { for (Long roleId : roleIds) { SysRole role = roleService.selectRoleById(roleId); if (isSystemBuiltinRole(role)) { return AjaxResult.error("系统内置角色不可删除"); } } return toAjax(roleService.deleteRoleByIds(roleIds)); } /** * 查询已分配用户角色列表 */ @RequiresPermissions("/roleManagement") @GetMapping("/authUser/allocatedList") public TableDataInfo allocatedList(SysUser user) { startPage(); List list = userService.selectAllocatedList(user); return getDataTable(list); } /** * 查询未分配用户角色列表 */ @RequiresPermissions("/roleManagement") @GetMapping("/authUser/unallocatedList") public TableDataInfo unallocatedList(SysUser user) { startPage(); List 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; } /** * 根据id集合获取数据 * * @param ids * @return */ @ResponseBody @PostMapping(value = "/getSysRoleByIds") public R> getSysRoleByIds(@RequestBody GetSysRoleByIds ids) { List sysRoleByIds = roleService.getSysRoleByIds(ids.getIds()); return R.ok(sysRoleByIds); } /** * 根据角色标识查询启用且未删除的角色ID(远程接口,公司账号分配默认角色使用) * * @param roleKey 角色标识 * @return 角色ID;不存在时返回 null */ @ResponseBody @PostMapping(value = "/getRoleIdByRoleKey") public R getRoleIdByRoleKey(@RequestBody String roleKey) { SysRole role = roleService.getOne(new LambdaQueryWrapper() .eq(SysRole::getRoleKey, roleKey) .eq(SysRole::getDelFlag, 0) .eq(SysRole::getStatus, 0) .orderByAsc(SysRole::getRoleId) .last("LIMIT 1")); return R.ok(role == null ? null : role.getRoleId()); } }