|
@@ -1,7 +1,6 @@
|
|
|
package com.sf.book.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.sf.book.dto.inner.InnerBookCommentRespDto;
|
|
|
import com.sf.book.dto.resp.BookCommentRespDto;
|
|
|
import com.sf.book.dto.resp.CommentInfoRespDto;
|
|
|
import com.sf.book.entity.BookComment;
|
|
@@ -10,7 +9,7 @@ import com.sf.book.service.IBookCommentService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.sf.core.dto.RestResp;
|
|
|
import com.sf.user.client.UserFeign;
|
|
|
-import com.sf.user.client.dto.InnerUserCommentRespDto;
|
|
|
+import com.sf.user.client.dto.UserCommentRespDto;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -43,21 +42,26 @@ public class BookCommentServiceImpl extends ServiceImpl<BookCommentMapper, BookC
|
|
|
List<BookComment> bookComments = bookCommentMapper.selectList(queryWrapper);
|
|
|
int count = bookComments.size();
|
|
|
|
|
|
- Set<Long> userSet = new HashSet<>();
|
|
|
- for (BookComment bookComment : bookComments) {
|
|
|
- userSet.add(bookComment.getUserId());
|
|
|
+ if (count == 0) {
|
|
|
+ // 如果当前书籍没有评论 直接返回空
|
|
|
+ return BookCommentRespDto.builder()
|
|
|
+ .commentTotal(0L).comments(new ArrayList<>()).build();
|
|
|
}
|
|
|
|
|
|
- List<CommentInfoRespDto> commentInfoRespDtoList = new ArrayList<>();
|
|
|
- RestResp<List<InnerUserCommentRespDto>> listRestResp = userFeign.listUserInfoByIds(userSet);
|
|
|
- List<InnerUserCommentRespDto> data = listRestResp.getData();
|
|
|
- Map<Long, InnerUserCommentRespDto> collected =
|
|
|
- data.stream().collect(Collectors.toMap(InnerUserCommentRespDto::getUserId, t -> t));
|
|
|
+ // 汇总需要是userId 传递给novel-user的接口 将返回结果映射为map
|
|
|
+ List<Long> userIds = bookComments.stream().map(BookComment::getUserId).toList();
|
|
|
+ RestResp<List<UserCommentRespDto>> listRestResp = userFeign.listUserInfoByIds(userIds);
|
|
|
+ List<UserCommentRespDto> data = listRestResp.getData();
|
|
|
+ Map<Long, UserCommentRespDto> collected =
|
|
|
+ data.stream().collect(Collectors.toMap(UserCommentRespDto::getUserId, t -> t));
|
|
|
|
|
|
+ // 组装数据
|
|
|
+ List<CommentInfoRespDto> commentInfoRespDtoList = new ArrayList<>();
|
|
|
for (BookComment bookComment : bookComments) {
|
|
|
// select * from user_info where id = ''
|
|
|
// UserInfo userInfo = userInfoMapper.selectById(bookComment.getUserId());
|
|
|
- InnerUserCommentRespDto commentRespDto = collected.get(bookComment.getUserId());
|
|
|
+ // 将原来从数据库中拿数据的逻辑 变成从map中拿数据
|
|
|
+ UserCommentRespDto commentRespDto = collected.get(bookComment.getUserId());
|
|
|
CommentInfoRespDto commentInfoRespDto = CommentInfoRespDto.builder()
|
|
|
.id(bookComment.getId()).commentContent(bookComment.getCommentContent())
|
|
|
.commentUserId(bookComment.getUserId())
|