【新增】学生管理功能

This commit is contained in:
任维炳
2025-04-23 17:23:23 +08:00
parent 8f99ff4d47
commit 121da7c76a
13 changed files with 640 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package pc.exam.pp.module.exam.controller.admin.enums.student;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum ErrorStudentEnums {
STUDENT_NOT_EXISTS("没有");
private final String description;
}

View File

@@ -0,0 +1,101 @@
package pc.exam.pp.module.exam.controller.admin.student;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import pc.exam.pp.framework.common.pojo.PageParam;
import pc.exam.pp.framework.common.pojo.PageResult;
import pc.exam.pp.framework.common.pojo.CommonResult;
import pc.exam.pp.framework.common.util.object.BeanUtils;
import static pc.exam.pp.framework.common.pojo.CommonResult.success;
import pc.exam.pp.framework.excel.core.util.ExcelUtils;
import pc.exam.pp.framework.apilog.core.annotation.ApiAccessLog;
import static pc.exam.pp.framework.apilog.core.enums.OperateTypeEnum.*;
import pc.exam.pp.module.exam.controller.admin.student.vo.*;
import pc.exam.pp.module.exam.dal.dataobject.student.StudentDO;
import pc.exam.pp.module.exam.service.student.StudentService;
@Tag(name = "管理后台 - 学生")
@RestController
@RequestMapping("/exam/student")
@Validated
public class StudentController {
@Resource
private StudentService studentService;
@PostMapping("/create")
@Operation(summary = "创建学生")
@PreAuthorize("@ss.hasPermission('exam:student:create')")
public CommonResult<Long> createStudent(@Valid @RequestBody StudentSaveReqVO createReqVO) {
return success(studentService.createStudent(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新学生")
@PreAuthorize("@ss.hasPermission('exam:student:update')")
public CommonResult<Boolean> updateStudent(@Valid @RequestBody StudentSaveReqVO updateReqVO) {
studentService.updateStudent(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除学生")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('exam:student:delete')")
public CommonResult<Boolean> deleteStudent(@RequestParam("id") Long id) {
studentService.deleteStudent(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得学生")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('exam:student:query')")
public CommonResult<StudentRespVO> getStudent(@RequestParam("id") Long id) {
StudentDO student = studentService.getStudent(id);
return success(BeanUtils.toBean(student, StudentRespVO.class));
}
@GetMapping("/page")
@Operation(summary = "获得学生分页")
@PreAuthorize("@ss.hasPermission('exam:student:query')")
public CommonResult<PageResult<StudentRespVO>> getStudentPage(@Valid StudentPageReqVO pageReqVO) {
PageResult<StudentDO> pageResult = studentService.getStudentPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, StudentRespVO.class));
}
@GetMapping("/export-excel")
@Operation(summary = "导出学生 Excel")
@PreAuthorize("@ss.hasPermission('exam:student:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportStudentExcel(@Valid StudentPageReqVO pageReqVO,
HttpServletResponse response) throws IOException {
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<StudentDO> list = studentService.getStudentPage(pageReqVO).getList();
// 导出 Excel
ExcelUtils.write(response, "学生.xls", "数据", StudentRespVO.class,
BeanUtils.toBean(list, StudentRespVO.class));
}
@GetMapping("/getStuList")
@Operation(summary = "获得没有绑定过班级的学生列表")
public CommonResult<List<StudentRespVO>> getStudentPage() {
List<StudentDO> pageResult = studentService.getNobindingStudents();
return success(BeanUtils.toBean(pageResult, StudentRespVO.class));
}
}

View File

@@ -0,0 +1,53 @@
package pc.exam.pp.module.exam.controller.admin.student.vo;
import lombok.*;
import io.swagger.v3.oas.annotations.media.Schema;
import pc.exam.pp.framework.common.pojo.PageParam;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static pc.exam.pp.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 学生分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class StudentPageReqVO extends PageParam {
@Schema(description = "名字", example = "李四")
private String name;
@Schema(description = "学号")
private String code;
@Schema(description = "状态", example = "1")
private Integer status;
@Schema(description = "学生用户名", example = "王五")
private String userName;
@Schema(description = "学生密码")
private String passWord;
@Schema(description = "性别")
private Integer sex;
@Schema(description = "电子邮件")
private String email;
@Schema(description = "学生电话")
private String phone;
@Schema(description = "简介", example = "随便")
private String description;
@Schema(description = "头像")
private String avatar;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
private Long classId;
}

View File

@@ -0,0 +1,66 @@
package pc.exam.pp.module.exam.controller.admin.student.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.*;
import pc.exam.pp.framework.excel.core.annotations.DictFormat;
import pc.exam.pp.framework.excel.core.convert.DictConvert;
@Schema(description = "管理后台 - 学生 Response VO")
@Data
@ExcelIgnoreUnannotated
public class StudentRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "25172")
@ExcelProperty("编号")
private Long id;
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "李四")
@ExcelProperty("名字")
private String name;
@Schema(description = "学号")
@ExcelProperty("学号")
private String code;
@Schema(description = "状态", example = "1")
@ExcelProperty(value = "状态", converter = DictConvert.class)
@DictFormat("common_status") // TODO 代码优化:建议设置到对应的 DictTypeConstants 枚举类中
private Integer status;
@Schema(description = "学生用户名", example = "王五")
@ExcelProperty("学生用户名")
private String userName;
@Schema(description = "学生密码")
@ExcelProperty("学生密码")
private String passWord;
@Schema(description = "性别", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty(value = "性别", converter = DictConvert.class)
@DictFormat("system_user_sex") // TODO 代码优化:建议设置到对应的 DictTypeConstants 枚举类中
private Integer sex;
@Schema(description = "电子邮件")
@ExcelProperty("电子邮件")
private String email;
@Schema(description = "学生电话")
@ExcelProperty("学生电话")
private String phone;
@Schema(description = "简介", requiredMode = Schema.RequiredMode.REQUIRED, example = "随便")
@ExcelProperty("简介")
private String description;
@Schema(description = "头像")
@ExcelProperty("头像")
private String avatar;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,47 @@
package pc.exam.pp.module.exam.controller.admin.student.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import jakarta.validation.constraints.*;
@Schema(description = "管理后台 - 学生新增/修改 Request VO")
@Data
public class StudentSaveReqVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "25172")
private Long id;
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "李四")
@NotEmpty(message = "名字不能为空")
private String name;
@Schema(description = "学号")
private String code;
@Schema(description = "状态", example = "1")
private Integer status;
@Schema(description = "学生用户名", example = "王五")
private String userName;
@Schema(description = "学生密码")
private String passWord;
@Schema(description = "性别", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "性别不能为空")
private Integer sex;
@Schema(description = "电子邮件")
private String email;
@Schema(description = "学生电话")
private String phone;
@Schema(description = "简介", requiredMode = Schema.RequiredMode.REQUIRED, example = "随便")
@NotEmpty(message = "简介不能为空")
private String description;
@Schema(description = "头像")
private String avatar;
}

View File

@@ -0,0 +1,34 @@
package pc.exam.pp.module.exam.dal.dataobject.student;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import pc.exam.pp.framework.tenant.core.db.TenantBaseDO;
/**
* 学生班级 DO
*
* @author rwb
*/
@TableName("exam_student_class")
@Data
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StudentClassDO extends TenantBaseDO {
/**
* Id
*/
@TableId
private Long id;
/**
* 班级编号
*/
private Long classId;
/**
* 学生编号
*/
private Long studentId;
}

View File

@@ -0,0 +1,72 @@
package pc.exam.pp.module.exam.dal.dataobject.student;
import lombok.*;
import com.baomidou.mybatisplus.annotation.*;
import pc.exam.pp.framework.mybatis.core.dataobject.BaseDO;
/**
* 学生 DO
*
* @author rwb
*/
@TableName("exam_student")
@KeySequence("exam_student_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StudentDO extends BaseDO {
/**
* 编号
*/
@TableId
private Long id;
/**
* 名字
*/
private String name;
/**
* 学号
*/
private String code;
/**
* 状态
*
* 枚举 {@link TODO common_status 对应的类}
*/
private Integer status;
/**
* 学生用户名
*/
private String userName;
/**
* 学生密码
*/
private String passWord;
/**
* 性别
*
* 枚举 {@link TODO system_user_sex 对应的类}
*/
private Integer sex;
/**
* 电子邮件
*/
private String email;
/**
* 学生电话
*/
private String phone;
/**
* 简介
*/
private String description;
/**
* 头像
*/
private String avatar;
}

View File

@@ -0,0 +1,12 @@
package pc.exam.pp.module.exam.dal.mysql.student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import pc.exam.pp.framework.mybatis.core.mapper.BaseMapperX;
import pc.exam.pp.module.exam.controller.admin.classs.vo.ClassStudentSaveReqVO;
import pc.exam.pp.module.exam.dal.dataobject.student.StudentClassDO;
@Mapper
public interface StudentClassMapper extends BaseMapperX<StudentClassDO> {
}

View File

@@ -0,0 +1,62 @@
package pc.exam.pp.module.exam.dal.mysql.student;
import pc.exam.pp.framework.common.pojo.PageResult;
import pc.exam.pp.framework.mybatis.core.query.LambdaQueryWrapperX;
import pc.exam.pp.framework.mybatis.core.mapper.BaseMapperX;
import pc.exam.pp.framework.mybatis.core.query.MPJLambdaWrapperX;
import pc.exam.pp.module.exam.dal.dataobject.student.StudentClassDO;
import pc.exam.pp.module.exam.dal.dataobject.student.StudentDO;
import org.apache.ibatis.annotations.Mapper;
import pc.exam.pp.module.exam.controller.admin.student.vo.*;
import java.util.List;
/**
* 学生 Mapper
*
* @author rwb
*/
@Mapper
public interface StudentMapper extends BaseMapperX<StudentDO> {
// default PageResult<StudentDO> selectPage(StudentPageReqVO reqVO) {
// return selectPage(reqVO, new LambdaQueryWrapperX<StudentDO>()
// .likeIfPresent(StudentDO::getName, reqVO.getName())
// .eqIfPresent(StudentDO::getCode, reqVO.getCode())
// .eqIfPresent(StudentDO::getStatus, reqVO.getStatus())
// .likeIfPresent(StudentDO::getUserName, reqVO.getUserName())
// .eqIfPresent(StudentDO::getPassWord, reqVO.getPassWord())
// .eqIfPresent(StudentDO::getSex, reqVO.getSex())
// .eqIfPresent(StudentDO::getEmail, reqVO.getEmail())
// .eqIfPresent(StudentDO::getPhone, reqVO.getPhone())
// .eqIfPresent(StudentDO::getDescription, reqVO.getDescription())
// .eqIfPresent(StudentDO::getAvatar, reqVO.getAvatar())
// .betweenIfPresent(StudentDO::getCreateTime, reqVO.getCreateTime())
// .orderByDesc(StudentDO::getId));
// }
default PageResult<StudentDO> selectPage(StudentPageReqVO reqVO) {
MPJLambdaWrapperX<StudentDO> query = new MPJLambdaWrapperX<StudentDO>()
.likeIfPresent(StudentDO::getName, reqVO.getName())
.eqIfPresent(StudentDO::getCode, reqVO.getCode())
.eqIfPresent(StudentDO::getStatus, reqVO.getStatus())
.likeIfPresent(StudentDO::getUserName, reqVO.getUserName())
.eqIfPresent(StudentDO::getPassWord, reqVO.getPassWord())
.eqIfPresent(StudentDO::getSex, reqVO.getSex())
.eqIfPresent(StudentDO::getEmail, reqVO.getEmail())
.eqIfPresent(StudentDO::getPhone, reqVO.getPhone())
.eqIfPresent(StudentDO::getDescription, reqVO.getDescription())
.eqIfPresent(StudentDO::getAvatar, reqVO.getAvatar())
.betweenIfPresent(StudentDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(StudentDO::getId);
if (reqVO.getClassId() != null) {
query.leftJoin(StudentClassDO.class, StudentClassDO::getStudentId, StudentDO::getId)
.eq(reqVO.getClassId() != null, StudentClassDO::getClassId, reqVO.getClassId()); // 避免 1 对多查询,产生相同的 1
}
return selectJoinPage(reqVO, StudentDO.class, query);
}
List<StudentDO> getBindingStudents();
}

View File

@@ -0,0 +1,69 @@
package pc.exam.pp.module.exam.service.student;
import jakarta.validation.*;
import pc.exam.pp.module.exam.controller.admin.student.vo.*;
import pc.exam.pp.module.exam.dal.dataobject.student.StudentDO;
import pc.exam.pp.framework.common.pojo.PageResult;
import java.util.List;
/**
* 学生 Service 接口
*
* @author rwb
*/
public interface StudentService {
/**
* 创建学生
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createStudent(@Valid StudentSaveReqVO createReqVO);
/**
* 更新学生
*
* @param updateReqVO 更新信息
*/
void updateStudent(@Valid StudentSaveReqVO updateReqVO);
/**
* 删除学生
*
* @param id 编号
*/
void deleteStudent(Long id);
/**
* 根据班级ID查询学生列表分页
*
* @param id 编号
*/
// PageResult<StudentDO> getStudentListByClassId(Long id);
/**
* 获得学生
*
* @param id 编号
* @return 学生
*/
StudentDO getStudent(Long id);
/**
* 获得学生分页
*
* @param pageReqVO 分页查询
* @return 学生分页
*/
PageResult<StudentDO> getStudentPage(StudentPageReqVO pageReqVO);
/**
* 获得学生没有绑定班级的学生列表
*
* @return 学生列表
*/
List<StudentDO> getNobindingStudents();
}

View File

@@ -0,0 +1,78 @@
package pc.exam.pp.module.exam.service.student;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import pc.exam.pp.module.exam.controller.admin.student.vo.*;
import pc.exam.pp.module.exam.dal.dataobject.student.StudentDO;
import pc.exam.pp.framework.common.pojo.PageResult;
import pc.exam.pp.framework.common.util.object.BeanUtils;
import pc.exam.pp.module.exam.dal.mysql.student.StudentMapper;
import java.util.List;
import static pc.exam.pp.framework.common.exception.util.ServiceExceptionUtil.exception;
import static pc.exam.pp.module.system.enums.ErrorCodeConstants.STUDENT_NOT_EXISTS;
/**
* 学生 Service 实现类
*
* @author rwb
*/
@Service
@Validated
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Override
public Long createStudent(StudentSaveReqVO createReqVO) {
// 插入
StudentDO student = BeanUtils.toBean(createReqVO, StudentDO.class);
studentMapper.insert(student);
// 返回
return student.getId();
}
@Override
public void updateStudent(StudentSaveReqVO updateReqVO) {
// 校验存在
validateStudentExists(updateReqVO.getId());
// 更新
StudentDO updateObj = BeanUtils.toBean(updateReqVO, StudentDO.class);
studentMapper.updateById(updateObj);
}
@Override
public void deleteStudent(Long id) {
// 校验存在
validateStudentExists(id);
// 删除
studentMapper.deleteById(id);
}
private void validateStudentExists(Long id) {
if (studentMapper.selectById(id) == null) {
throw exception(STUDENT_NOT_EXISTS);
}
}
@Override
public StudentDO getStudent(Long id) {
return studentMapper.selectById(id);
}
@Override
public PageResult<StudentDO> getStudentPage(StudentPageReqVO pageReqVO) {
return studentMapper.selectPage(pageReqVO);
}
@Override
public List<StudentDO> getNobindingStudents() {
return studentMapper.getBindingStudents();
}
}

View File

@@ -0,0 +1,12 @@
<?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="pc.exam.pp.module.exam.dal.mysql.student.StudentClassMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>

View File

@@ -0,0 +1,20 @@
<?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="pc.exam.pp.module.exam.dal.mysql.student.StudentMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
<select id="getBindingStudents" resultType="pc.exam.pp.module.exam.dal.dataobject.student.StudentDO">
SELECT *
FROM exam_student
WHERE id NOT IN (
SELECT student_id FROM exam_student_class
)
AND deleted = 0;
</select>
</mapper>