v0.2.4 代码优化 单元测试
parent
1b90321cda
commit
befecab00c
|
|
@ -111,9 +111,10 @@ public class CollegeController {
|
|||
|
||||
/**
|
||||
* 校级 & 院级管理员:获取学院负责人
|
||||
* todo 负责人和学院重新处理
|
||||
*/
|
||||
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN') or hasRole('DEPARTMENT_ADMIN')")
|
||||
@GetMapping("/{collegeId}")
|
||||
@GetMapping("/getleader/{collegeId}")
|
||||
public AjaxResult getLeader(@PathVariable Long collegeId, @RequestParam Long userId) {
|
||||
if (userHasRole("ADMIN") || userHasRole("COLLEGE_ADMIN")) {
|
||||
return AjaxResult.success(collegeLeaderService.getLeaderByCollegeId(collegeId));
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@ public class CollegeLeader {
|
|||
/**
|
||||
* 院系ID
|
||||
*/
|
||||
@TableId
|
||||
@TableId(value = "college_id", type = IdType.INPUT)
|
||||
private Long collegeId;
|
||||
|
||||
/**
|
||||
* 负责人ID
|
||||
*/
|
||||
@TableId
|
||||
@TableField
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ public class UserRole {
|
|||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
@TableId
|
||||
@TableId(value = "user_id", type = IdType.INPUT)
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 角色ID
|
||||
*/
|
||||
@TableId
|
||||
@TableField
|
||||
private Long roleId;
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
|||
|
||||
import com.bruce.sams.domain.sms.Club;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2025-02-19 20:18:22
|
||||
* @Entity com.bruce.sams.domain.sms.Club
|
||||
*/
|
||||
@Mapper
|
||||
public interface ClubMapper extends BaseMapper<Club> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
|||
|
||||
import com.bruce.sams.domain.sms.ClubUser;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2025-02-19 22:11:10
|
||||
* @Entity com.bruce.sams.domain.sms.ClubUser
|
||||
*/
|
||||
@Mapper
|
||||
public interface ClubUserMapper extends BaseMapper<ClubUser> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
|||
|
||||
import com.bruce.sams.domain.sms.CollegeLeader;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2025-02-19 20:18:55
|
||||
* @Entity com.bruce.sams.domain.sms.CollegeLeader
|
||||
*/
|
||||
@Mapper
|
||||
public interface CollegeLeaderMapper extends BaseMapper<CollegeLeader> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
|||
|
||||
import com.bruce.sams.domain.sms.College;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2025-02-19 20:18:40
|
||||
* @Entity com.bruce.sams.domain.sms.College
|
||||
*/
|
||||
@Mapper
|
||||
public interface CollegeMapper extends BaseMapper<College> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@ package com.bruce.sams.mapper;
|
|||
import com.bruce.sams.domain.sys.Role;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
* @description 针对表【sys_role(用户角色表)】的数据库操作Mapper
|
||||
|
|
@ -16,6 +19,14 @@ public interface RoleMapper extends BaseMapper<Role> {
|
|||
@Select("SELECT * FROM sys_role WHERE role_id = #{roleId}")
|
||||
Role findByRoleId(Long roleId);
|
||||
|
||||
@Select("<script>" +
|
||||
"SELECT * FROM sys_role WHERE role_id IN " +
|
||||
"<foreach item='roleId' collection='roleIds' open='(' separator=',' close=')'>" +
|
||||
"#{roleId}" +
|
||||
"</foreach>" +
|
||||
"</script>")
|
||||
List<Role> selectRolesByIds(@Param("roleIds") List<Long> roleIds);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ import java.util.List;
|
|||
@Mapper
|
||||
public interface UserRoleMapper extends BaseMapper<UserRole> {
|
||||
|
||||
@Select("SELECT role_id from sys_user_role where user_id =#{userId}")
|
||||
public List<Long> selectUserRoleByUserId(@Param("userId") Long userId);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
package com.bruce.sams.service;
|
||||
|
||||
import com.bruce.sams.domain.sys.UserRole;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
* @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Service
|
||||
* @createDate 2025-02-19 18:45:11
|
||||
*/
|
||||
public interface UserRoleService extends IService<UserRole> {
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.bruce.sams.service;
|
||||
|
||||
import com.bruce.sams.domain.sys.Role;
|
||||
import com.bruce.sams.domain.sys.User;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -74,4 +75,11 @@ public interface UserService {
|
|||
*/
|
||||
void deleteUser(Long userId);
|
||||
|
||||
/**
|
||||
* 根据用户获取角色
|
||||
* @param userId 用户ID
|
||||
* @return
|
||||
*/
|
||||
List<Role> getRolesByUserId(Long userId);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,17 @@ package com.bruce.sams.service.impl;
|
|||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.sams.domain.sms.Club;
|
||||
import com.bruce.sams.domain.sys.Role;
|
||||
import com.bruce.sams.domain.sys.User;
|
||||
import com.bruce.sams.mapper.UserMapper;
|
||||
import com.bruce.sams.mapper.UserRoleMapper;
|
||||
import com.bruce.sams.service.ClubService;
|
||||
import com.bruce.sams.mapper.ClubMapper;
|
||||
import com.bruce.sams.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -20,6 +27,12 @@ import java.util.List;
|
|||
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club>
|
||||
implements ClubService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 添加社团(校级或院级管理员)
|
||||
*/
|
||||
|
|
@ -60,12 +73,56 @@ public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club>
|
|||
*/
|
||||
@Override
|
||||
public List<Club> listClubs(String keyword, Long userId) {
|
||||
// 获取当前用户信息
|
||||
User user = userMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
|
||||
// 获取用户角色
|
||||
List<String> roles = userService.getRolesByUserId(userId)
|
||||
.stream().map(Role::getRoleKey).toList();
|
||||
|
||||
// 根据不同角色调用不同的方法
|
||||
if (roles.contains("COLLEGE_ADMIN")) {
|
||||
return listAllClubs(keyword);
|
||||
} else if (roles.contains("DEPARTMENT_ADMIN")) {
|
||||
return listDepartmentClubs(keyword, user.getCollegeId());
|
||||
} else if (roles.contains("CLUB_ADMIN")) {
|
||||
return listManagedClubs(keyword, userId);
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
// 校级管理员(COLLEGE_ADMIN):查询所有社团
|
||||
private List<Club> listAllClubs(String keyword) {
|
||||
LambdaQueryWrapper<Club> query = new LambdaQueryWrapper<>();
|
||||
if (keyword != null && !keyword.isEmpty()) {
|
||||
query.like(Club::getClubName, keyword);
|
||||
}
|
||||
return this.list(query);
|
||||
}
|
||||
|
||||
// 院级管理员(DEPARTMENT_ADMIN):查询本院系的社团
|
||||
private List<Club> listDepartmentClubs(String keyword, Long collegeId) {
|
||||
LambdaQueryWrapper<Club> query = new LambdaQueryWrapper<>();
|
||||
if (keyword != null && !keyword.isEmpty()) {
|
||||
query.like(Club::getClubName, keyword);
|
||||
}
|
||||
query.eq(Club::getCollegeId, collegeId); // 仅查询所属学院的社团
|
||||
return this.list(query);
|
||||
}
|
||||
|
||||
// 社团管理员(CLUB_ADMIN):查询自己管理的社团
|
||||
private List<Club> listManagedClubs(String keyword, Long userId) {
|
||||
LambdaQueryWrapper<Club> query = new LambdaQueryWrapper<>();
|
||||
if (keyword != null && !keyword.isEmpty()) {
|
||||
query.like(Club::getClubName, keyword);
|
||||
}
|
||||
query.eq(Club::getLeaderId, userId); // 仅查询自己负责的社团
|
||||
return this.list(query);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
package com.bruce.sams.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.sams.domain.sys.UserRole;
|
||||
import com.bruce.sams.service.UserRoleService;
|
||||
import com.bruce.sams.mapper.UserRoleMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
* @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Service实现
|
||||
* @createDate 2025-02-19 18:45:11
|
||||
*/
|
||||
@Service
|
||||
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole>
|
||||
implements UserRoleService{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,17 +1,23 @@
|
|||
package com.bruce.sams.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.sams.domain.sys.Role;
|
||||
import com.bruce.sams.domain.sys.User;
|
||||
import com.bruce.sams.common.exception.PasswordIncorrectException;
|
||||
import com.bruce.sams.common.exception.UserNotFoundException;
|
||||
import com.bruce.sams.domain.sys.UserRole;
|
||||
import com.bruce.sams.mapper.RoleMapper;
|
||||
import com.bruce.sams.mapper.UserMapper;
|
||||
import com.bruce.sams.mapper.UserRoleMapper;
|
||||
import com.bruce.sams.service.UserService;
|
||||
import com.bruce.sams.common.utils.PasswordUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
|
|
@ -20,6 +26,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private UserRoleMapper userRoleMapper;
|
||||
|
||||
@Autowired
|
||||
private RoleMapper roleMapper;
|
||||
|
||||
/**
|
||||
* 批量导入用户(管理端)
|
||||
*
|
||||
|
|
@ -166,4 +178,23 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|||
public void deleteUser(Long userId) {
|
||||
userMapper.deleteById(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户获取角色
|
||||
* @param userId 用户ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Role> getRolesByUserId(Long userId) {
|
||||
// 1. 查询 user_role 表,获取 role_id 列表
|
||||
List<Long> roleIds = userRoleMapper.selectUserRoleByUserId(userId);
|
||||
|
||||
// 2. 如果用户没有角色,返回空列表
|
||||
if (roleIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
// 3. 根据 role_id 查询 sys_role 表
|
||||
return roleMapper.selectRolesByIds(roleIds);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,31 @@
|
|||
package com.bruce.sams;
|
||||
|
||||
import com.bruce.sams.common.utils.PasswordUtil;
|
||||
import com.bruce.sams.domain.sys.Role;
|
||||
import com.bruce.sams.mapper.UserRoleMapper;
|
||||
import com.bruce.sams.service.UserService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
class SamsApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserRoleMapper userRoleMapper;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
System.out.println(PasswordUtil.encode("password123"));
|
||||
System.out.println(userRoleMapper.selectUserRoleByUserId(3L));
|
||||
|
||||
List<String> roles = userService.getRolesByUserId(3L).stream().
|
||||
map(Role::getRoleKey).toList();
|
||||
System.out.println(roles);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue