mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-02 00:16:11 +08:00
commit
92de93aae2
@ -0,0 +1,85 @@
|
|||||||
|
package top.suyiiyii.sims.controller;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import top.suyiiyii.sims.common.AuthAccess;
|
||||||
|
import top.suyiiyii.sims.common.Result;
|
||||||
|
import top.suyiiyii.sims.dto.UserDto;
|
||||||
|
import top.suyiiyii.sims.entity.User;
|
||||||
|
import top.suyiiyii.sims.exception.ServiceException;
|
||||||
|
import top.suyiiyii.sims.service.HierarchyService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/hierarchy")
|
||||||
|
public class HierarchyController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HierarchyService hierarchyService;
|
||||||
|
|
||||||
|
@GetMapping("/{userId}/SuperiorUser")
|
||||||
|
@Operation(summary = "获取用户的上级(管理员)")
|
||||||
|
@AuthAccess(allowRoles = {"admin"})
|
||||||
|
public UserDto getSuperiorUser(@PathVariable int userId) {
|
||||||
|
User user = hierarchyService.getSuperiorUser(userId);
|
||||||
|
if (user == null) {
|
||||||
|
throw new ServiceException("该用户没有上级");
|
||||||
|
}
|
||||||
|
return new UserDto(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{userId}/subordinateUser")
|
||||||
|
@Operation(summary = "获取用户的下级(管理员)")
|
||||||
|
@AuthAccess(allowRoles = {"admin"})
|
||||||
|
public List<UserDto> getSubordinateUser(@PathVariable int userId) {
|
||||||
|
List<User> users = hierarchyService.getSubordinateUser(userId);
|
||||||
|
if (users == null || users.isEmpty()) {
|
||||||
|
throw new ServiceException("该用户没有下级");
|
||||||
|
}
|
||||||
|
return users.stream().map(UserDto::new).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{userId}/SuperiorUser")
|
||||||
|
@Operation(summary = "设置用户的上级(管理员)")
|
||||||
|
@AuthAccess(allowRoles = {"admin"})
|
||||||
|
|
||||||
|
public Result setSuperiorUser(@PathVariable int userId, @RequestParam int superiorUserId) {
|
||||||
|
hierarchyService.setSuperiorUser(userId, superiorUserId);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{userId}/SuperiorUser")
|
||||||
|
@Operation(summary = "删除用户的上级(管理员)")
|
||||||
|
@AuthAccess(allowRoles = {"admin"})
|
||||||
|
public Result deleteSuperiorUser(@PathVariable int userId) {
|
||||||
|
hierarchyService.setSuperiorUser(userId, 0);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/self/superiorUser")
|
||||||
|
@Operation(summary = "获取自己的上级")
|
||||||
|
@AuthAccess(allowRoles = {"user"})
|
||||||
|
public UserDto getSelfSuperiorUser(@RequestAttribute("userId") int userId) {
|
||||||
|
User user = hierarchyService.getSuperiorUser(userId);
|
||||||
|
if (user == null) {
|
||||||
|
throw new ServiceException("您没有上级");
|
||||||
|
}
|
||||||
|
return new UserDto(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/self/subordinateUser")
|
||||||
|
@Operation(summary = "获取自己的下级")
|
||||||
|
@AuthAccess(allowRoles = {"user"})
|
||||||
|
public List<UserDto> getSelfSubordinateUser(@RequestAttribute("userId") int userId) {
|
||||||
|
List<User> users = hierarchyService.getSubordinateUser(userId);
|
||||||
|
if (users == null || users.isEmpty()) {
|
||||||
|
throw new ServiceException("您没有下级");
|
||||||
|
}
|
||||||
|
return users.stream().map(UserDto::new).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
package top.suyiiyii.sims.dto;
|
package top.suyiiyii.sims.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import top.suyiiyii.sims.entity.User;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -13,6 +16,8 @@ import java.util.List;
|
|||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class UserDto {
|
public class UserDto {
|
||||||
private Integer userId;
|
private Integer userId;
|
||||||
private Integer studentId;
|
private Integer studentId;
|
||||||
@ -20,4 +25,12 @@ public class UserDto {
|
|||||||
private String grade;
|
private String grade;
|
||||||
private String userGroup;
|
private String userGroup;
|
||||||
private List<String> roles; // 角色名称列表
|
private List<String> roles; // 角色名称列表
|
||||||
|
|
||||||
|
public UserDto(User user) {
|
||||||
|
this.userId = user.getId();
|
||||||
|
this.studentId = user.getStudentId();
|
||||||
|
this.username = user.getUsername();
|
||||||
|
this.grade = user.getGrade();
|
||||||
|
this.userGroup = user.getUserGroup();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ package top.suyiiyii.sims.entity;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.tangzc.mpe.autotable.annotation.Column;
|
||||||
import com.tangzc.mpe.autotable.annotation.Table;
|
import com.tangzc.mpe.autotable.annotation.Table;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@ -24,9 +26,13 @@ public class HierarchyRelation {
|
|||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
// 上级用户ID
|
// 上级用户ID
|
||||||
|
@NotNull
|
||||||
|
@Column(defaultValue = "0")
|
||||||
private Integer superiorUserId;
|
private Integer superiorUserId;
|
||||||
|
|
||||||
// 下级用户ID
|
// 下级用户ID
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private Integer subordinateUserId;
|
private Integer subordinateUserId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
19
src/main/java/top/suyiiyii/sims/mapper/HierarchyMapper.java
Normal file
19
src/main/java/top/suyiiyii/sims/mapper/HierarchyMapper.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package top.suyiiyii.sims.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
import top.suyiiyii.sims.entity.HierarchyRelation;
|
||||||
|
import top.suyiiyii.sims.entity.User;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface HierarchyMapper extends BaseMapper<HierarchyRelation> {
|
||||||
|
|
||||||
|
@Select("SELECT * FROM user WHERE id = " +
|
||||||
|
"(SELECT hierarchy_relation.superior_user_id FROM hierarchy_relation WHERE subordinate_user_id = #{userId})")
|
||||||
|
User getSuperiorUser(int userId);
|
||||||
|
|
||||||
|
@Select("SELECT * FROM user WHERE id = " +
|
||||||
|
"(SELECT hierarchy_relation.subordinate_user_id FROM hierarchy_relation WHERE superior_user_id = #{userId})")
|
||||||
|
List<User> getSubordinateUser(int userId);
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package top.suyiiyii.sims.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import top.suyiiyii.sims.entity.HierarchyRelation;
|
||||||
|
import top.suyiiyii.sims.entity.User;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface HierarchyService extends IService<HierarchyRelation> {
|
||||||
|
public User getSuperiorUser(int UserId);
|
||||||
|
|
||||||
|
public List<User> getSubordinateUser(int userId);
|
||||||
|
|
||||||
|
|
||||||
|
void setSuperiorUser(int userId, int superiorUserId);
|
||||||
|
}
|
@ -8,13 +8,11 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import top.suyiiyii.sims.controller.UserController;
|
import top.suyiiyii.sims.controller.UserController;
|
||||||
import top.suyiiyii.sims.dto.UserDto;
|
import top.suyiiyii.sims.dto.UserDto;
|
||||||
|
import top.suyiiyii.sims.entity.HierarchyRelation;
|
||||||
import top.suyiiyii.sims.entity.Role;
|
import top.suyiiyii.sims.entity.Role;
|
||||||
import top.suyiiyii.sims.entity.User;
|
import top.suyiiyii.sims.entity.User;
|
||||||
import top.suyiiyii.sims.exception.ServiceException;
|
import top.suyiiyii.sims.exception.ServiceException;
|
||||||
import top.suyiiyii.sims.mapper.MpUserMapper;
|
import top.suyiiyii.sims.mapper.*;
|
||||||
import top.suyiiyii.sims.mapper.PermissionsMapper;
|
|
||||||
import top.suyiiyii.sims.mapper.RoleMapper;
|
|
||||||
import top.suyiiyii.sims.mapper.UserMapper;
|
|
||||||
import top.suyiiyii.sims.utils.JwtUtils;
|
import top.suyiiyii.sims.utils.JwtUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -44,6 +42,8 @@ public class UserService {
|
|||||||
private RbacService rbacService;
|
private RbacService rbacService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ModelMapper modelMapper;
|
private ModelMapper modelMapper;
|
||||||
|
@Autowired
|
||||||
|
private HierarchyMapper hierarchyMapper;
|
||||||
|
|
||||||
public void deleteUser(int id) {
|
public void deleteUser(int id) {
|
||||||
userMapper.deleteUser(id);
|
userMapper.deleteUser(id);
|
||||||
@ -77,6 +77,7 @@ public class UserService {
|
|||||||
mpUserMapper.insert(user);
|
mpUserMapper.insert(user);
|
||||||
user = mpUserMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, req.getUsername()));
|
user = mpUserMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, req.getUsername()));
|
||||||
rbacService.addRoleWithUserId(user.getId(), "user");
|
rbacService.addRoleWithUserId(user.getId(), "user");
|
||||||
|
hierarchyMapper.insert(new HierarchyRelation(null,0, user.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
package top.suyiiyii.sims.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import top.suyiiyii.sims.entity.HierarchyRelation;
|
||||||
|
import top.suyiiyii.sims.entity.User;
|
||||||
|
import top.suyiiyii.sims.exception.ServiceException;
|
||||||
|
import top.suyiiyii.sims.mapper.HierarchyMapper;
|
||||||
|
import top.suyiiyii.sims.mapper.MpUserMapper;
|
||||||
|
import top.suyiiyii.sims.service.HierarchyService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class HierarchyServiceImpl extends ServiceImpl<HierarchyMapper, HierarchyRelation> implements HierarchyService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MpUserMapper userMapper;
|
||||||
|
@Override
|
||||||
|
public User getSuperiorUser(int UserId) {
|
||||||
|
return baseMapper.getSuperiorUser(UserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<User> getSubordinateUser(int userId) {
|
||||||
|
return baseMapper.getSubordinateUser(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSuperiorUser(int userId, int superiorUserId) {
|
||||||
|
if (userId == superiorUserId) {
|
||||||
|
throw new ServiceException("不能设置自己为上级");
|
||||||
|
}
|
||||||
|
if (superiorUserId != 0 && userMapper.selectById(superiorUserId) == null) {
|
||||||
|
throw new ServiceException("上级用户不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测是否存在循环上级
|
||||||
|
User superiorUser = userMapper.selectById(superiorUserId);
|
||||||
|
while (superiorUser != null) {
|
||||||
|
if (superiorUser.getId() == userId) {
|
||||||
|
throw new ServiceException("不能设置自己的下级为上级");
|
||||||
|
}
|
||||||
|
superiorUser = getSuperiorUser(superiorUser.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
baseMapper.update(new LambdaUpdateWrapper<HierarchyRelation>().eq(HierarchyRelation::getSubordinateUserId, userId)
|
||||||
|
.set(HierarchyRelation::getSuperiorUserId, superiorUserId));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user