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

master
Alan 4 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. 212
      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 java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable; import java.io.Serializable;
import io.swagger.models.auth.In;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;

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

@ -52,6 +52,7 @@ import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.teaching.backend.constant.RoleConstants.TEACHER_ROLE; 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) { private List<CourseUserInfVO> queryCourseUserForTeacher(Long userId) {
// 查询所有与该教师相关的课程 // 1. 获取该教师负责的所有课程
List<Courses> teacherCoursesInf = coursesMapper.selectList( List<Courses> teacherCoursesInf = coursesMapper.selectList(new LambdaQueryWrapper<Courses>()
new LambdaQueryWrapper<Courses>()
.apply("FIND_IN_SET({0}, teacher)", userId) .apply("FIND_IN_SET({0}, teacher)", userId)
.select(Courses::getId, Courses::getTeacher) .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<>(); List<CourseUserInfVO> courseUserInfVOS = new ArrayList<>();
for (Courses courses : teacherCoursesInf) { for (Courses course : teacherCoursesInf) {
CourseUserInfVO courseUserInfVO = new CourseUserInfVO(); CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
courseUserInfVO.setCourseId(course.getId());
courseUserInfVO.setCourseName(course.getName());
List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>(); List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>();
courseUserInfVO.setCourseId(courses.getId());
// 添加学生信息
// 查询与课程相关的所有学生 for (StudentCourses sc : studentCourses) {
List<Long> studentIds = studentCoursesMapper.selectList( if (sc.getCourse().equals(course.getId())) {
new LambdaQueryWrapper<>(StudentCourses.class) UmsStudent student = studentMap.get(sc.getStudent()); // 类型转换
.eq(StudentCourses::getCourse, courses.getId()) if (student != null) {
).stream().map(StudentCourses::getStudent).collect(Collectors.toList()); UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
userReadMessageTimeVO.setId(student.getUserId());
// 为每个学生创建一个新的 UserReadMessageTimeVO 对象 userReadMessageTimeVO.setReceiverName(student.getName());
for (Long studentId : studentIds) { userReadMessageTimeVOS.add(userReadMessageTimeVO);
UmsStudent umsStudent = umsStudentMapper.selectOne( } else {
new LambdaQueryWrapper<>(UmsStudent.class) log.error("学生不存在: {}", sc.getStudent());
.eq(UmsStudent::getUserId, studentId)
.select(UmsStudent::getName)
);
if (umsStudent == null) {
log.error("用户不存在: {}",studentId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); 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) { for (String teacherId : teacherUserIds) {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne( UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>() new LambdaQueryWrapper<UmsTeacher>().eq(UmsTeacher::getUserId, teacherId)
.eq(UmsTeacher::getUserId, teacherId)
); );
if (umsTeacher == null) { if (umsTeacher != null) {
log.error("教师不存在: {}",teacherId); UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(umsTeacher.getUserId()); userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName()); userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO); userReadMessageTimeVOS.add(userReadMessageTimeVO);
} else {
log.error("教师不存在: {}", teacherId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
} }
courseUserInfVO.setUsersInf(userReadMessageTimeVOS); courseUserInfVO.setUsersInf(userReadMessageTimeVOS);
@ -700,64 +720,94 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
} }
private List<CourseUserInfVO> queryCourseUserForStudent(Long userId) { private List<CourseUserInfVO> queryCourseUserForStudent(Long userId) {
// 查询学生所参与的所有课程 // 1. 获取学生参与的所有课程
List<StudentCourses> studentCoursesList = studentCoursesMapper.selectList( List<StudentCourses> studentCourses = studentCoursesMapper.selectList(
new LambdaQueryWrapper<StudentCourses>() new LambdaQueryWrapper<StudentCourses>().eq(StudentCourses::getStudent, userId)
.eq(StudentCourses::getStudent, userId) );
.select(StudentCourses::getCourse)
// 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<>(); List<CourseUserInfVO> courseUserInfVOS = new ArrayList<>();
for (StudentCourses studentCourse : studentCoursesList) { for (Courses course : coursesList) {
CourseUserInfVO courseUserInfVO = new CourseUserInfVO(); CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
courseUserInfVO.setCourseId(course.getId());
courseUserInfVO.setCourseName(course.getName());
List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>(); List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>();
courseUserInfVO.setCourseId(studentCourse.getCourse());
// 添加教师信息
// 查询与课程相关的所有学生 String[] courseTeacherIds = course.getTeacher().split(",");
List<Long> studentIds = studentCoursesMapper.selectList( for (String teacherId : courseTeacherIds) {
new LambdaQueryWrapper<>(StudentCourses.class) UmsTeacher umsTeacher = teacherMap.get(Long.valueOf(teacherId));
.eq(StudentCourses::getCourse, studentCourse.getCourse()) if (umsTeacher != null) {
).stream().map(StudentCourses::getStudent).collect(Collectors.toList()); UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
userReadMessageTimeVO.setId(umsTeacher.getUserId());
// 为每个学生创建一个新的 UserReadMessageTimeVO 对象 userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
for (Long studentId : studentIds) { userReadMessageTimeVOS.add(userReadMessageTimeVO);
UmsStudent umsStudent = umsStudentMapper.selectOne( } else {
new LambdaQueryWrapper<>(UmsStudent.class) log.error("教师不存在: {}", teacherId);
.eq(UmsStudent::getUserId, studentId)
.select(UmsStudent::getName)
);
if (umsStudent == null) {
log.error("用户不存在: {}", studentId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); 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()); for (StudentCourses sc : allStudentCourses) {
if (course != null) { if (sc.getCourse().equals(course.getId())) {
String[] teacherUserIds = course.getTeacher().split(","); UmsStudent student = studentMap.get(sc.getStudent()); // 类型转换
for (String teacherId : teacherUserIds) { if (student != null) {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne( UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO();
new LambdaQueryWrapper<UmsTeacher>() userReadMessageTimeVO.setId(student.getUserId());
.eq(UmsTeacher::getUserId, teacherId) userReadMessageTimeVO.setReceiverName(student.getName());
); userReadMessageTimeVOS.add(userReadMessageTimeVO);
if (umsTeacher == null) { } else {
log.error("教师不存在: {}", teacherId); log.error("学生不存在: {}", sc.getStudent());
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); 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); courseUserInfVO.setUsersInf(userReadMessageTimeVOS);
@ -766,6 +816,8 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
return courseUserInfVOS; return courseUserInfVOS;
} }
} }

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

@ -5,15 +5,8 @@ import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; import com.teaching.backend.mapper.chapter.TemporaryChapterMapper;
import com.teaching.backend.model.dto.chapter.ChapterExcelDTO; import com.teaching.backend.model.dto.chapter.ChapterExcelDTO;
import com.teaching.backend.service.chapter.IChapterService; 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.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;

Loading…
Cancel
Save