|
|
@@ -9,13 +9,8 @@ import com.njuzr.eaibackend.dto.course.CourseUpdateDTO;
|
|
|
import com.njuzr.eaibackend.dto.course.EnrollDTO;
|
|
|
import com.njuzr.eaibackend.enums.Role;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
-import com.njuzr.eaibackend.mapper.CourseMapper;
|
|
|
-import com.njuzr.eaibackend.mapper.CourseStudentMapper;
|
|
|
-import com.njuzr.eaibackend.mapper.UserMapper;
|
|
|
-import com.njuzr.eaibackend.po.Course;
|
|
|
-import com.njuzr.eaibackend.po.Enrollment;
|
|
|
-import com.njuzr.eaibackend.po.MyUserDetails;
|
|
|
-import com.njuzr.eaibackend.po.User;
|
|
|
+import com.njuzr.eaibackend.mapper.*;
|
|
|
+import com.njuzr.eaibackend.po.*;
|
|
|
import com.njuzr.eaibackend.service.CourseService;
|
|
|
import com.njuzr.eaibackend.utils.ConvertUtil;
|
|
|
import com.njuzr.eaibackend.utils.ModelMapperUtil;
|
|
|
@@ -27,6 +22,7 @@ import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
@@ -50,11 +46,17 @@ public class CourseServiceImpl implements CourseService {
|
|
|
|
|
|
private final CourseStudentMapper courseStudentMapper;
|
|
|
|
|
|
+ private final AssignmentMapper assignmentMapper;
|
|
|
+
|
|
|
+ private final StudentAssignmentMapper studentAssignmentMapper;
|
|
|
+
|
|
|
@Autowired
|
|
|
- public CourseServiceImpl(CourseMapper courseMapper, UserMapper userMapper, CourseStudentMapper courseStudentMapper) {
|
|
|
+ public CourseServiceImpl(CourseMapper courseMapper, UserMapper userMapper, CourseStudentMapper courseStudentMapper, AssignmentMapper assignmentMapper, StudentAssignmentMapper studentAssignmentMapper) {
|
|
|
this.courseMapper = courseMapper;
|
|
|
this.userMapper = userMapper;
|
|
|
this.courseStudentMapper = courseStudentMapper;
|
|
|
+ this.assignmentMapper = assignmentMapper;
|
|
|
+ this.studentAssignmentMapper = studentAssignmentMapper;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -180,18 +182,36 @@ public class CourseServiceImpl implements CourseService {
|
|
|
* @param courseId
|
|
|
*/
|
|
|
@Override
|
|
|
+ @Transactional
|
|
|
public void deleteById(MyUserDetails user, Long courseId) {
|
|
|
Course course = courseMapper.selectById(courseId);
|
|
|
- if (course == null)
|
|
|
+ if (course == null) {
|
|
|
throw MyException.create(HttpStatus.BAD_REQUEST, "课程不存在");
|
|
|
+ }
|
|
|
|
|
|
- if (user.getRole() != Role.ADMIN && !course.getTeacherIds().contains(String.valueOf(user.getId())))
|
|
|
+ if (user.getRole() != Role.ADMIN && !course.getTeacherIds().contains(String.valueOf(user.getId()))) {
|
|
|
throw MyException.create(HttpStatus.BAD_REQUEST, "无权限删除");
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
+ // 首先找到选了该课程的所有学生
|
|
|
+ List<User> students = courseStudentMapper.selectStudentsByCourseId(courseId);
|
|
|
+ // 所有学生执行退课操作
|
|
|
+ for(User student : students) {
|
|
|
+ dropCourse(student.getId(), courseId);
|
|
|
+ }
|
|
|
+ // 删除该课程下的所有assignment信息
|
|
|
+ QueryWrapper<Assignment> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("course_id", courseId);
|
|
|
+ List<Assignment> assignments = assignmentMapper.selectList(wrapper);
|
|
|
+ for(Assignment assignment : assignments) {
|
|
|
+ assignmentMapper.deleteById(assignment.getAssignmentId());
|
|
|
+ }
|
|
|
+ // 删除课程信息
|
|
|
int code = courseMapper.deleteById(courseId);
|
|
|
- if (code == 0)
|
|
|
+ if (code == 0) {
|
|
|
throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "删除课程失败");
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage());
|
|
|
throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "删除课程失败");
|
|
|
@@ -249,6 +269,29 @@ public class CourseServiceImpl implements CourseService {
|
|
|
return courseMapper.getEnrollCodeByCourseId(courseId);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 学生退课
|
|
|
+ * @param studentId
|
|
|
+ * @param courseId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void dropCourse(Long studentId, Long courseId) {
|
|
|
+ // 首先找到该课程的所有作业信息
|
|
|
+ QueryWrapper<Assignment> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("course_id", courseId);
|
|
|
+ List<Assignment> assignments = assignmentMapper.selectList(wrapper);
|
|
|
+ // 删除学生的所有作业记录
|
|
|
+ for(Assignment assignment : assignments) {
|
|
|
+ studentAssignmentMapper.delete(studentId, assignment.getAssignmentId());
|
|
|
+ }
|
|
|
+ // 学生退课
|
|
|
+ int code = courseStudentMapper.dropCourse(studentId, courseId);
|
|
|
+ if (code == 0) {
|
|
|
+ throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "退课失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 将DTO转换成PO
|
|
|
* @param courseDTO
|