Java SpringBoot 若依 后端实现评论“盖楼“,“楼中楼“功能 递归查询递归组装评论结构

时间:2024-07-19 07:24:32
/** * 递归查询评论 * */ public List<SubReviewForm> recursiveQuerySubReviewForm(List<String> ids) { List<SubReviewForm> subReviewFormList = subReviewFormMapper.getReviewBySubPublishingParentId(ids); if (CollectionUtils.isEmpty(subReviewFormList)) { subReviewFormList = new ArrayList<>(); } else { // 递归获取楼层 List<String> newIds = subReviewFormList.stream().map(SubReviewForm::getId).collect(Collectors.toList()); subReviewFormList.addAll(recursiveQuerySubReviewForm(newIds)); } return subReviewFormList; } /** * 递归组装评论楼层 * */ private void recursiveAssembleSubReviewForm(List<SubReviewForm> rootList, List<SubReviewForm> subList) { for (SubReviewForm reviewForm : rootList) { for (SubReviewForm subReviewForm : subList) { //一级评论 if (StringUtils.equals(reviewForm.getId(), subReviewForm.getSubPublishingParentId()) || //二级评论以及子评论 StringUtils.equals(reviewForm.getSubPublishingId(), subReviewForm.getSubPublishingParentId())) { reviewForm.getChildren().add(subReviewForm); } } recursiveAssembleSubReviewForm(reviewForm.getChildren(), subList); } }