查询课程下面的用户信息,代码查询逻辑优化

master
Alan 3 months ago
parent 55777b33a4
commit 3070602cb1
  1. 2
      src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudent.java
  2. 2
      src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java
  3. 218
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  4. 1
      src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java
  5. 7
      src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java

@ -6,6 +6,8 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@ -13,8 +13,6 @@ public class CourseUserInfVO {
private String courseId;
@ApiModelProperty(value = "课程name")
private String courseName;
@ApiModelProperty(value = "与课程有关的用户信息集合")
private List<UserReadMessageTimeVO> usersInf;
}

@ -52,6 +52,7 @@ import java.io.File;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.teaching.backend.constant.RoleConstants.TEACHER_ROLE;
@ -640,57 +641,76 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
}
private List<CourseUserInfVO> queryCourseUserForTeacher(Long userId) {
// 查询所有与该教师相关的课程
List<Courses> teacherCoursesInf = coursesMapper.selectList(
new LambdaQueryWrapper<Courses>()
.apply("FIND_IN_SET({0}, teacher)", userId)
.select(Courses::getId, Courses::getTeacher)
// 1. 获取该教师负责的所有课程
List<Courses> teacherCoursesInf = coursesMapper.selectList(new LambdaQueryWrapper<Courses>()
.apply("FIND_IN_SET({0}, teacher)", userId)
.select(Courses::getId, Courses::getTeacher, Courses::getName)
);
// 2. 提取所有课程的ID
Set<String> courseIds = teacherCoursesInf.stream()
.map(Courses::getId)
.collect(Collectors.toSet());
// 3. 批量查询所有课程的学生ID
List<StudentCourses> studentCourses = studentCoursesMapper.selectList(
new LambdaQueryWrapper<StudentCourses>().in(StudentCourses::getCourse, courseIds)
);
// 4. 获取所有的学生ID
Set<Long> studentIds = studentCourses.stream()
.map(StudentCourses::getStudent)
.collect(Collectors.toSet());
// 5. 批量查询所有学生的信息
List<UmsStudent> students = umsStudentMapper.selectList(
new LambdaQueryWrapper<UmsStudent>().in(UmsStudent::getUserId, studentIds)
);
// 6. 将学生信息缓存到Map中,Key为学生ID
Map<Long, UmsStudent> studentMap = students.stream()
.collect(Collectors.toMap(UmsStudent::getUserId, Function.identity()));
// 7. 处理教师信息并构建返回结果
List<CourseUserInfVO> courseUserInfVOS = new ArrayList<>();
for (Courses courses : teacherCoursesInf) {
for (Courses course : teacherCoursesInf) {
CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
courseUserInfVO.setCourseId(course.getId());
courseUserInfVO.setCourseName(course.getName());
List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>();
courseUserInfVO.setCourseId(courses.getId());
// 查询与课程相关的所有学生
List<Long> studentIds = studentCoursesMapper.selectList(
new LambdaQueryWrapper<>(StudentCourses.class)
.eq(StudentCourses::getCourse, courses.getId())
).stream().map(StudentCourses::getStudent).collect(Collectors.toList());
// 为每个学生创建一个新的 UserReadMessageTimeVO 对象
for (Long studentId : studentIds) {
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<>(UmsStudent.class)
.eq(UmsStudent::getUserId, studentId)
.select(UmsStudent::getName)
);
if (umsStudent == null) {
log.error("用户不存在: {}",studentId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
// 添加学生信息
for (StudentCourses sc : studentCourses) {
if (sc.getCourse().equals(course.getId())) {
UmsStudent student = studentMap.get(sc.getStudent()); // 类型转换
if (student != null) {
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
userReadMessageTimeVO.setId(student.getUserId());
userReadMessageTimeVO.setReceiverName(student.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
} else {
log.error("学生不存在: {}", sc.getStudent());
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(studentId);
userReadMessageTimeVO.setReceiverName(umsStudent.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
// 为每个教师创建一个新的 UserReadMessageTimeVO 对象
String[] teacherUserIds = courses.getTeacher().split(",");
// 添加教师信息
String[] teacherUserIds = course.getTeacher().split(",");
for (String teacherId : teacherUserIds) {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, teacherId)
new LambdaQueryWrapper<UmsTeacher>().eq(UmsTeacher::getUserId, teacherId)
);
if (umsTeacher == null) {
log.error("教师不存在: {}",teacherId);
if (umsTeacher != null) {
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
} else {
log.error("教师不存在: {}", teacherId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
courseUserInfVO.setUsersInf(userReadMessageTimeVOS);
@ -700,64 +720,94 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
}
private List<CourseUserInfVO> queryCourseUserForStudent(Long userId) {
// 查询学生所参与的所有课程
List<StudentCourses> studentCoursesList = studentCoursesMapper.selectList(
new LambdaQueryWrapper<StudentCourses>()
.eq(StudentCourses::getStudent, userId)
.select(StudentCourses::getCourse)
// 1. 获取学生参与的所有课程
List<StudentCourses> studentCourses = studentCoursesMapper.selectList(
new LambdaQueryWrapper<StudentCourses>().eq(StudentCourses::getStudent, userId)
);
// 2. 提取所有课程的ID
Set<String> courseIds = studentCourses.stream()
.map(StudentCourses::getCourse)
.collect(Collectors.toSet());
// 3. 批量查询所有课程的信息
List<Courses> coursesList = coursesMapper.selectBatchIds(courseIds);
// 4. 获取所有的教师ID
Set<Long> teacherIds = coursesList.stream()
.flatMap(course -> Arrays.stream(course.getTeacher().split(",")))
.map(Long::parseLong)
.collect(Collectors.toSet());
// 5. 批量查询所有教师的信息
List<UmsTeacher> teachers = umsTeacherMapper.selectList(
new LambdaQueryWrapper<UmsTeacher>().in(UmsTeacher::getUserId, teacherIds)
);
// 6. 将教师信息缓存到Map中,Key为教师ID
Map<Long, UmsTeacher> teacherMap = teachers.stream()
.collect(Collectors.toMap(UmsTeacher::getUserId, Function.identity()));
// 7. 批量查询所有课程的学生信息
List<StudentCourses> allStudentCourses = studentCoursesMapper.selectList(
new LambdaQueryWrapper<StudentCourses>().in(StudentCourses::getCourse, courseIds)
);
// 8. 提取所有学生的ID
Set<Long> allStudentIds = allStudentCourses.stream()
.map(StudentCourses::getStudent)
.collect(Collectors.toSet());
// 9. 批量查询所有学生的信息
List<UmsStudent> students = umsStudentMapper.selectList(
new LambdaQueryWrapper<UmsStudent>().in(UmsStudent::getUserId, allStudentIds)
);
// 10. 将学生信息缓存到Map中,Key为学生ID
Map<Long, UmsStudent> studentMap = students.stream()
.collect(Collectors.toMap(UmsStudent::getUserId, Function.identity()));
// 11. 处理课程信息并构建返回结果
List<CourseUserInfVO> courseUserInfVOS = new ArrayList<>();
for (StudentCourses studentCourse : studentCoursesList) {
for (Courses course : coursesList) {
CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
courseUserInfVO.setCourseId(course.getId());
courseUserInfVO.setCourseName(course.getName());
List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>();
courseUserInfVO.setCourseId(studentCourse.getCourse());
// 查询与课程相关的所有学生
List<Long> studentIds = studentCoursesMapper.selectList(
new LambdaQueryWrapper<>(StudentCourses.class)
.eq(StudentCourses::getCourse, studentCourse.getCourse())
).stream().map(StudentCourses::getStudent).collect(Collectors.toList());
// 为每个学生创建一个新的 UserReadMessageTimeVO 对象
for (Long studentId : studentIds) {
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<>(UmsStudent.class)
.eq(UmsStudent::getUserId, studentId)
.select(UmsStudent::getName)
);
if (umsStudent == null) {
log.error("用户不存在: {}", studentId);
// 添加教师信息
String[] courseTeacherIds = course.getTeacher().split(",");
for (String teacherId : courseTeacherIds) {
UmsTeacher umsTeacher = teacherMap.get(Long.valueOf(teacherId));
if (umsTeacher != null) {
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
} else {
log.error("教师不存在: {}", teacherId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(studentId);
userReadMessageTimeVO.setReceiverName(umsStudent.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
// 查询与课程相关的所有教师
Courses course = coursesMapper.selectById(studentCourse.getCourse());
if (course != null) {
String[] teacherUserIds = course.getTeacher().split(",");
for (String teacherId : teacherUserIds) {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, teacherId)
);
if (umsTeacher == null) {
log.error("教师不存在: {}", teacherId);
// 添加学生信息
for (StudentCourses sc : allStudentCourses) {
if (sc.getCourse().equals(course.getId())) {
UmsStudent student = studentMap.get(sc.getStudent()); // 类型转换
if (student != null) {
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
userReadMessageTimeVO.setId(student.getUserId());
userReadMessageTimeVO.setReceiverName(student.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
} else {
log.error("学生不存在: {}", sc.getStudent());
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
} else {
log.error("课程不存在: {}", studentCourse.getCourse());
throw new BusinessException(ErrorCode.PARAMS_COURSE_NOTEXISTS);
}
courseUserInfVO.setUsersInf(userReadMessageTimeVOS);
@ -766,6 +816,8 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
return courseUserInfVOS;
}
}

@ -56,7 +56,6 @@ public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox
@Autowired
private UserInboxMapper userInboxMapper;
//TODO 不需要特别指定“已读”或者是“未读”
@Override
public PageDTO<SysMessageVO> queryReceiveMessages(MessagesQuery messagesQuery) {
// 先按用户的id找到其的消息列表 默认看未删除的,(是否读 作为筛选条件后传进去)

@ -5,15 +5,8 @@ import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.chapter.TemporaryChapterMapper;
import com.teaching.backend.model.dto.chapter.ChapterExcelDTO;
import com.teaching.backend.service.chapter.IChapterService;
import jdk.nashorn.internal.ir.IfNode;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

Loading…
Cancel
Save