Commit 6af80433 by 胡学良

优化通过标签ID得到问答列表

parent c8e81e9f
......@@ -256,7 +256,7 @@ public class QuestionApi {
}
@Anonymous
@ApiOperation("标签下的问答列表")
@ApiOperation("得到标签下的问答列表")
@GetMapping("/tag/{tagId}/questions")
public Response<PageVO<Question>> getQuestionsByTagId(@Valid PageForm pageForm, @PathVariable("tagId") String tagId) {
try {
......
......@@ -421,25 +421,31 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
@Override
public IPage<Question> getQuestionsByTagId(Integer pageNumber, Integer pageSize, String tagId) throws QuestionTagServiceException {
IPage<Question> page = new Page<>(pageNumber, pageSize);
//1.检查标签是否存在
Tag tag = tagService.getById(tagId);
if (null == tag) {
throw new QuestionTagServiceException(QuestionTagErrorCode.TAG_NOT_FOUND);
}
IPage<Question> questionPage = new Page<>(pageNumber, pageSize);
IPage<QuestionTag> questionTagPage = new Page<>(pageNumber, pageSize);
//2.通过标签ID得到对应的问题ID
List<String> questionIds = questionTagService.list(QuestionUtils.getWrapper("tag_id", tag.getTagId()))
.stream().map(QuestionTag::getQuestionId).collect(Collectors.toList());
QueryWrapper<QuestionTag> questionTagQueryWrapper = new QueryWrapper<>();
questionTagQueryWrapper.eq("tag_id", tag.getTagId());
questionTagService.page(questionTagPage,questionTagQueryWrapper);
List<String> questionIds = questionTagPage.getRecords().stream().map(QuestionTag::getQuestionId).collect(Collectors.toList());
questionPage.setTotal(questionTagPage.getTotal());
questionPage.setCurrent(questionTagPage.getCurrent());
if (!CollectionUtils.isEmpty(questionIds)) {
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.in("q.question_id", questionIds)
.orderByDesc("q.create_time");
return questionMapper.selectPageWithTags(page, questionQueryWrapper);
questionPage.setRecords(questionMapper.listByIdsWithTags(questionIds));
}else {
return page;
questionPage.setRecords(List.of());
}
return questionPage;
}
}
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