Commit cce8401b authored by 刘基明's avatar 刘基明

递归改造队列

parent 887a46c8
...@@ -34,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -34,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayDeque;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -233,17 +234,20 @@ public class CommentManager { ...@@ -233,17 +234,20 @@ public class CommentManager {
if (StringUtils.isBlank(commentId)) { if (StringUtils.isBlank(commentId)) {
throw new BizException("commentId不能为空"); throw new BizException("commentId不能为空");
} }
ArrayDeque<CommentEntity> queue = new ArrayDeque<>();
queue.add(commentService.queryByIdIncludeDelete(commentId));
// 循环删除二级评论的一级评论 // 循环删除二级评论的一级评论
while (StringUtils.isNotBlank(commentId)) { while (!queue.isEmpty()) {
CommentEntity pop = queue.pop();
// 逻辑删除comment // 逻辑删除comment
CommentEntity commentEntity = commentService.delete(commentId, userId); CommentEntity commentEntity = commentService.delete(pop.getCommentId(), pop.getAuthorId());
// 删除站内信 // 删除站内信
ThemeEntity themeEntity = themeService.queryByThemeId(commentEntity.getThemeId()); ThemeEntity themeEntity = themeService.queryByThemeId(commentEntity.getThemeId());
notificationService.deleteCommentNotify(themeEntity.getAuthorId(), userId, commentId, commentEntity.getCreateTime()); notificationService.deleteCommentNotify(themeEntity.getAuthorId(), userId, commentId, commentEntity.getCreateTime());
// 递归 // 递归
CommentEntity replyForComment =commentService.queryReplyForComment(commentId); queue.addAll(commentService.queryReplyForComment(commentEntity.getCommentId()));
commentId = replyForComment.getCommentId();
userId = replyForComment.getAuthorId();
} }
} }
......
...@@ -230,14 +230,10 @@ public class CommentService { ...@@ -230,14 +230,10 @@ public class CommentService {
* @param commentId * @param commentId
* @return * @return
*/ */
public CommentEntity queryReplyForComment(String commentId) { public List<CommentEntity> queryReplyForComment(String commentId) {
List<CommentEntity> replies = commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>() List<CommentEntity> replies = commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>()
.eq(CommentEntity::getReplyId, commentId) .eq(CommentEntity::getReplyId, commentId)
.eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)); .eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
if (CollectionUtils.isNotEmpty(replies)){ return replies;
return replies.get(0);
}else {
return new CommentEntity();
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment