用户发送消息(学生只能单发)

master
Alan 3 months ago
parent ce9cc31736
commit 908d808681
  1. 3
      src/main/java/com/teaching/backend/common/ErrorCode.java
  2. 20
      src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java
  3. 29
      src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.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, "添加关系失败"),

@ -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<MessagesMapper, SysMessages
@Autowired
private UserInboxMapper userInboxMapper;
@Autowired
private UmsUserMapper umsUserMapper;
@Override
@Transactional
public String sendMessageToUserInbox(SysMessageDTO sysMessageDTO) {
@ -51,8 +55,20 @@ public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, SysMessages
List<Long> 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<UserInbox> userInboxes = receiverIds.stream()
.map(receiverId -> {
UserInbox userInbox = new UserInbox();
@ -68,7 +84,7 @@ public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, SysMessages
throw new BusinessException(ErrorCode.OPERATION_ERROR, "收件箱数据插入失败!");
}
// 5.更新或保存消息数据
// 6.更新或保存消息数据
SysMessages sysMessages = new SysMessages();
BeanUtils.copyProperties(sysMessageDTO, sysMessages);
sysMessages.setId(messageId);

@ -125,10 +125,10 @@ public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox
@Override
public PageDTO<SysMessageVO> querySendMessages(MessagesQuery messagesQuery) {
// 初始化一个按发送时间降序排序的Page对象
// 1. 初始化一个按发送时间降序排序的Page对象
Page<SysMessages> page = messagesQuery.toMpPage("send_time", false);
// 根据条件查询SysMessages表
// 2. 根据条件查询SysMessages表
Page<SysMessages> p = messagesService.page(page,
new LambdaQueryWrapper<SysMessages>()
.eq(SysMessages::getSenderId, messagesQuery.getUserId())
@ -137,7 +137,7 @@ public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox
.select(SysMessages::getId, SysMessages::getCourseId, SysMessages::getTitle, SysMessages::getSendTime)
);
// 将Page<SysMessages>转换为PageDTO<SysMessageVO>
// 3. 将Page<SysMessages>转换为PageDTO<SysMessageVO>
PageDTO<SysMessageVO> messages = PageDTO.of(p, SysMessageVO.class);
List<SysMessageVO> messagesList = messages.getList();
@ -145,31 +145,33 @@ public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox
throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT);
}
// 批量查询课程名称
// 4. 批量查询课程ID
Set<String> courseIds = messagesList.stream()
.map(SysMessageVO::getCourseId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
// 5. 批量查询课程ID对应的课程名称 并把ID:name 对应到一起
Map<String, String> courseNames = coursesMapper.selectBatchIds(courseIds).stream()
.collect(Collectors.toMap(Courses::getId, Courses::getName));
// 批量查询所有用户收件箱数据
// 6. 批量查询当前用户对应的消息集合messageIds
Set<Long> messageIds = messagesList.stream()
.map(SysMessageVO::getId)
.collect(Collectors.toSet());
// 在用户邮箱中批量查询 对应消息的 信息(消息id,接受者id,是否已读)
List<UserInbox> allUserInboxes = userInboxMapper.selectList(
new LambdaQueryWrapper<UserInbox>()
.in(UserInbox::getMessageId, messageIds)
.select(UserInbox::getMessageId, UserInbox::getReceiverId, UserInbox::getIsRead)
);
// 将UserInbox数据按消息ID进行分组
// 7. 将UserInbox数据按消息ID进行分组(已经初步得到了消息的详情)
Map<Long, List<UserInbox>> userInboxesByMessageId = allUserInboxes.stream()
.collect(Collectors.groupingBy(UserInbox::getMessageId));
// 缓存用户名称以避免重复查询
// 8. 缓存用户名称以避免重复查询
Map<Long, String> userNamesCache = new HashMap<>();
for (SysMessageVO sysMessageVO : messagesList) {
@ -200,7 +202,7 @@ public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>().eq(UmsTeacher::getUserId, id)
);
return umsTeacher != null ? umsTeacher.getName() : null;
return umsTeacher != null ? umsTeacher.getName() : "教师不存在";
}
});
@ -212,20 +214,27 @@ public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox
}
}
// 更新PageDTO中的消息列表并返回
// 9. 更新PageDTO中的消息列表并返回
messages.setList(messagesList);
return messages;
}
@Override
public SysMessageVO getMessageDetail(MessagesQuery messagesQuery) {
Long count = userInboxMapper.selectCount(new LambdaQueryWrapper<>(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<UmsTeacher>()
.eq(UmsTeacher::getUserId, sysMessage.getSenderId()));

Loading…
Cancel
Save