Files
yjearth/src/main/java/com/yj/earth/business/controller/RoleController.java
2025-11-03 14:51:13 +08:00

144 lines
5.6 KiB
Java

package com.yj.earth.business.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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;
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
@RestController
@RequestMapping("/role")
public class RoleController {
@Resource
private RoleService roleService;
@Resource
private UserService userService;
@Operation(summary = "新增角色")
@PostMapping("/add")
public ApiResponse save(@RequestBody AddRoleDto addRoleDto) {
Role role = new Role();
BeanUtils.copyProperties(addRoleDto, role);
roleService.save(role);
return ApiResponse.success(null);
}
@Operation(summary = "删除角色")
@PostMapping("/deletes")
public ApiResponse deletes(@Parameter(description = "用户ID列表") @RequestBody List<String> ids) {
roleService.removeByIds(ids);
return ApiResponse.success(null);
}
@Operation(summary = "更新角色")
@PostMapping("/update")
public ApiResponse update(@RequestBody UpdateRoleDto updateRoleDto) {
Role role = new Role();
BeanUtils.copyProperties(updateRoleDto, role);
roleService.updateById(role);
return ApiResponse.success(null);
}
@Operation(summary = "角色详情")
@GetMapping("/getById")
public ApiResponse get(@Parameter(description = "角色ID") String id) {
Role role = roleService.getById(id);
return ApiResponse.success(role);
}
@Operation(summary = "角色列表")
@GetMapping("/list")
public ApiResponse list(@Parameter(description = "分页数量") Integer pageNum, @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);
// 转换为 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 = "管理员数量查询")
@GetMapping("/count")
public ApiResponse count() {
LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Role::getIsSuper, 1);
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);
}
}