Skip to content

Commit

Permalink
Ref: 全站评论内容列表代码重构(先实现,再优化的)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangrunkang committed Jan 9, 2023
1 parent 8c97846 commit 34b2884
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.upupor.framework.thread;

import java.util.concurrent.ForkJoinPool;

/**
* UpuporForkJoin
*
* @author Yang Runkang (cruise)
* @date 1/9/23 15:39
*/
public class UpuporForkJoin {
static final ForkJoinPool commentListForkJoin = new ForkJoinPool();

public static ForkJoinPool commentListForkJoin() {
return commentListForkJoin;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,24 @@

package com.upupor.service.aggregation;

import com.upupor.data.dao.entity.Comment;
import com.upupor.data.dao.entity.Content;
import com.upupor.data.dao.entity.Member;
import com.upupor.data.dao.entity.Radio;
import com.upupor.data.dao.entity.enhance.CommentEnhance;
import com.upupor.data.dao.entity.enhance.ContentEnhance;
import com.upupor.data.dao.entity.enhance.MemberEnhance;
import com.upupor.data.dto.page.CommentIndexDto;
import com.upupor.data.dto.page.comment.CommentDto;
import com.upupor.data.dto.page.common.ListCommentDto;
import com.upupor.data.dto.query.ListCommentQuery;
import com.upupor.data.types.CommentStatus;
import com.upupor.data.types.ContentStatus;
import com.upupor.data.types.ContentType;
import com.upupor.framework.BusinessException;
import com.upupor.framework.ErrorCode;
import com.upupor.framework.thread.UpuporForkJoin;
import com.upupor.service.base.CommentService;
import com.upupor.service.base.ContentService;
import com.upupor.service.base.MemberService;
import com.upupor.service.base.RadioService;
import com.upupor.service.business.comment.list.abstracts.AbstractCommentList;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.concurrent.CompletableFuture;


/**
Expand All @@ -70,9 +58,8 @@
@RequiredArgsConstructor
public class CommentAggregateService {
private final CommentService commentService;
private final ContentService contentService;
private final RadioService radioService;
private final MemberService memberService;
@Resource
private List<AbstractCommentList<?>> abstractCommentListList;

public CommentIndexDto index(Integer pageNum, Integer pageSize) {
CommentIndexDto commentIndexDto = new CommentIndexDto();
Expand All @@ -86,79 +73,16 @@ public CommentIndexDto index(Integer pageNum, Integer pageSize) {
ListCommentDto listCommentDto = commentService.listComment(listCommentQuery);
List<CommentEnhance> commentEnhanceList = listCommentDto.getCommentEnhanceList();

List<String> contentIdList =
commentEnhanceList.stream()
.map(CommentEnhance::getComment)
.filter(s -> ContentType.isContent(s.getCommentSource()))
.map(Comment::getTargetId)
.collect(Collectors.toList());

List<String> messageBoardUserIdList =
commentEnhanceList.stream()
.map(CommentEnhance::getComment)
.filter(s -> ContentType.isMessagetBoard(s.getCommentSource()))
.map(Comment::getTargetId)
.collect(Collectors.toList());

List<String> radioIdList =
commentEnhanceList.stream()
.map(CommentEnhance::getComment)
.filter(s -> ContentType.isRadio(s.getCommentSource()))
.map(Comment::getTargetId)
.collect(Collectors.toList());

Map<String, Content> contentMap = new HashMap<>();
if (!CollectionUtils.isEmpty(contentIdList)) {
List<ContentEnhance> contentEnhances = contentService.listByContentIdList(contentIdList);
contentEnhances.stream()
.map(ContentEnhance::getContent)
.filter(content -> ContentStatus.NORMAL.equals(content.getStatus()))
.forEach(s -> contentMap.putIfAbsent(s.getContentId(), s))
;
}

Map<String, Radio> radioMap = new HashMap<>();
if (!CollectionUtils.isEmpty(radioIdList)) {
List<Radio> radios = radioService.listByRadioId(radioIdList);
radios.forEach(s -> radioMap.putIfAbsent(s.getRadioId(), s));
}

Map<String, Member> memberMap = new HashMap<>();
if (!CollectionUtils.isEmpty(messageBoardUserIdList)) {
List<MemberEnhance> memberEnhances = memberService.listByUserIdList(messageBoardUserIdList);
memberEnhances.stream()
.map(MemberEnhance::getMember)
.forEach(s -> memberMap.putIfAbsent(s.getUserId(), s))
;
}

List<CommentDto> commentDtoList = new ArrayList<>();
if (!CollectionUtils.isEmpty(commentEnhanceList)) {
for (CommentEnhance commentEnhance : commentEnhanceList) {
Comment comment = commentEnhance.getComment();
ContentType commentSource = comment.getCommentSource();
String targetId = comment.getTargetId();

if (ContentType.isContent(commentSource)) {
Content content = contentMap.get(targetId);
commentDtoList.add(CommentDto.create(comment.getCommentContent(),
"/u/" + content.getContentId(), content.getTitle(), commentEnhance
));
} else if (ContentType.isMessagetBoard(commentSource)) {
Member member = memberMap.get(targetId);
commentDtoList.add(CommentDto.create(comment.getCommentContent(),
"/profile/" + member.getUserId() + "/message", member.getUserName(), commentEnhance
));
} else if (ContentType.isRadio(commentSource)) {
Radio radio = radioMap.get(targetId);
commentDtoList.add(CommentDto.create(comment.getCommentContent(),
"/r/" + radio.getRadioId(),
radio.getRadioIntro(), commentEnhance
));
} else {
throw new BusinessException(ErrorCode.UNKNOWN_TYPE_CONTENT);
}
List<CompletableFuture<Void>> voidCompletableFutureList = new ArrayList<>();
for (AbstractCommentList<?> abstractCommentList : abstractCommentListList) {
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
commentDtoList.addAll(abstractCommentList.handle(commentEnhanceList));
}, UpuporForkJoin.commentListForkJoin());
voidCompletableFutureList.add(voidCompletableFuture);
}
CompletableFuture.allOf(voidCompletableFutureList.toArray(new CompletableFuture[0])).join();
}

commentIndexDto.setCommentDtoList(commentDtoList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import com.upupor.service.base.ContentService;
import com.upupor.service.base.MemberIntegralService;
import com.upupor.service.base.MemberService;
import com.upupor.service.business.comment.AbstractComment;
import com.upupor.service.business.comment.comment.abstracts.AbstractComment;
import com.upupor.service.business.message.MessageSend;
import com.upupor.service.business.message.model.MessageModel;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.upupor.framework.common.IntegralEnum;
import com.upupor.service.base.MemberIntegralService;
import com.upupor.service.base.MemberService;
import com.upupor.service.business.comment.AbstractComment;
import com.upupor.service.business.comment.comment.abstracts.AbstractComment;
import com.upupor.service.business.message.MessageSend;
import com.upupor.service.business.message.model.MessageModel;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.upupor.framework.utils.CcDateUtil;
import com.upupor.service.base.MemberService;
import com.upupor.service.base.RadioService;
import com.upupor.service.business.comment.AbstractComment;
import com.upupor.service.business.comment.comment.abstracts.AbstractComment;
import com.upupor.service.business.message.MessageSend;
import com.upupor.service.business.message.model.MessageModel;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* SOFTWARE.
*/

package com.upupor.service.business.comment;
package com.upupor.service.business.comment.comment.abstracts;

import com.upupor.data.dao.entity.enhance.MemberEnhance;
import com.upupor.data.types.ContentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* MIT License
*
* Copyright (c) 2021-2022 yangrunkang
*
* Author: yangrunkang
* Email: [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.upupor.service.business.comment.list;

import com.upupor.data.dao.entity.Comment;
import com.upupor.data.dao.entity.Content;
import com.upupor.data.dao.entity.enhance.CommentEnhance;
import com.upupor.data.dao.entity.enhance.ContentEnhance;
import com.upupor.data.dto.page.comment.CommentDto;
import com.upupor.data.types.ContentStatus;
import com.upupor.data.types.ContentType;
import com.upupor.service.base.ContentService;
import com.upupor.service.business.comment.list.abstracts.AbstractCommentList;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Yang Runkang (cruise)
* @date 2023年01月09日 15:01
* @email: [email protected]
*/
@Component
public class ContentCommentList extends AbstractCommentList<Content> {

@Resource
private ContentService contentService;

@Override
protected Boolean isFit(ContentType commentSource) {
return ContentType.isContent(commentSource);
}

@Override
protected List<String> filterTargetIdList() {
return commentEnhanceList.stream()
.map(CommentEnhance::getComment)
.filter(s -> ContentType.isContent(s.getCommentSource()))
.map(Comment::getTargetId)
.collect(Collectors.toList());
}

@Override
protected Map<String, Content> filteredIdConvertToTMap(List<String> filteredIdList) {
Map<String, Content> contentMap = new HashMap<>();
List<ContentEnhance> contentEnhances = contentService.listByContentIdList(filteredIdList);
contentEnhances.stream()
.map(ContentEnhance::getContent)
.filter(content -> ContentStatus.NORMAL.equals(content.getStatus()))
.forEach(s -> contentMap.putIfAbsent(s.getContentId(), s))
;
return contentMap;
}

@Override
protected void createCommentDto(Map<String, Content> contentMap, CommentEnhance commentEnhance) {
Comment comment = commentEnhance.getComment();
String targetId = comment.getTargetId();

Content content = contentMap.get(targetId);
commentDtoList.add(CommentDto.create(comment.getCommentContent(),
"/u/" + content.getContentId(), content.getTitle(), commentEnhance
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* MIT License
*
* Copyright (c) 2021-2022 yangrunkang
*
* Author: yangrunkang
* Email: [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.upupor.service.business.comment.list;

import com.upupor.data.dao.entity.Comment;
import com.upupor.data.dao.entity.Member;
import com.upupor.data.dao.entity.enhance.CommentEnhance;
import com.upupor.data.dao.entity.enhance.MemberEnhance;
import com.upupor.data.dto.page.comment.CommentDto;
import com.upupor.data.types.ContentType;
import com.upupor.service.base.MemberService;
import com.upupor.service.business.comment.list.abstracts.AbstractCommentList;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Yang Runkang (cruise)
* @date 2023年01月09日 15:01
* @email: [email protected]
*/
@Component
public class MemberBoardCommentList extends AbstractCommentList<Member> {
@Resource
private MemberService memberService;

@Override
protected Boolean isFit(ContentType commentSource) {
return ContentType.isMessagetBoard(commentSource);
}

@Override
protected List<String> filterTargetIdList() {
return commentEnhanceList.stream()
.map(CommentEnhance::getComment)
.filter(s -> ContentType.isMessagetBoard(s.getCommentSource()))
.map(Comment::getTargetId)
.collect(Collectors.toList());
}

@Override
protected void createCommentDto(Map<String, Member> memberMap, CommentEnhance commentEnhance) {
Comment comment = commentEnhance.getComment();
String targetId = comment.getTargetId();

Member member = memberMap.get(targetId);
commentDtoList.add(CommentDto.create(comment.getCommentContent(),
"/profile/" + member.getUserId() + "/message", member.getUserName(), commentEnhance
));
}

@Override
protected Map<String, Member> filteredIdConvertToTMap(List<String> filteredIdList) {
Map<String, Member> memberMap = new HashMap<>();
List<MemberEnhance> memberEnhances = memberService.listByUserIdList(filteredIdList);
memberEnhances.stream()
.map(MemberEnhance::getMember)
.forEach(s -> memberMap.putIfAbsent(s.getUserId(), s))
;
return memberMap;
}
}
Loading

0 comments on commit 34b2884

Please sign in to comment.