From b1ccfb1c3e3b85a97aacebce8471da9b8feb4acf Mon Sep 17 00:00:00 2001 From: Alan <3052806735@qq.com> Date: Sat, 10 Aug 2024 20:30:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8F=91=E4=BB=B6=E7=AE=B1?= =?UTF-8?q?=20=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/courses/CoursesController.java | 9 + .../model/entity/courses/StudentCourses.java | 2 +- .../model/vo/message/CourseUserInfVO.java | 20 +++ .../model/vo/message/UserCheckMessageVO.java | 36 ---- .../service/courses/ICoursesService.java | 3 + .../impl/courses/CoursesServiceImpl.java | 157 +++++++++++++++++- .../courses/StudentCoursesServiceImpl.java | 2 +- .../impl/message/MessagesServiceImpl.java | 14 +- .../impl/message/UserInboxServiceImpl.java | 131 +++++++-------- 9 files changed, 263 insertions(+), 111 deletions(-) create mode 100644 src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java delete mode 100644 src/main/java/com/teaching/backend/model/vo/message/UserCheckMessageVO.java diff --git a/src/main/java/com/teaching/backend/controller/courses/CoursesController.java b/src/main/java/com/teaching/backend/controller/courses/CoursesController.java index adba450..c42bb16 100644 --- a/src/main/java/com/teaching/backend/controller/courses/CoursesController.java +++ b/src/main/java/com/teaching/backend/controller/courses/CoursesController.java @@ -12,6 +12,7 @@ import com.teaching.backend.model.entity.umsAdmin.UmsStudent; import com.teaching.backend.model.query.CourseQuery; import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.courses.PersonalCenterStudentListVO; +import com.teaching.backend.model.vo.message.CourseUserInfVO; import com.teaching.backend.service.courses.ICoursesService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -138,4 +139,12 @@ public class CoursesController { return CommonResult.success(umsStudentList); } + @ApiOperation("根据用户id,查询与用户有关的课程列表,并在课程列表显示所有用户信息") +// @ValidateParams({"userId"}) + @GetMapping("/page/courseusers") + public BaseResponse> getUserInfByCourse(@RequestParam Long userId){ + List courseUsersInfList = coursesService.getUserInfByCourse(userId); + return ResultUtils.success(courseUsersInfList); + } + } diff --git a/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java b/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java index a91a9a0..151e8ea 100644 --- a/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java +++ b/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java @@ -33,7 +33,7 @@ public class StudentCourses implements Serializable { /** * 学生的userId */ - private String student; + private Long student; /** * 课程id diff --git a/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java b/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java new file mode 100644 index 0000000..364f456 --- /dev/null +++ b/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java @@ -0,0 +1,20 @@ +package com.teaching.backend.model.vo.message; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +public class CourseUserInfVO { + + @ApiModelProperty(value = "课程id") + private String courseId; + @ApiModelProperty(value = "课程name") + private String courseName; + + @ApiModelProperty(value = "与课程有关的用户信息集合") + private List usersInf; + +} diff --git a/src/main/java/com/teaching/backend/model/vo/message/UserCheckMessageVO.java b/src/main/java/com/teaching/backend/model/vo/message/UserCheckMessageVO.java deleted file mode 100644 index 373ade4..0000000 --- a/src/main/java/com/teaching/backend/model/vo/message/UserCheckMessageVO.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.teaching.backend.model.vo.message; - -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; - -import java.time.LocalDateTime; - -@Data -public class UserCheckMessageVO { - @ApiModelProperty("id") - private Long id; - - @ApiModelProperty("课程id") - private String courseId; - - @ApiModelProperty("课程名称") - private String courseName; - - @ApiModelProperty(value = "发件人id") - private Long senderId; - @ApiModelProperty(value = "发件人name") - private String senderName; - - @ApiModelProperty(value = "消息标题") - private String title; - - @ApiModelProperty(value = "消息内容") - private String content; - - @ApiModelProperty(value = "发送时间") - private LocalDateTime sendTime; - - @ApiModelProperty(value = "是否已读(0没有,1有)") - private Boolean isRead; - -} diff --git a/src/main/java/com/teaching/backend/service/courses/ICoursesService.java b/src/main/java/com/teaching/backend/service/courses/ICoursesService.java index 9f619a2..ac2e4c7 100644 --- a/src/main/java/com/teaching/backend/service/courses/ICoursesService.java +++ b/src/main/java/com/teaching/backend/service/courses/ICoursesService.java @@ -11,6 +11,7 @@ import com.teaching.backend.model.entity.umsAdmin.UmsUser; import com.teaching.backend.model.query.CourseQuery; import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.courses.PersonalCenterStudentListVO; +import com.teaching.backend.model.vo.message.CourseUserInfVO; import com.teaching.backend.model.vo.umsAdmin.UmsStudentVO; import javax.servlet.http.HttpServletResponse; @@ -51,4 +52,6 @@ public interface ICoursesService extends IService { List queryStudentList(String userId); LinkedHashSet queryTeacherByStudentList(String userId); + + List getUserInfByCourse(Long userId); } diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index ebfe8ae..da9f0dc 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java @@ -32,9 +32,12 @@ import com.teaching.backend.model.query.CourseQuery; import com.teaching.backend.model.vo.courses.CourseObjectivesTreeVO; import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.courses.PersonalCenterStudentListVO; +import com.teaching.backend.model.vo.message.CourseUserInfVO; +import com.teaching.backend.model.vo.message.UserReadMessageTimeVO; import com.teaching.backend.model.vo.umsAdmin.UmsStudentVO; import com.teaching.backend.service.courses.ICoursesService; import com.teaching.backend.utils.CourseCode; +import lombok.extern.slf4j.Slf4j; import org.apache.poi.xwpf.usermodel.*; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -62,6 +65,7 @@ import static com.teaching.backend.constant.RoleConstants.TEACHER_ROLE; * @since 2024-05-30 */ @Service +@Slf4j public class CoursesServiceImpl extends ServiceImpl implements ICoursesService { @Autowired @@ -382,7 +386,7 @@ public class CoursesServiceImpl extends ServiceImpl impl List studentUsernames = studentCoursesMapper.selectBatchSomeStudent(courseId); List courseLearningRecordList = new ArrayList<>(); for (StudentCourses studentUsername : studentUsernames) { - CourseLearningRecord courseLearningRecord = courseLearningRecordMapper.selectNewRecord(studentUsername.getStudent()); + CourseLearningRecord courseLearningRecord = courseLearningRecordMapper.selectNewRecord(String.valueOf(studentUsername.getStudent())); courseLearningRecordList.add(courseLearningRecord); } courseLearningRecordList = courseLearningRecordList.stream().sorted(Comparator.comparing(CourseLearningRecord::getTimes).reversed()).collect(Collectors.toList()); @@ -396,6 +400,8 @@ public class CoursesServiceImpl extends ServiceImpl impl throw new BusinessException(400,"只有老师才有此功能"); } + + @Override public List getPagePageSize(int page, int pageSize) { int startIndex = (page-1) * pageSize; @@ -610,6 +616,155 @@ public class CoursesServiceImpl extends ServiceImpl impl document.write(outputStream); } + @Override + public List getUserInfByCourse(Long userId) { + UmsUser umsUser = umsUserMapper.selectById(userId); + if (umsUser == null){ + throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); + } + int roleId = Integer.parseInt(umsUser.getRoleId()); + List courseUserInfVO; + switch (roleId) { + //教师 + case 1: + courseUserInfVO = queryCourseUserForTeacher(userId); + break; + //学生 + case 2: + courseUserInfVO = queryCourseUserForStudent(userId); + break; + default: + throw new BusinessException(ErrorCode.INVALID_ROLE); + } + return courseUserInfVO; + } + + private List queryCourseUserForTeacher(Long userId) { + // 查询所有与该教师相关的课程 + List teacherCoursesInf = coursesMapper.selectList( + new LambdaQueryWrapper() + .apply("FIND_IN_SET({0}, teacher)", userId) + .select(Courses::getId, Courses::getTeacher) + ); + + List courseUserInfVOS = new ArrayList<>(); + for (Courses courses : teacherCoursesInf) { + CourseUserInfVO courseUserInfVO = new CourseUserInfVO(); + List userReadMessageTimeVOS = new ArrayList<>(); + courseUserInfVO.setCourseId(courses.getId()); + + // 查询与课程相关的所有学生 + List 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); + } + UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象 + userReadMessageTimeVO.setId(studentId); + userReadMessageTimeVO.setReceiverName(umsStudent.getName()); + userReadMessageTimeVOS.add(userReadMessageTimeVO); + } + + // 为每个教师创建一个新的 UserReadMessageTimeVO 对象 + String[] teacherUserIds = courses.getTeacher().split(","); + for (String teacherId : teacherUserIds) { + UmsTeacher umsTeacher = umsTeacherMapper.selectOne( + new LambdaQueryWrapper() + .eq(UmsTeacher::getUserId, teacherId) + ); + if (umsTeacher == null) { + 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); + courseUserInfVOS.add(courseUserInfVO); + } + return courseUserInfVOS; + } + + + private List queryCourseUserForStudent(Long userId) { + // 查询学生所参与的所有课程 + List studentCoursesList = studentCoursesMapper.selectList( + new LambdaQueryWrapper() + .eq(StudentCourses::getStudent, userId) + .select(StudentCourses::getCourse) + ); + + List courseUserInfVOS = new ArrayList<>(); + for (StudentCourses studentCourse : studentCoursesList) { + CourseUserInfVO courseUserInfVO = new CourseUserInfVO(); + List userReadMessageTimeVOS = new ArrayList<>(); + courseUserInfVO.setCourseId(studentCourse.getCourse()); + + // 查询与课程相关的所有学生 + List 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); + 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() + .eq(UmsTeacher::getUserId, teacherId) + ); + if (umsTeacher == null) { + 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); + } + } else { + log.error("课程不存在: {}", studentCourse.getCourse()); + throw new BusinessException(ErrorCode.PARAMS_COURSE_NOTEXISTS); + } + + courseUserInfVO.setUsersInf(userReadMessageTimeVOS); + courseUserInfVOS.add(courseUserInfVO); + } + return courseUserInfVOS; + } } diff --git a/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java index 4a92433..4caa06f 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java @@ -53,7 +53,7 @@ public class StudentCoursesServiceImpl extends ServiceImpl转换为PageDTO -// PageDTO messages = PageDTO.of(p, sysMessages -> { -// SysMessageVO vo = new SysMessageVO(); -// BeanUtil.copyProperties(sysMessages, vo, CopyOptions.create().setIgnoreProperties(SysMessageVO::getSenderId)); -// return vo; -// }); PageDTO messages = PageDTO.of(p, SysMessageVO.class); - // 获取PageDTO中的SysMessageVO列表 List messagesList = messages.getList(); if (messagesList.isEmpty()) { throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT); } - // 遍历每个消息以填充附加数据 - for (SysMessageVO sysMessageVO : messagesList) { - // 设置课程名称 - if (!StringUtils.isEmpty(sysMessageVO.getCourseId())) { - Courses courses = coursesMapper.selectById(sysMessageVO.getCourseId()); - if (courses != null) { - sysMessageVO.setCourseName(courses.getName()); - } - } + // 批量查询课程名称 + Set courseIds = messagesList.stream() + .map(SysMessageVO::getCourseId) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); - // 查询UserInbox以获取当前消息的所有收件人数量 - Long totalReceiverCount = userInboxMapper.selectCount( - new LambdaQueryWrapper() - .eq(UserInbox::getMessageId, sysMessageVO.getId()) - ); + Map courseNames = coursesMapper.selectBatchIds(courseIds).stream() + .collect(Collectors.toMap(Courses::getId, Courses::getName)); - sysMessageVO.setUserNum(totalReceiverCount); // 设置收信总人数 + // 批量查询所有用户收件箱数据 + Set messageIds = messagesList.stream() + .map(SysMessageVO::getId) + .collect(Collectors.toSet()); - // 查询前5个UserInbox以获取当前消息的收件人列表 - List userInboxes = userInboxMapper.selectList( - new LambdaQueryWrapper() - .eq(UserInbox::getMessageId, sysMessageVO.getId()) - .select(UserInbox::getReceiverId, UserInbox::getIsRead) - .last("LIMIT 5") - ); + List allUserInboxes = userInboxMapper.selectList( + new LambdaQueryWrapper() + .in(UserInbox::getMessageId, messageIds) + .select(UserInbox::getMessageId, UserInbox::getReceiverId, UserInbox::getIsRead) + ); - // 设置收件用户集合 - List receiveUsers = userInboxes.stream().map(userInbox -> { - UserReadMessageTimeVO vo = new UserReadMessageTimeVO(); - vo.setId(userInbox.getReceiverId()); + // 将UserInbox数据按消息ID进行分组 + Map> userInboxesByMessageId = allUserInboxes.stream() + .collect(Collectors.groupingBy(UserInbox::getMessageId)); - UmsStudent umsStudent = umsStudentMapper.selectOne( - new LambdaQueryWrapper() - .eq(UmsStudent::getUserId, userInbox.getReceiverId())); - if (umsStudent == null) { - vo.setReceiverName(umsTeacherMapper.selectOne( - new LambdaQueryWrapper() - .eq(UmsTeacher::getUserId, userInbox.getReceiverId()) - ).getName()); - } else { - vo.setReceiverName(umsStudent.getName()); - } - return vo; - }).collect(Collectors.toList()); - sysMessageVO.setReceiveUsers(receiveUsers); // 设置收件人信息 + // 缓存用户名称以避免重复查询 + Map userNamesCache = new HashMap<>(); - // 计算已读用户人数 - long readUserCount = userInboxes.stream() - .filter(UserInbox::getIsRead) - .count(); - sysMessageVO.setReadUserNum(readUserCount); + for (SysMessageVO sysMessageVO : messagesList) { + String courseId = sysMessageVO.getCourseId(); + if (courseId != null) { + sysMessageVO.setCourseName(courseNames.get(courseId)); + } + + List userInboxes = userInboxesByMessageId.get(sysMessageVO.getId()); + if (userInboxes != null) { + sysMessageVO.setUserNum((long) userInboxes.size()); + sysMessageVO.setReadUserNum(userInboxes.stream().filter(UserInbox::getIsRead).count()); + + List receiveUsers = userInboxes.stream().limit(5) + .map(userInbox -> { + UserReadMessageTimeVO vo = new UserReadMessageTimeVO(); + Long receiverId = userInbox.getReceiverId(); + vo.setId(receiverId); + + // 获取或缓存接收者名称 + String receiverName = userNamesCache.computeIfAbsent(receiverId, id -> { + UmsStudent umsStudent = umsStudentMapper.selectOne( + new LambdaQueryWrapper().eq(UmsStudent::getUserId, id) + ); + if (umsStudent != null) { + return umsStudent.getName(); + } else { + UmsTeacher umsTeacher = umsTeacherMapper.selectOne( + new LambdaQueryWrapper().eq(UmsTeacher::getUserId, id) + ); + return umsTeacher != null ? umsTeacher.getName() : null; + } + }); + + vo.setReceiverName(receiverName); + return vo; + }).collect(Collectors.toList()); + + sysMessageVO.setReceiveUsers(receiveUsers); + } } // 更新PageDTO中的消息列表并返回 @@ -213,25 +219,12 @@ public class UserInboxServiceImpl extends ServiceImpl