diff --git a/src/main/java/com/teaching/backend/common/ErrorCode.java b/src/main/java/com/teaching/backend/common/ErrorCode.java index 4883d57..041682c 100644 --- a/src/main/java/com/teaching/backend/common/ErrorCode.java +++ b/src/main/java/com/teaching/backend/common/ErrorCode.java @@ -26,7 +26,8 @@ public enum ErrorCode { NO_AUTH_ERROR(40101, "无权限"), NOT_FOUND_ERROR(40400, "请求数据不存在"), CONTENT_EXISTS(41000, "内容存在"), - + STUDENT_ROLE_CANT_MULTIPLESEND(41001, "学生用户不能群发,只能单发"), + LEAST_ONE_USER(41002, "最少选择一个用户"), KNOWS_EXISTS(41001, "存在关联的知识点"), ADD_RELATION_FAILED(41002, "添加关系失败"), diff --git a/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java index 6205d54..6b7b2c4 100644 --- a/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java @@ -7,9 +7,11 @@ import com.teaching.backend.common.ErrorCode; import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.message.MessagesMapper; import com.teaching.backend.mapper.message.UserInboxMapper; +import com.teaching.backend.mapper.umsAdmin.UmsUserMapper; import com.teaching.backend.model.dto.message.SysMessageDTO; import com.teaching.backend.model.entity.message.SysMessages; import com.teaching.backend.model.entity.message.UserInbox; +import com.teaching.backend.model.entity.umsAdmin.UmsUser; import com.teaching.backend.service.message.IMessagesService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; @@ -38,6 +40,8 @@ public class MessagesServiceImpl extends ServiceImpl receiverIds = Arrays.stream(sysMessageDTO.getReceiverId().split(",")) .map(Long::valueOf) .collect(Collectors.toList()); + if (receiverIds.size()<1){ + throw new BusinessException(ErrorCode.LEAST_ONE_USER); + } + + // 4.判断是什么用户,只有教师可以群发,学生只能单发 + String roleId = umsUserMapper.selectOne(new LambdaQueryWrapper<>(UmsUser.class) + .eq(UmsUser::getId, sysMessageDTO.getSenderId())).getRoleId(); + if(roleId.equals("2")){ + if (receiverIds.size()>1){ + throw new BusinessException(ErrorCode.STUDENT_ROLE_CANT_MULTIPLESEND); + } + } - // 4.数据写入收件箱 + // 5.数据写入收件箱 List userInboxes = receiverIds.stream() .map(receiverId -> { UserInbox userInbox = new UserInbox(); @@ -68,7 +84,7 @@ public class MessagesServiceImpl extends ServiceImpl querySendMessages(MessagesQuery messagesQuery) { - // 初始化一个按发送时间降序排序的Page对象 + // 1. 初始化一个按发送时间降序排序的Page对象 Page page = messagesQuery.toMpPage("send_time", false); - // 根据条件查询SysMessages表 + // 2. 根据条件查询SysMessages表 Page p = messagesService.page(page, new LambdaQueryWrapper() .eq(SysMessages::getSenderId, messagesQuery.getUserId()) @@ -137,7 +137,7 @@ public class UserInboxServiceImpl extends ServiceImpl转换为PageDTO + // 3. 将Page转换为PageDTO PageDTO messages = PageDTO.of(p, SysMessageVO.class); List messagesList = messages.getList(); @@ -145,31 +145,33 @@ public class UserInboxServiceImpl extends ServiceImpl courseIds = messagesList.stream() .map(SysMessageVO::getCourseId) .filter(Objects::nonNull) .collect(Collectors.toSet()); + // 5. 批量查询课程ID对应的课程名称 并把ID:name 对应到一起 Map courseNames = coursesMapper.selectBatchIds(courseIds).stream() .collect(Collectors.toMap(Courses::getId, Courses::getName)); - // 批量查询所有用户收件箱数据 + // 6. 批量查询当前用户对应的消息集合messageIds Set messageIds = messagesList.stream() .map(SysMessageVO::getId) .collect(Collectors.toSet()); + // 在用户邮箱中批量查询 对应消息的 信息(消息id,接受者id,是否已读) List allUserInboxes = userInboxMapper.selectList( new LambdaQueryWrapper() .in(UserInbox::getMessageId, messageIds) .select(UserInbox::getMessageId, UserInbox::getReceiverId, UserInbox::getIsRead) ); - // 将UserInbox数据按消息ID进行分组 + // 7. 将UserInbox数据按消息ID进行分组(已经初步得到了消息的详情) Map> userInboxesByMessageId = allUserInboxes.stream() .collect(Collectors.groupingBy(UserInbox::getMessageId)); - // 缓存用户名称以避免重复查询 + // 8. 缓存用户名称以避免重复查询 Map userNamesCache = new HashMap<>(); for (SysMessageVO sysMessageVO : messagesList) { @@ -200,7 +202,7 @@ public class UserInboxServiceImpl extends ServiceImpl().eq(UmsTeacher::getUserId, id) ); - return umsTeacher != null ? umsTeacher.getName() : null; + return umsTeacher != null ? umsTeacher.getName() : "教师不存在"; } }); @@ -212,20 +214,27 @@ public class UserInboxServiceImpl extends ServiceImpl(UserInbox.class) + .eq(UserInbox::getMessageId, messagesQuery.getMessageId()) + .eq(UserInbox::getReceiverId, messagesQuery.getUserId())); + if (!(count > 0)){ + throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT); + } SysMessages sysMessage = messagesService.getById(messagesQuery.getMessageId()); SysMessageVO sysMessageVO = new SysMessageVO(); sysMessageVO.setId(messagesQuery.getMessageId()); sysMessageVO.setContent(sysMessage.getContent()); sysMessageVO.setSenderId(messagesQuery.getUserId()); -// 获取发件人信息 + //获取发件人信息 UmsTeacher umsTeacher = umsTeacherMapper.selectOne( new LambdaQueryWrapper() .eq(UmsTeacher::getUserId, sysMessage.getSenderId()));