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')")
|
@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) {
|
public AjaxResult getLeader(@PathVariable Long collegeId, @RequestParam Long userId) {
|
||||||
if (userHasRole("ADMIN") || userHasRole("COLLEGE_ADMIN")) {
|
if (userHasRole("ADMIN") || userHasRole("COLLEGE_ADMIN")) {
|
||||||
return AjaxResult.success(collegeLeaderService.getLeaderByCollegeId(collegeId));
|
return AjaxResult.success(collegeLeaderService.getLeaderByCollegeId(collegeId));
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,13 @@ public class CollegeLeader {
|
||||||
/**
|
/**
|
||||||
* 院系ID
|
* 院系ID
|
||||||
*/
|
*/
|
||||||
@TableId
|
@TableId(value = "college_id", type = IdType.INPUT)
|
||||||
private Long collegeId;
|
private Long collegeId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 负责人ID
|
* 负责人ID
|
||||||
*/
|
*/
|
||||||
@TableId
|
@TableField
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,13 @@ public class UserRole {
|
||||||
/**
|
/**
|
||||||
* 用户ID
|
* 用户ID
|
||||||
*/
|
*/
|
||||||
@TableId
|
@TableId(value = "user_id", type = IdType.INPUT)
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色ID
|
* 角色ID
|
||||||
*/
|
*/
|
||||||
@TableId
|
@TableField
|
||||||
private Long roleId;
|
private Long roleId;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
||||||
|
|
||||||
import com.bruce.sams.domain.sms.Club;
|
import com.bruce.sams.domain.sms.Club;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bruce
|
* @author bruce
|
||||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
* @createDate 2025-02-19 20:18:22
|
* @createDate 2025-02-19 20:18:22
|
||||||
* @Entity com.bruce.sams.domain.sms.Club
|
* @Entity com.bruce.sams.domain.sms.Club
|
||||||
*/
|
*/
|
||||||
|
@Mapper
|
||||||
public interface ClubMapper extends BaseMapper<Club> {
|
public interface ClubMapper extends BaseMapper<Club> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
||||||
|
|
||||||
import com.bruce.sams.domain.sms.ClubUser;
|
import com.bruce.sams.domain.sms.ClubUser;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bruce
|
* @author bruce
|
||||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
* @createDate 2025-02-19 22:11:10
|
* @createDate 2025-02-19 22:11:10
|
||||||
* @Entity com.bruce.sams.domain.sms.ClubUser
|
* @Entity com.bruce.sams.domain.sms.ClubUser
|
||||||
*/
|
*/
|
||||||
|
@Mapper
|
||||||
public interface ClubUserMapper extends BaseMapper<ClubUser> {
|
public interface ClubUserMapper extends BaseMapper<ClubUser> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
||||||
|
|
||||||
import com.bruce.sams.domain.sms.CollegeLeader;
|
import com.bruce.sams.domain.sms.CollegeLeader;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bruce
|
* @author bruce
|
||||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
* @createDate 2025-02-19 20:18:55
|
* @createDate 2025-02-19 20:18:55
|
||||||
* @Entity com.bruce.sams.domain.sms.CollegeLeader
|
* @Entity com.bruce.sams.domain.sms.CollegeLeader
|
||||||
*/
|
*/
|
||||||
|
@Mapper
|
||||||
public interface CollegeLeaderMapper extends BaseMapper<CollegeLeader> {
|
public interface CollegeLeaderMapper extends BaseMapper<CollegeLeader> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.bruce.sams.mapper;
|
||||||
|
|
||||||
import com.bruce.sams.domain.sms.College;
|
import com.bruce.sams.domain.sms.College;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bruce
|
* @author bruce
|
||||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
* @createDate 2025-02-19 20:18:40
|
* @createDate 2025-02-19 20:18:40
|
||||||
* @Entity com.bruce.sams.domain.sms.College
|
* @Entity com.bruce.sams.domain.sms.College
|
||||||
*/
|
*/
|
||||||
|
@Mapper
|
||||||
public interface CollegeMapper extends BaseMapper<College> {
|
public interface CollegeMapper extends BaseMapper<College> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@ package com.bruce.sams.mapper;
|
||||||
import com.bruce.sams.domain.sys.Role;
|
import com.bruce.sams.domain.sys.Role;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bruce
|
* @author bruce
|
||||||
* @description 针对表【sys_role(用户角色表)】的数据库操作Mapper
|
* @description 针对表【sys_role(用户角色表)】的数据库操作Mapper
|
||||||
|
|
@ -16,6 +19,14 @@ public interface RoleMapper extends BaseMapper<Role> {
|
||||||
@Select("SELECT * FROM sys_role WHERE role_id = #{roleId}")
|
@Select("SELECT * FROM sys_role WHERE role_id = #{roleId}")
|
||||||
Role findByRoleId(Long 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
|
@Mapper
|
||||||
public interface UserRoleMapper extends BaseMapper<UserRole> {
|
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;
|
package com.bruce.sams.service;
|
||||||
|
|
||||||
|
import com.bruce.sams.domain.sys.Role;
|
||||||
import com.bruce.sams.domain.sys.User;
|
import com.bruce.sams.domain.sys.User;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -74,4 +75,11 @@ public interface UserService {
|
||||||
*/
|
*/
|
||||||
void deleteUser(Long userId);
|
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.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.bruce.sams.domain.sms.Club;
|
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.service.ClubService;
|
||||||
import com.bruce.sams.mapper.ClubMapper;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -20,6 +27,12 @@ import java.util.List;
|
||||||
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club>
|
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club>
|
||||||
implements ClubService {
|
implements ClubService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加社团(校级或院级管理员)
|
* 添加社团(校级或院级管理员)
|
||||||
*/
|
*/
|
||||||
|
|
@ -60,12 +73,56 @@ public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club>
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Club> listClubs(String keyword, Long userId) {
|
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<>();
|
LambdaQueryWrapper<Club> query = new LambdaQueryWrapper<>();
|
||||||
if (keyword != null && !keyword.isEmpty()) {
|
if (keyword != null && !keyword.isEmpty()) {
|
||||||
query.like(Club::getClubName, keyword);
|
query.like(Club::getClubName, keyword);
|
||||||
}
|
}
|
||||||
return this.list(query);
|
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;
|
package com.bruce.sams.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.bruce.sams.domain.sys.Role;
|
||||||
import com.bruce.sams.domain.sys.User;
|
import com.bruce.sams.domain.sys.User;
|
||||||
import com.bruce.sams.common.exception.PasswordIncorrectException;
|
import com.bruce.sams.common.exception.PasswordIncorrectException;
|
||||||
import com.bruce.sams.common.exception.UserNotFoundException;
|
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.UserMapper;
|
||||||
|
import com.bruce.sams.mapper.UserRoleMapper;
|
||||||
import com.bruce.sams.service.UserService;
|
import com.bruce.sams.service.UserService;
|
||||||
import com.bruce.sams.common.utils.PasswordUtil;
|
import com.bruce.sams.common.utils.PasswordUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||||
|
|
@ -20,6 +26,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
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) {
|
public void deleteUser(Long userId) {
|
||||||
userMapper.deleteById(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;
|
package com.bruce.sams;
|
||||||
|
|
||||||
import com.bruce.sams.common.utils.PasswordUtil;
|
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.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class SamsApplicationTests {
|
class SamsApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRoleMapper userRoleMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
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