v0.1.7 完成高校院系模块后端接口,完成用户与高校之间的数据交互

v0.1
bruce 2025-02-15 13:19:36 +08:00
parent f2a45f123b
commit 0a9e8cd815
24 changed files with 625 additions and 29 deletions

View File

@ -1,4 +1,4 @@
package com.bruce.sams.Constant;
package com.bruce.sams.constant;
/**
*

View File

@ -0,0 +1,74 @@
package com.bruce.sams.controller;
import com.bruce.sams.utils.AjaxResult;
import com.bruce.sams.domain.sms.College;
import com.bruce.sams.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
*/
@RestController
@RequestMapping("/api/admin/college")
public class CollegeController {
@Autowired
private CollegeService collegeService;
/**
*
*/
@GetMapping("/universities")
public AjaxResult getAllUniversities() {
List<College> universities = collegeService.getAllUniversities();
return AjaxResult.success("查询成功", universities);
}
/**
*
*/
@GetMapping("/{universityId}/departments")
public AjaxResult getDepartments(@PathVariable Long universityId) {
List<College> departments = collegeService.getDepartmentsByUniversity(universityId);
return AjaxResult.success("查询成功", departments);
}
/**
*
*/
@GetMapping("/{collegeId}")
public AjaxResult getCollegeById(@PathVariable Long collegeId) {
College college = collegeService.getCollegeById(collegeId);
return college != null ? AjaxResult.success("查询成功", college) : AjaxResult.error("院系不存在");
}
/**
*
*/
@PostMapping("/add")
public AjaxResult addCollege(@RequestBody College college) {
collegeService.addCollege(college);
return AjaxResult.success("添加成功");
}
/**
*
*/
@PutMapping("/update")
public AjaxResult updateCollege(@RequestBody College college) {
collegeService.updateCollege(college);
return AjaxResult.success("更新成功");
}
/**
*
*/
@DeleteMapping("/delete/{collegeId}")
public AjaxResult deleteCollege(@PathVariable Long collegeId) {
collegeService.deleteCollege(collegeId);
return AjaxResult.success("删除成功");
}
}

View File

@ -0,0 +1,47 @@
package com.bruce.sams.controller;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.bruce.sams.service.CollegeLeaderService;
import com.bruce.sams.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* /
*/
@RestController
@RequestMapping("/api/admin/college-leader")
public class CollegeLeaderController {
@Autowired
private CollegeLeaderService collegeLeaderService;
/**
* /
*/
@GetMapping("/{collegeId}")
public AjaxResult getCollegeLeader(@PathVariable Long collegeId) {
CollegeLeader leader = collegeLeaderService.getCollegeLeader(collegeId);
return leader != null ? AjaxResult.success("查询成功", leader) : AjaxResult.error("未找到负责人");
}
/**
*
*/
@PostMapping("/assign")
public AjaxResult assignLeader(@RequestAttribute Long adminUserId,
@RequestParam Long collegeId,
@RequestParam Long userId) {
collegeLeaderService.assignLeader(collegeId, userId, adminUserId);
return AjaxResult.success("指派成功");
}
/**
*
*/
@DeleteMapping("/remove/{collegeId}")
public AjaxResult removeLeader(@RequestAttribute Long adminUserId, @PathVariable Long collegeId) {
collegeLeaderService.removeLeader(collegeId, adminUserId);
return AjaxResult.success("负责人移除成功");
}
}

View File

@ -0,0 +1,88 @@
package com.bruce.sams.domain.sms;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName sms_college
*/
@TableName(value ="sms_college")
@Data
public class College {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Long collegeId;
/**
*
*/
private String collegeName;
/**
* ID
*/
private Long parentId;
/**
*
*/
private String email;
/**
*
*/
private Date createdAt;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
College other = (College) that;
return (this.getCollegeId() == null ? other.getCollegeId() == null : this.getCollegeId().equals(other.getCollegeId()))
&& (this.getCollegeName() == null ? other.getCollegeName() == null : this.getCollegeName().equals(other.getCollegeName()))
&& (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId()))
&& (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCollegeId() == null) ? 0 : getCollegeId().hashCode());
result = prime * result + ((getCollegeName() == null) ? 0 : getCollegeName().hashCode());
result = prime * result + ((getParentId() == null) ? 0 : getParentId().hashCode());
result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", collegeId=").append(collegeId);
sb.append(", collegeName=").append(collegeName);
sb.append(", parentId=").append(parentId);
sb.append(", email=").append(email);
sb.append(", createdAt=").append(createdAt);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,73 @@
package com.bruce.sams.domain.sms;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName sms_college_leader
*/
@TableName(value ="sms_college_leader")
@Data
public class CollegeLeader {
/**
* ID
*/
@TableId
private Long collegeId;
/**
* ID
*/
@TableId
private Long userId;
/**
*
*/
private Date assignedAt;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
CollegeLeader other = (CollegeLeader) that;
return (this.getCollegeId() == null ? other.getCollegeId() == null : this.getCollegeId().equals(other.getCollegeId()))
&& (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
&& (this.getAssignedAt() == null ? other.getAssignedAt() == null : this.getAssignedAt().equals(other.getAssignedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCollegeId() == null) ? 0 : getCollegeId().hashCode());
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getAssignedAt() == null) ? 0 : getAssignedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", collegeId=").append(collegeId);
sb.append(", userId=").append(userId);
sb.append(", assignedAt=").append(assignedAt);
sb.append("]");
return sb.toString();
}
}

View File

@ -1,6 +1,6 @@
package com.bruce.sams.exception;
import com.bruce.sams.Constant.HttpStatus;
import com.bruce.sams.constant.HttpStatus;
/**
*

View File

@ -1,6 +1,6 @@
package com.bruce.sams.exception;
import com.bruce.sams.Constant.HttpStatus;
import com.bruce.sams.constant.HttpStatus;
/**
*

View File

@ -1,6 +1,6 @@
package com.bruce.sams.exception;
import com.bruce.sams.Constant.HttpStatus;
import com.bruce.sams.constant.HttpStatus;
/**
*

View File

@ -0,0 +1,33 @@
package com.bruce.sams.mapper;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* @author bruce
* @description sms_college_leader()Mapper
* @createDate 2025-02-14 14:46:05
* @Entity com.bruce.sams.domain.sms.CollegeLeader
*/
@Mapper
public interface CollegeLeaderMapper extends BaseMapper<CollegeLeader> {
/**
* /
*/
@Select("SELECT * FROM sms_college_leader WHERE college_id = #{collegeId}")
CollegeLeader findLeaderByCollegeId(Long collegeId);
/**
* /
*/
@Delete("DELETE FROM sms_college_leader WHERE college_id = #{collegeId}")
void removeLeaderByCollegeId(Long collegeId);
}

View File

@ -0,0 +1,34 @@
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;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author bruce
* @description sms_college()Mapper
* @createDate 2025-02-14 13:18:44
* @Entity com.bruce.sams.domain.sms.College
*/
@Mapper
public interface CollegeMapper extends BaseMapper<College> {
/**
*
*/
@Select("SELECT * FROM sms_college WHERE parent_id = #{collegeId}")
List<College> findDepartmentsByUniversityId(Long collegeId);
/**
*
*/
@Select("SELECT * FROM sms_college WHERE parent_id IS NULL")
List<College> findAllUniversities();
}

View File

@ -12,7 +12,7 @@ import org.apache.ibatis.annotations.Select;
* @Entity generator.domain.SysRole
*/
@Mapper
public interface SysRoleMapper extends BaseMapper<Role> {
public interface RoleMapper extends BaseMapper<Role> {
@Select("SELECT * FROM sys_role WHERE role_id = #{roleId}")
Role findByRoleId(Long roleId);

View File

@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface SysUserMapper extends BaseMapper<User> {
public interface UserMapper extends BaseMapper<User> {
// 通过用户名查询用户
@Select("SELECT * FROM sys_user WHERE user_name = #{identifier}")

View File

@ -12,5 +12,5 @@ public interface AuthService {
* @param loginRequest username / schoolId / email + password
* @return JWT null
*/
public String authenticate(LoginRequest loginRequest);
String authenticate(LoginRequest loginRequest);
}

View File

@ -0,0 +1,27 @@
package com.bruce.sams.service;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author bruce
* @description sms_college_leader()Service
* @createDate 2025-02-14 14:46:05
*/
public interface CollegeLeaderService extends IService<CollegeLeader> {
/**
* /
*/
CollegeLeader getCollegeLeader(Long collegeId);
/**
* /
*/
public void assignLeader(Long collegeId, Long userId, Long adminUserId);
/**
*
*/
public void removeLeader(Long collegeId, Long adminUserId);
}

View File

@ -0,0 +1,44 @@
package com.bruce.sams.service;
import com.bruce.sams.domain.sms.College;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author bruce
* @description sms_college()Service
* @createDate 2025-02-14 13:18:44
*/
public interface CollegeService extends IService<College> {
/**
*
*/
public List<College> getAllUniversities();
/**
*
*/
public List<College> getDepartmentsByUniversity(Long universityId);
/**
*
*/
public College getCollegeById(Long collegeId);
/**
*
*/
public void addCollege(College college);
/**
* /
*/
public void updateCollege(College college);
/**
* /
*/
public void deleteCollege(Long collegeId);
}

View File

@ -5,8 +5,8 @@ import com.bruce.sams.domain.sys.User;
import com.bruce.sams.exception.AccountDisabledException;
import com.bruce.sams.exception.PasswordIncorrectException;
import com.bruce.sams.exception.UserNotFoundException;
import com.bruce.sams.mapper.SysRoleMapper;
import com.bruce.sams.mapper.SysUserMapper;
import com.bruce.sams.mapper.RoleMapper;
import com.bruce.sams.mapper.UserMapper;
import com.bruce.sams.service.AuthService;
import com.bruce.sams.utils.PasswordUtil;
import com.bruce.sams.utils.TokenUtil;
@ -20,10 +20,10 @@ import org.springframework.stereotype.Service;
public class AuthServiceImpl implements AuthService {
@Autowired
private SysUserMapper userMapper;
private UserMapper userMapper;
@Autowired
private SysRoleMapper roleMapper;
private RoleMapper roleMapper;
/**
*

View File

@ -0,0 +1,88 @@
package com.bruce.sams.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.constant.HttpStatus;
import com.bruce.sams.domain.sms.CollegeLeader;
import com.bruce.sams.domain.sys.User;
import com.bruce.sams.exception.AccountDisabledException;
import com.bruce.sams.exception.CustomException;
import com.bruce.sams.exception.UserNotFoundException;
import com.bruce.sams.mapper.UserMapper;
import com.bruce.sams.service.CollegeLeaderService;
import com.bruce.sams.mapper.CollegeLeaderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author bruce
* @description sms_college_leader()Service
* @createDate 2025-02-14 14:46:05
*/
@Service
public class CollegeLeaderServiceImpl extends ServiceImpl<CollegeLeaderMapper, CollegeLeader>
implements CollegeLeaderService{
@Autowired
private CollegeLeaderMapper collegeLeaderMapper;
@Autowired
private UserMapper userMapper;
/**
* /
*/
public CollegeLeader getCollegeLeader(Long collegeId) {
return collegeLeaderMapper.findLeaderByCollegeId(collegeId);
}
/**
* /
*/
public void assignLeader(Long collegeId, Long userId, Long adminUserId) {
// 查询用户是否存在
User user = userMapper.selectById(userId);
if (user == null) {
throw new UserNotFoundException();
}
// 检查是否禁用
if ("banned".equals(user.getStatus())) {
throw new AccountDisabledException();
}
// 检查指派权限(系统管理员只能指派学校管理员,学校管理员只能指派学院管理员)
User adminUser = userMapper.selectById(adminUserId);
if (adminUser == null || adminUser.getRoleId() > 1) { // roleId=0 为系统管理员roleId=1 为学校管理员
throw new CustomException(HttpStatus.FORBIDDEN, "无权指派负责人");
}
// 如果已存在负责人,先移除
collegeLeaderMapper.removeLeaderByCollegeId(collegeId);
// 指派新负责人
CollegeLeader leader = new CollegeLeader();
leader.setCollegeId(collegeId);
leader.setUserId(userId);
leader.setAssignedAt(new Date());
collegeLeaderMapper.insert(leader);
}
/**
*
*/
public void removeLeader(Long collegeId, Long adminUserId) {
User adminUser = userMapper.selectById(adminUserId);
if (adminUser == null || adminUser.getRoleId() > 1) {
throw new CustomException(HttpStatus.FORBIDDEN, "无权移除负责人");
}
collegeLeaderMapper.removeLeaderByCollegeId(collegeId);
}
}

View File

@ -0,0 +1,65 @@
package com.bruce.sams.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.domain.sms.College;
import com.bruce.sams.service.CollegeService;
import com.bruce.sams.mapper.CollegeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College>
implements CollegeService{
@Autowired
private CollegeMapper collegeMapper;
/**
*
*/
public List<College> getAllUniversities() {
return collegeMapper.findAllUniversities();
}
/**
*
*/
public List<College> getDepartmentsByUniversity(Long universityId) {
return collegeMapper.findDepartmentsByUniversityId(universityId);
}
/**
*
*/
public College getCollegeById(Long collegeId) {
return collegeMapper.selectById(collegeId);
}
/**
*
*/
public void addCollege(College college) {
collegeMapper.insert(college);
}
/**
* /
*/
public void updateCollege(College college) {
collegeMapper.updateById(college);
}
/**
* /
*/
public void deleteCollege(Long collegeId) {
collegeMapper.deleteById(collegeId);
}
}

View File

@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.sams.domain.sys.User;
import com.bruce.sams.exception.PasswordIncorrectException;
import com.bruce.sams.exception.UserNotFoundException;
import com.bruce.sams.mapper.SysUserMapper;
import com.bruce.sams.mapper.UserMapper;
import com.bruce.sams.service.UserService;
import com.bruce.sams.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
@ -14,11 +14,11 @@ import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<SysUserMapper, User>
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
implements UserService {
@Autowired
private SysUserMapper userMapper;
private UserMapper userMapper;
/**
*

View File

@ -1,6 +1,6 @@
package com.bruce.sams.utils;
import com.bruce.sams.Constant.HttpStatus;
import com.bruce.sams.constant.HttpStatus;
import java.util.HashMap;
import java.util.Objects;
@ -23,19 +23,8 @@ public class AjaxResult extends HashMap<String, Object>
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* AjaxResult 使
*/
public AjaxResult()
{
}
public AjaxResult() {}
/**
* AjaxResult
*
* @param code
* @param msg
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bruce.sams.mapper.CollegeLeaderMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sms.CollegeLeader">
<id property="collegeId" column="college_id" />
<id property="userId" column="user_id" />
<result property="assignedAt" column="assigned_at" />
</resultMap>
<sql id="Base_Column_List">
college_id,user_id,assigned_at
</sql>
</mapper>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bruce.sams.mapper.CollegeMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sms.College">
<id property="collegeId" column="college_id" />
<result property="collegeName" column="college_name" />
<result property="parentId" column="parent_id" />
<result property="email" column="email" />
<result property="createdAt" column="created_at" />
</resultMap>
<sql id="Base_Column_List">
college_id,college_name,parent_id,email,created_at
</sql>
</mapper>

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bruce.sams.mapper.SysRoleMapper">
<mapper namespace="com.bruce.sams.mapper.RoleMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sys.Role">
<id property="roleId" column="role_id" />

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bruce.sams.mapper.SysUserMapper">
<mapper namespace="com.bruce.sams.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.bruce.sams.domain.sys.User">
<id property="userId" column="user_id" />