Commit e1b7d7a4 by 胡学良

优化相关问答

parent fe724e60
......@@ -2,6 +2,11 @@ package cn.meteor.beyondclouds.modules.question.mapper;
import cn.meteor.beyondclouds.modules.question.entity.QuestionTag;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* <p>
......@@ -13,4 +18,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/
public interface QuestionTagMapper extends BaseMapper<QuestionTag> {
/**
* 通过标签ID集合获取相关的问题ID
* @param page 分页
* @param tagIds 标签ID集合
* @return 问题ID的分页信息
*/
IPage<String> relevantQuestionIdPage(IPage<String> page, @Param("tagIds") List<String> tagIds);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.meteor.beyondclouds.modules.question.mapper.QuestionTagMapper">
<select id="relevantQuestionIdPage" resultType="String">
select
DISTINCT(question_id)
from (
select
*
from question_tag
where tag_id in
<foreach collection="tagIds" item="tagId" index="index" open="(" close=")" separator=",">
#{tagId}
</foreach>
) qt
</select>
</mapper>
......@@ -31,8 +31,9 @@ public interface IQuestionTagService extends IService<QuestionTag> {
/**
* 根据标签ID集合查询相关问题
* @param page 分页信息
* @param tagIds 标签ID集合
* @return 相关问题ID
* @return 相关问题ID分页
*/
List<String> getQuestionsByTagIds(List<String> tagIds);
IPage<String> getQuestionsByTagIds(IPage<String> page, List<String> tagIds);
}
......@@ -427,26 +427,34 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
throw new QuestionServiceException(QuestionErrorCode.QUESTION_NOT_FOUND);
}
IPage<Question> page = new Page<>(pageNumber, pageSize);
IPage<String> tagIdsPage = new Page<>(pageNumber, pageSize);
IPage<Question> questionPage = new Page<>(pageNumber, pageSize);
//2.得到问题对应的tagIds
List<String> tagIds = questionTagService.list(QuestionUtils.getWrapper("question_id", question.getQuestionId()))
.stream().map(QuestionTag::getTagId).collect(Collectors.toList());
//如果问题引用标签不为空,则通过tagIds得到相关问答,否则,直接返回page
//3.判断tagIds是否为空
if (!CollectionUtils.isEmpty(tagIds)) {
//得到相关问答的问题ID
List<String> questionIds = questionTagService.getQuestionsByTagIds(tagIds);
questionTagService.getQuestionsByTagIds(tagIdsPage, tagIds);
//通过问题ID得到问题的具体信息
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.in("q.question_id", questionIds)
.orderByDesc("q.create_time");
questionPage.setTotal(tagIdsPage.getTotal());
questionPage.setCurrent(tagIdsPage.getCurrent());
return questionMapper.selectPageWithTags(page, questionQueryWrapper);
List<String> questionIds = tagIdsPage.getRecords();
//判断questionIds的长度是否为1,若为1,则说明questionIds只有传入的questionId,直接返回空
if (questionIds.size() != 1) {
//移除自身ID
questionIds.remove(question.getQuestionId());
questionPage.setRecords(questionMapper.listByIdsWithTags(questionIds));
}else {
return page;
questionPage.setRecords(List.of());
}
}else {
questionPage.setRecords(List.of());
}
return questionPage;
}
}
......@@ -41,10 +41,13 @@ public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, Quest
private QuestionMapper questionMapper;
private QuestionTagMapper questionTagMapper;
@Autowired
public QuestionTagServiceImpl(ITagService tagService, QuestionMapper questionMapper) {
public QuestionTagServiceImpl(ITagService tagService, QuestionMapper questionMapper, QuestionTagMapper questionTagMapper) {
this.tagService = tagService;
this.questionMapper = questionMapper;
this.questionTagMapper = questionTagMapper;
}
@Autowired
......@@ -78,9 +81,7 @@ public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, Quest
}
@Override
public List<String> getQuestionsByTagIds(List<String> tagIds) {
QueryWrapper<QuestionTag> questionTagQueryWrapper = new QueryWrapper<>();
questionTagQueryWrapper.in("tag_id", tagIds);
return list(questionTagQueryWrapper).stream().map(QuestionTag::getQuestionId).collect(Collectors.toList());
public IPage<String> getQuestionsByTagIds(IPage<String> page, List<String> tagIds) {
return questionTagMapper.relevantQuestionIdPage(page, tagIds);
}
}
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