Commit e1b7d7a4 by 胡学良

优化相关问答

parent fe724e60
...@@ -2,6 +2,11 @@ package cn.meteor.beyondclouds.modules.question.mapper; ...@@ -2,6 +2,11 @@ package cn.meteor.beyondclouds.modules.question.mapper;
import cn.meteor.beyondclouds.modules.question.entity.QuestionTag; import cn.meteor.beyondclouds.modules.question.entity.QuestionTag;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; 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> * <p>
...@@ -13,4 +18,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; ...@@ -13,4 +18,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/ */
public interface QuestionTagMapper extends BaseMapper<QuestionTag> { 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"?> <?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"> <!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"> <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> </mapper>
...@@ -31,8 +31,9 @@ public interface IQuestionTagService extends IService<QuestionTag> { ...@@ -31,8 +31,9 @@ public interface IQuestionTagService extends IService<QuestionTag> {
/** /**
* 根据标签ID集合查询相关问题 * 根据标签ID集合查询相关问题
* @param page 分页信息
* @param tagIds 标签ID集合 * @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 ...@@ -427,26 +427,34 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
throw new QuestionServiceException(QuestionErrorCode.QUESTION_NOT_FOUND); 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 //2.得到问题对应的tagIds
List<String> tagIds = questionTagService.list(QuestionUtils.getWrapper("question_id", question.getQuestionId())) List<String> tagIds = questionTagService.list(QuestionUtils.getWrapper("question_id", question.getQuestionId()))
.stream().map(QuestionTag::getTagId).collect(Collectors.toList()); .stream().map(QuestionTag::getTagId).collect(Collectors.toList());
//如果问题引用标签不为空,则通过tagIds得到相关问答,否则,直接返回page //3.判断tagIds是否为空
if (!CollectionUtils.isEmpty(tagIds)) { if (!CollectionUtils.isEmpty(tagIds)) {
//得到相关问答的问题ID questionTagService.getQuestionsByTagIds(tagIdsPage, tagIds);
List<String> questionIds = questionTagService.getQuestionsByTagIds(tagIds);
//通过问题ID得到问题的具体信息 questionPage.setTotal(tagIdsPage.getTotal());
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>(); questionPage.setCurrent(tagIdsPage.getCurrent());
questionQueryWrapper.in("q.question_id", questionIds)
.orderByDesc("q.create_time");
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 { }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 ...@@ -41,10 +41,13 @@ public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, Quest
private QuestionMapper questionMapper; private QuestionMapper questionMapper;
private QuestionTagMapper questionTagMapper;
@Autowired @Autowired
public QuestionTagServiceImpl(ITagService tagService, QuestionMapper questionMapper) { public QuestionTagServiceImpl(ITagService tagService, QuestionMapper questionMapper, QuestionTagMapper questionTagMapper) {
this.tagService = tagService; this.tagService = tagService;
this.questionMapper = questionMapper; this.questionMapper = questionMapper;
this.questionTagMapper = questionTagMapper;
} }
@Autowired @Autowired
...@@ -78,9 +81,7 @@ public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, Quest ...@@ -78,9 +81,7 @@ public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, Quest
} }
@Override @Override
public List<String> getQuestionsByTagIds(List<String> tagIds) { public IPage<String> getQuestionsByTagIds(IPage<String> page, List<String> tagIds) {
QueryWrapper<QuestionTag> questionTagQueryWrapper = new QueryWrapper<>(); return questionTagMapper.relevantQuestionIdPage(page, tagIds);
questionTagQueryWrapper.in("tag_id", tagIds);
return list(questionTagQueryWrapper).stream().map(QuestionTag::getQuestionId).collect(Collectors.toList());
} }
} }
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