mitao
2025-03-06 01d6fa48a0de7a21988e89f71721b6b85e53b517
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysRoleController.java
@@ -1,39 +1,11 @@
package com.ruoyi.system.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
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.common.core.web.page.PageInfo;
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.PageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
@@ -42,6 +14,43 @@
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.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.RoleQuery;
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.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * 角色信息
@@ -72,30 +81,32 @@
    @Resource
    private SysMenuMapper menuMapper;
    @ApiOperation("获取所有角色信息根据公司id")
    // @ApiOperation("获取所有角色信息根据公司id")
    @GetMapping("/list")
    public AjaxResult list(Integer companyId)
    {
        SysRole role = new SysRole();
        role.setCompanyId(companyId);
        List<SysRole> list = roleService.selectRoleList(role);
        return AjaxResult.success(list);
    }
    @ApiOperation("获取所有角色信息根据公司id--分页")
    @GetMapping("/listPage")
    public AjaxResult listPage(Integer companyId,int pageNumber,int pageSize)
    @ApiOperation("获取角色列表")
    @PostMapping("/listPage")
    public AjaxResult listPage(@Validated @RequestBody RoleQuery query)
    {
        PageInfo<SysRole> pageInfo = new PageInfo<>(pageNumber, pageSize);
        PageInfo<SysRole> pageInfo = new PageInfo<>(query.getPageCurr(), query.getPageSize());
        PageInfo<SysRole> page = roleService.page(pageInfo, new LambdaQueryWrapper<SysRole>().eq(SysRole::getCompanyId, companyId).ne(SysRole::getRoleId,20));
        PageInfo<SysRole> page = roleService.page(pageInfo,
                new LambdaQueryWrapper<SysRole>().ne(SysRole::getRoleId, 20)
                        .like(StringUtils.isNotBlank(query.getRoleName()), SysRole::getRoleName,
                                query.getRoleName()).eq(SysRole::getDelFlag, "0")
                        .orderByDesc(SysRole::getCreateTime));
        return AjaxResult.success(page);
    }
    @ApiOperation("角色启用停用")
    // @ApiOperation("角色启用停用")
    @GetMapping("/roleStart")
    public AjaxResult roleStart(Long roleId)
    {
@@ -109,7 +120,7 @@
    }
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @ApiOperation("添加角色")
    @PostMapping("/roleAdd")
    public AjaxResult roleAdd(@Validated @RequestBody RoleAddDto dto)
@@ -117,8 +128,7 @@
        SysRole role = new SysRole();
        role.setRoleName(dto.getRoleName());
        long count = roleService.count(Wrappers.lambdaQuery(SysRole.class)
                    .eq(SysRole::getRoleName,dto.getRoleName())
                    .eq(SysRole::getCompanyId,dto.getCompanyId()));
                .eq(SysRole::getRoleName, dto.getRoleName()).eq(SysRole::getDelFlag, "0"));
        if(count>0){
            return AjaxResult.error("角色已存在,请重新输入");
        }
@@ -126,12 +136,8 @@
        if(CollectionUtils.isEmpty(menuIds1)){
            return AjaxResult.error("菜单id不能为空");
        }
        role.setMenuIds(dto.getMenuIds().toArray((new Long[dto.getMenuIds().size()])));
        role.setMenuIds(dto.getMenuIds().toArray(new Long[0]));
        // 添加角色
        role.setCompanyId(dto.getCompanyId());
        role.setCarDataAuth(dto.getCarDataAuth());
        role.setCarTrainOperAuth(dto.getCarTrainOperAuth());
        role.setContractDataAuth(dto.getContractDataAuth());
        role.setCreateBy(SecurityUtils.getUsername());
        role.setCreateTime(new Date());
        roleService.insertRole(role);
@@ -149,15 +155,13 @@
    @ApiOperation("角色详情")
    @GetMapping("/roleInfo")
    public AjaxResult roleInfo( @RequestParam Long id)
    @GetMapping("/roleInfo/{id}")
    public AjaxResult roleInfo(
            @ApiParam(value = "角色id", name = "id", required = true) @PathVariable("id") Long id)
    {
        SysRole role = roleService.selectRoleById(id);
        RoleInfoVo roleInfoVo = new RoleInfoVo();
        roleInfoVo.setRoleId(role.getRoleId());
        roleInfoVo.setCarDataAuth(role.getCarDataAuth());
        roleInfoVo.setCarTrainOperAuth(role.getCarTrainOperAuth());
        roleInfoVo.setContractDataAuth(role.getContractDataAuth());
        roleInfoVo.setRoleName(role.getRoleName());
        // 获取当前角色的菜单id
        List<Long> menusId = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
@@ -193,17 +197,29 @@
    public AjaxResult roleInfoFromUserId( @RequestParam Long userId)
    {
        SysUserRole one = sysUserRoleService.getOne(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
        if (Objects.isNull(one)) {
            return AjaxResult.success();
        }
        Long id =one.getRoleId();
        // 获取当前角色的菜单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<>());
        }
        if(id!=1){
            menusId = menusId.stream().filter(e->e!=1074 && e!=1075 && e!=1193 && e!=1194).collect(Collectors.toList());
        }
        //获取当前的权限菜单
        List<SysMenus> all = menuMapper.getAllInIds(menusId);
        List<SysMenus> allUser = menuMapper.getAllInIds(menusId);
        // 查询所有权限菜单
        List<SysMenus> all = menuMapper.getAll();
        // 创建一个Map来存储all集合中的menuId
        Map<Long, Boolean> menuIdExistMap = allUser.stream()
                .collect(Collectors.toMap(SysMenus::getMenuId, aMenu -> true,
                        (oldValue, newValue) -> oldValue));
        // 遍历allUser集合,根据menuIdExistMap来设置isHave值
        for (SysMenus allMenu : all) {
            allMenu.setIsHave(menuIdExistMap.containsKey(allMenu.getMenuId()) ? 1 : 2);
        }
        // 第三级
        List<SysMenus> s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList());
        // 第二级
@@ -224,21 +240,21 @@
        return AjaxResult.success(s1);
    }
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @ApiOperation("编辑角色")
    @PostMapping("/roleUpdate")
    public AjaxResult roleUpdate(@Validated @RequestBody RoleUpdateDto dto)
    {
        SysRole role = new SysRole();
        role.setRoleName(dto.getRoleName());
        SysRole one = roleService.getOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleId, dto.getRoleId()));
        List<SysRole> sysRoles = roleService.isExitUpdate(dto.getRoleName(),dto.getRoleId(),one.getCompanyId());
        SysRole one = roleService.getOne(
                new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleId, dto.getRoleId())
                        .eq(SysRole::getDelFlag, "0"));
        List<SysRole> sysRoles = roleService.isExitUpdate(dto.getRoleName(), dto.getRoleId());
        if(sysRoles.size()>0){
            return AjaxResult.error("角色已存在,请重新输入");
        }
        // 编辑角色
        role.setCarDataAuth(dto.getCarDataAuth());
        role.setCarTrainOperAuth(dto.getCarTrainOperAuth());
        role.setContractDataAuth(dto.getContractDataAuth());
        role.setUpdateBy(SecurityUtils.getUsername());
        role.setUpdateTime(new Date());
        role.setRoleId(dto.getRoleId());
@@ -246,20 +262,8 @@
        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)
            );
        }
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, dto.getRoleId()));
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
@@ -270,6 +274,14 @@
        return AjaxResult.success();
    }
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @ApiOperation("删除角色")
    @DeleteMapping("/del/{id}")
    public AjaxResult removeRole(
            @ApiParam(name = "id", value = "角色ID", required = true) @PathVariable Long id) {
        roleService.removeRole(id);
        return AjaxResult.success();
    }
    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:role:export")
    @PostMapping("/export")