Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beyond-clouds
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
段启岩
beyond-clouds
Commits
e1b7d7a4
Commit
e1b7d7a4
authored
Feb 08, 2020
by
胡学良
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化相关问答
parent
fe724e60
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
19 deletions
+53
-19
src/main/java/cn/meteor/beyondclouds/modules/question/mapper/QuestionTagMapper.java
+12
-0
src/main/java/cn/meteor/beyondclouds/modules/question/mapper/xml/QuestionTagMapper.xml
+13
-1
src/main/java/cn/meteor/beyondclouds/modules/question/service/IQuestionTagService.java
+3
-2
src/main/java/cn/meteor/beyondclouds/modules/question/service/impl/QuestionServiceImpl.java
+19
-11
src/main/java/cn/meteor/beyondclouds/modules/question/service/impl/QuestionTagServiceImpl.java
+6
-5
No files found.
src/main/java/cn/meteor/beyondclouds/modules/question/mapper/QuestionTagMapper.java
View file @
e1b7d7a4
...
@@ -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
);
}
}
src/main/java/cn/meteor/beyondclouds/modules/question/mapper/xml/QuestionTagMapper.xml
View file @
e1b7d7a4
<?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>
src/main/java/cn/meteor/beyondclouds/modules/question/service/IQuestionTagService.java
View file @
e1b7d7a4
...
@@ -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
);
}
}
src/main/java/cn/meteor/beyondclouds/modules/question/service/impl/QuestionServiceImpl.java
View file @
e1b7d7a4
...
@@ -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
{
questionPage
.
setRecords
(
List
.
of
());
}
}
else
{
}
else
{
return
page
;
questionPage
.
setRecords
(
List
.
of
())
;
}
}
return
questionPage
;
}
}
}
}
src/main/java/cn/meteor/beyondclouds/modules/question/service/impl/QuestionTagServiceImpl.java
View file @
e1b7d7a4
...
@@ -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
());
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment