diff --git a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolCourseController.java b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolCourseController.java index d503a0fb..a69c9cec 100644 --- a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolCourseController.java +++ b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolCourseController.java @@ -29,6 +29,7 @@ import org.jeecg.modules.aiol.constant.EntityPermissionConst; import org.jeecg.modules.aiol.mapper.AiolCourseSignupMapper; import org.jeecg.modules.aiol.mapper.AiolCourseTeacherMapper; import org.jeecg.modules.aiol.mapper.AiolEntityLinkMapper; +import org.jeecg.modules.aiol.mapper.AiolClassStudentMapper; import org.jeecg.modules.system.entity.SysUser; import org.jeecg.modules.system.mapper.SysUserMapper; import org.springframework.beans.BeanUtils; @@ -104,7 +105,24 @@ public class AiolCourseController extends JeecgController add(@RequestBody AiolCourseSaveDTO dto) { + public Result add(@RequestBody AiolCourse dto) { + aiolCourseService.save(dto); + + return Result.OK("添加成功!"); + } + + /** + * 添加 + * + * @param aiolCourse + * @return + */ + @AutoLog(value = "课程-教师端添加") + @Operation(summary = "课程-教师端添加", description = "添加课程并关联分类,同时根据班级ID将班级学生添加到课程报名表") + @RequiresPermissions("aiol:aiol_course:add") + @PostMapping(value = "/teacher_add") + @Transactional(rollbackFor = Exception.class) + public Result teacherAdd(@RequestBody AiolCourseSaveDTO dto) { aiolCourseService.save(dto); String categoryId = dto.getCategoryId(); @@ -119,6 +137,61 @@ public class AiolCourseController extends JeecgController studentWrapper = new QueryWrapper<>(); + studentWrapper.eq("class_id", classId); + List classStudents = classStudentMapper.selectList(studentWrapper); + + if (!classStudents.isEmpty()) { + // 2. 检查已存在的报名记录,避免重复添加 + List studentIds = classStudents.stream() + .map(AiolClassStudent::getStudentId) + .collect(Collectors.toList()); + + QueryWrapper existingWrapper = new QueryWrapper<>(); + existingWrapper.eq("course_id", dto.getId()) + .in("user_id", studentIds); + List existingSignups = courseSignupMapper.selectList(existingWrapper); + + // 3. 过滤掉已存在的学生ID + Set existingStudentIds = existingSignups.stream() + .map(AiolCourseSignup::getUserId) + .collect(Collectors.toSet()); + + List newStudentIds = studentIds.stream() + .filter(id -> !existingStudentIds.contains(id)) + .collect(Collectors.toList()); + + // 4. 为未报名的学生创建报名记录 + if (!newStudentIds.isEmpty()) { + for (String studentId : newStudentIds) { + AiolCourseSignup signup = new AiolCourseSignup(); + signup.setCourseId(dto.getId()); + signup.setUserId(studentId); + signup.setCreateBy(dto.getCreateBy()); + signup.setCreateTime(new Date()); + courseSignupMapper.insert(signup); + } + + log.info("成功为课程 {} 添加班级 {} 的学生,新增报名记录 {} 条", + dto.getId(), classId, newStudentIds.size()); + } else { + log.info("班级 {} 的所有学生已报名课程 {}", classId, dto.getId()); + } + } else { + log.warn("班级 {} 中没有找到学生", classId); + } + + } catch (Exception e) { + log.error("为课程 {} 添加班级 {} 学生失败: {}", dto.getId(), classId, e.getMessage(), e); + // 不抛出异常,避免影响课程创建 + } + } + return Result.OK("添加成功!"); } @@ -242,6 +315,8 @@ public class AiolCourseController extends JeecgController