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 a69c9cec..da9623d6 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 @@ -118,77 +118,97 @@ public class AiolCourseController extends JeecgController teacherAdd(@RequestBody AiolCourseSaveDTO dto) { aiolCourseService.save(dto); + // 处理多个分类ID String categoryId = dto.getCategoryId(); if (categoryId != null && !categoryId.trim().isEmpty()) { - AiolEntityLink link = new AiolEntityLink(); - link.setSourceType("course_category"); - link.setSourceId(categoryId); - link.setTargetType("course"); - link.setTargetId(dto.getId()); - link.setCreateBy(dto.getCreateBy()); - link.setCreateTime(new Date()); - entityLinkMapper.insert(link); + String[] categoryIds = categoryId.split(","); + for (String catId : categoryIds) { + if (catId != null && !catId.trim().isEmpty()) { + AiolEntityLink link = new AiolEntityLink(); + link.setSourceType("course_category"); + link.setSourceId(catId.trim()); + link.setTargetType("course"); + link.setTargetId(dto.getId()); + link.setCreateBy(dto.getCreateBy()); + link.setCreateTime(new Date()); + entityLinkMapper.insert(link); + } + } } - // 如果提供了班级ID,将班级学生添加到课程报名表 + // 处理多个班级ID,将班级学生添加到课程报名表 String classId = dto.getClassId(); if (classId != null && !classId.trim().isEmpty()) { - try { - // 1. 根据班级ID查询学生列表 - QueryWrapper 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); + String[] classIds = classId.split(","); + int totalNewStudents = 0; + + for (String clsId : classIds) { + if (clsId == null || clsId.trim().isEmpty()) { + continue; } - } catch (Exception e) { - log.error("为课程 {} 添加班级 {} 学生失败: {}", dto.getId(), classId, e.getMessage(), e); - // 不抛出异常,避免影响课程创建 + try { + // 1. 根据班级ID查询学生列表 + QueryWrapper studentWrapper = new QueryWrapper<>(); + studentWrapper.eq("class_id", clsId.trim()); + 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); + } + + totalNewStudents += newStudentIds.size(); + log.info("成功为课程 {} 添加班级 {} 的学生,新增报名记录 {} 条", + dto.getId(), clsId.trim(), newStudentIds.size()); + } else { + log.info("班级 {} 的所有学生已报名课程 {}", clsId.trim(), dto.getId()); + } + } else { + log.warn("班级 {} 中没有找到学生", clsId.trim()); + } + + } catch (Exception e) { + log.error("为课程 {} 添加班级 {} 学生失败: {}", dto.getId(), clsId.trim(), e.getMessage(), e); + // 不抛出异常,避免影响课程创建 + } + } + + if (totalNewStudents > 0) { + log.info("课程 {} 总共新增学生报名记录 {} 条", dto.getId(), totalNewStudents); } } @@ -209,21 +229,28 @@ public class AiolCourseController extends JeecgController edit(@RequestBody AiolCourseSaveDTO dto) { aiolCourseService.updateById(dto); + // 删除旧的分类关联 QueryWrapper deleteWrapper = new QueryWrapper<>(); deleteWrapper.eq("target_type", "course").eq("target_id", dto.getId()) .eq("source_type", "course_category"); entityLinkMapper.delete(deleteWrapper); + // 处理多个分类ID String categoryId = dto.getCategoryId(); if (categoryId != null && !categoryId.trim().isEmpty()) { - AiolEntityLink link = new AiolEntityLink(); - link.setSourceType("course_category"); - link.setSourceId(categoryId); - link.setTargetType("course"); - link.setTargetId(dto.getId()); - link.setCreateBy(dto.getUpdateBy()); - link.setCreateTime(new Date()); - entityLinkMapper.insert(link); + String[] categoryIds = categoryId.split(","); + for (String catId : categoryIds) { + if (catId != null && !catId.trim().isEmpty()) { + AiolEntityLink link = new AiolEntityLink(); + link.setSourceType("course_category"); + link.setSourceId(catId.trim()); + link.setTargetType("course"); + link.setTargetId(dto.getId()); + link.setCreateBy(dto.getUpdateBy()); + link.setCreateTime(new Date()); + entityLinkMapper.insert(link); + } + } } return Result.OK("编辑成功!"); diff --git a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/dto/AiolCourseSaveDTO.java b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/dto/AiolCourseSaveDTO.java index e6ba282a..edac54a5 100644 --- a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/dto/AiolCourseSaveDTO.java +++ b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/dto/AiolCourseSaveDTO.java @@ -10,10 +10,10 @@ import org.jeecg.modules.aiol.entity.AiolCourse; @Schema(description = "课程保存DTO,扩展课程分类和班级") public class AiolCourseSaveDTO extends AiolCourse { - @Schema(description = "课程分类ID") + @Schema(description = "课程分类ID,支持多个,用英文逗号分割") private String categoryId; - @Schema(description = "班级ID") + @Schema(description = "班级ID,支持多个,用英文逗号分割") private String classId; }