This commit is contained in:
2025-10-24 14:53:19 +08:00
parent 6b9aef1acd
commit 7eb63e3e5d
12 changed files with 184 additions and 251 deletions

View File

@ -5,10 +5,14 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.earth.annotation.CheckAuth;
import com.yj.earth.business.domain.Role;
import com.yj.earth.business.domain.User;
import com.yj.earth.business.service.RoleService;
import com.yj.earth.business.service.UserService;
import com.yj.earth.common.util.ApiResponse;
import com.yj.earth.dto.role.AddRoleDto;
import com.yj.earth.dto.role.SetUserDto;
import com.yj.earth.dto.role.UpdateRoleDto;
import com.yj.earth.vo.RoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -16,7 +20,10 @@ import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Tag(name = "角色数据管理")
@CheckAuth
@ -25,6 +32,8 @@ import java.util.List;
public class RoleController {
@Resource
private RoleService roleService;
@Resource
private UserService userService;
@Operation(summary = "新增角色")
@PostMapping("/add")
@ -64,18 +73,33 @@ public class RoleController {
@Parameter(description = "分页大小") Integer pageSize,
@Parameter(description = "角色名称") String roleName,
@Parameter(description = "角色状态") Integer status) {
// 构建查询条件
LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
// 根据角色名称进行模糊搜索
if (StringUtils.isNotBlank(roleName)) {
queryWrapper.like(Role::getRoleName, roleName);
}
// 角色状态
if (status != null) {
queryWrapper.eq(Role::getStatus, status);
}
// 统计当前条件下每个角色名称的数量
List<Role> allMatchedRoles = roleService.list(queryWrapper);
Map<String, Long> roleNameCountMap = allMatchedRoles.stream()
.collect(Collectors.groupingBy(Role::getRoleName, Collectors.counting()));
// 分页查询
Page<Role> rolePage = roleService.page(new Page<>(pageNum, pageSize), queryWrapper);
return ApiResponse.success(rolePage);
// 转换为 Page<RoleVo>
Page<RoleVo> roleVoPage = (Page<RoleVo>) rolePage.convert(role -> {
RoleVo roleVo = new RoleVo();
BeanUtils.copyProperties(role, roleVo);
// 设置数量
roleVo.setCount(roleNameCountMap.getOrDefault(role.getRoleName(), 0L).intValue());
return roleVo;
});
return ApiResponse.success(roleVoPage);
}
@Operation(summary = "管理员数量查询")
@ -86,4 +110,37 @@ public class RoleController {
return ApiResponse.success(roleService.count(queryWrapper));
}
@Operation(summary = "角色数量统计")
@GetMapping("/countByRole")
public ApiResponse countByRole() {
// 返回每一个角色的名称的数量
return ApiResponse.success(roleService.list().stream().collect(Collectors.groupingBy(Role::getRoleName, Collectors.counting())));
}
@Operation(summary = "为角色设置用户")
@PostMapping("/setUser")
public ApiResponse setUser(@RequestBody SetUserDto setUserDto) {
setUserDto.getUserIds().forEach(userId -> {
User user = new User();
user.setId(userId);
user.setRoleId(setUserDto.getRoleId());
userService.updateById(user);
});
return ApiResponse.success(null);
}
@Operation(summary = "根据角色ID返回用户信息")
@GetMapping("/getUsersByRoleId")
public ApiResponse getUsersByRoleId(@Parameter(description = "角色ID") String roleId) {
// 查询角色ID为参数的用户列表
LambdaQueryWrapper<User> queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(User::getRoleId, roleId);
// 查询角色ID不为参数的用户列表
LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.ne(User::getRoleId, roleId);
Map<String, List<User>> result = new HashMap<>();
result.put("users", userService.list(queryWrapper1));
result.put("otherUsers", userService.list(queryWrapper2));
return ApiResponse.success(result);
}
}

View File

@ -3,6 +3,7 @@ package com.yj.earth.business.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.yj.earth.annotation.CheckAuth;
import com.yj.earth.business.domain.Source;
@ -26,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import static com.yj.earth.common.constant.GlobalConstant.DIRECTORY;
import static com.yj.earth.common.constant.GlobalConstant.SHOW;
@ -127,8 +129,27 @@ public class SourceController {
@Operation(summary = "获取资源列表")
@GetMapping("/list")
public ApiResponse list() {
return ApiResponse.success(sourceService.getSourceListByUserId(StpUtil.getLoginIdAsString()));
public ApiResponse list(@Parameter(description = "资源类型") String sourceType,
@Parameter(description = "资源名称") String name,
@Parameter(description = "分页数量") Integer pageNum,
@Parameter(description = "分页大小") Integer pageSize) {
// 获取当前登录用户的ID
String userId = StpUtil.getLoginIdAsString();
List<Source> sourceList = sourceService.getSourceListByUserId(userId, sourceType, name, pageNum, pageSize);
return ApiResponse.success(sourceList);
}
@Operation(summary = "获取资源类型列表")
@GetMapping("/typeList")
public ApiResponse typeList() {
return ApiResponse.success(sourceService.list().stream().map(Source::getSourceType).distinct().toList());
}
@Operation(summary = "获取类型及其总数")
@GetMapping("/typeAndCount")
public ApiResponse typeAndCount() {
List<Source> list = sourceService.list();
return ApiResponse.success(list.stream().collect(Collectors.groupingBy(Source::getSourceType, Collectors.counting())));
}
@Operation(summary = "拖动资源")

View File

@ -9,7 +9,6 @@ import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
@ -18,8 +17,6 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@ -132,7 +129,6 @@ public class SystemController {
}
/**
* 返回指定状态码与错误信息
*/

View File

@ -154,9 +154,21 @@ public class UserController {
@Operation(summary = "删除用户")
@PostMapping("/deletes")
@RoleAccess(roleNames = "管理员")
public ApiResponse deletes(@Parameter(description = "用户ID列表") @RequestBody List<String> ids) {
userService.removeByIds(ids);
return ApiResponse.success(null);
}
@Operation(summary = "设置新密码")
@PostMapping("/setNewPassword")
@RoleAccess(roleNames = "管理员")
public ApiResponse setNewPassword(@RequestBody SetNewPasswordDto setNewPasswordDto) {
User user = userService.getById(setNewPasswordDto.getId());
if (user == null) {
return ApiResponse.failure("用户不存在");
}
user.setPassword(BCrypt.hashpw(setNewPasswordDto.getNewPassword(), BCrypt.gensalt()));
userService.updateById(user);
return ApiResponse.success(null);
}
}

View File

@ -1,5 +1,6 @@
package com.yj.earth.business.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yj.earth.business.domain.Source;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yj.earth.common.util.ApiResponse;
@ -20,7 +21,7 @@ public interface SourceService extends IService<Source> {
String fetchPakDetail(String sourceId);
List<Source> getSourceListByUserId(String userId);
List<Source> getSourceListByUserId(String userId, String type,String name,Integer pageNum, Integer pageSize);
String checkIsPass(String parentId, String sourceName);

View File

@ -3,6 +3,9 @@ package com.yj.earth.business.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yj.earth.business.domain.Role;
import com.yj.earth.business.domain.RoleSource;
@ -23,6 +26,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -169,30 +173,59 @@ public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> impleme
return HttpUtil.doGet(url);
}
/**
* 获取用户资源列表
*/
@Override
public List<Source> getSourceListByUserId(String userId) {
public List<Source> getSourceListByUserId(String userId, String sourceType, String sourceName, Integer pageNum, Integer pageSize) {
// 查询该用户信息
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getId, userId);
User user = userService.getOne(queryWrapper);
if (user == null) {
return Collections.emptyList();
}
// 查询角色信息
String roleId = user.getRoleId();
LambdaQueryWrapper<Role> roleQueryWrapper = new LambdaQueryWrapper<>();
roleQueryWrapper.eq(Role::getId, roleId);
// 如果这个角色是管理员则直接返回所有资源
if (roleService.getOne(roleQueryWrapper).getIsSuper() == 1) {
return sourceService.list();
Role role = roleService.getOne(roleQueryWrapper);
if (role == null) {
return Collections.emptyList();
}
// 构建资源查询条件
LambdaQueryWrapper<Source> sourceQueryWrapper = new LambdaQueryWrapper<>();
if (sourceType != null) {
sourceQueryWrapper.eq(Source::getSourceType, sourceType);
}
if (StringUtils.isNotBlank(sourceName)) {
sourceQueryWrapper.like(Source::getSourceName, sourceName);
}
// 增加资源ID的范围限制
if (role.getIsSuper() != 1) {
LambdaQueryWrapper<RoleSource> roleSourceQueryWrapper = new LambdaQueryWrapper<>();
roleSourceQueryWrapper.eq(RoleSource::getRoleId, roleId);
List<RoleSource> roleSourceList = roleSourceService.list(roleSourceQueryWrapper);
List<String> sourceIdList = roleSourceList.stream()
.map(RoleSource::getSourceId)
.toList();
if (sourceIdList.isEmpty()) {
return Collections.emptyList();
}
sourceQueryWrapper.in(Source::getId, sourceIdList);
}
// 判断是否需要分页
if (pageNum != null && pageSize != null) {
// 执行分页查询,返回分页结果中的记录列表
Page<Source> page = new Page<>(pageNum, pageSize);
Page<Source> resultPage = sourceService.page(page, sourceQueryWrapper);
return resultPage.getRecords();
} else {
return sourceService.list(sourceQueryWrapper);
}
// 查询属于该角色的资源列表
LambdaQueryWrapper<RoleSource> roleSourceQueryWrapper = new LambdaQueryWrapper<>();
roleSourceQueryWrapper.eq(RoleSource::getRoleId, roleId);
List<RoleSource> roleSourceList = roleSourceService.list(roleSourceQueryWrapper);
// 从结果提取出资源ID列表
List<String> sourceIdList = roleSourceList.stream().map(RoleSource::getSourceId).toList();
return sourceService.listByIds(sourceIdList);
}
}