Skip to content

Commit 62ad3b8

Browse files
committed
feat(recommendations): 添加推荐条目计数功能
1 parent 029f8f8 commit 62ad3b8

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

flow-client/src/main/java/com/ligg/flowclient/mapper/BangumiSubjectMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ List<SubjectRecommendationRow> selectRecommendedSubjects(@Param("subjectId") Int
2828
@Param("metaTagNames") List<String> metaTagNames,
2929
@Param("limit") int limit,
3030
@Param("offset") int offset);
31+
32+
Integer countRecommendedSubjects(@Param("subjectId") Integer subjectId,
33+
@Param("type") Integer type,
34+
@Param("nsfw") Boolean nsfw,
35+
@Param("tagNames") List<String> tagNames,
36+
@Param("metaTagNames") List<String> metaTagNames);
3137
}

flow-client/src/main/java/com/ligg/flowclient/service/impl/BangumiServiceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ public SubjectsVo getRecommendedSubjects(Integer subjectId, int limit, int offse
382382
List<String> tagNames = parseSubjectTagNames(target.getTags());
383383
List<String> metaTagNames = parseMetaTagNames(target.getMetaTags());
384384
Integer releaseYear = parseReleaseYear(target.getDate());
385+
Integer total = subjectMapper.countRecommendedSubjects(
386+
subjectId,
387+
target.getType(),
388+
target.getNsfw(),
389+
tagNames,
390+
metaTagNames);
385391
List<SubjectRecommendationRow> rows = subjectMapper.selectRecommendedSubjects(
386392
subjectId,
387393
target.getType(),
@@ -394,12 +400,12 @@ public SubjectsVo getRecommendedSubjects(Integer subjectId, int limit, int offse
394400

395401
if (rows == null || rows.isEmpty()) {
396402
vo.setData(Collections.emptyList());
397-
vo.setTotal(0);
403+
vo.setTotal(total);
398404
return vo;
399405
}
400406

401407
vo.setData(rows.stream().map(this::toRecommendedSubject).toList());
402-
vo.setTotal(rows.size());
408+
vo.setTotal(total);
403409
return vo;
404410
}
405411

flow-client/src/main/resources/mapper/BangumiSubjectMapper.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,47 @@
132132
limit #{limit} offset #{offset}
133133
</select>
134134

135+
<select id="countRecommendedSubjects" resultType="java.lang.Integer">
136+
select count(*)
137+
from (
138+
select s.id,
139+
<choose>
140+
<when test="tagNames != null and tagNames.size() > 0">
141+
(
142+
select count(distinct subject_tag.name)
143+
from json_table(coalesce(s.tags, cast('[]' as json)),
144+
'$[*]' columns (name varchar(128) path '$.name')) subject_tag
145+
where subject_tag.name in
146+
<foreach collection="tagNames" item="tagName" open="(" separator="," close=")">
147+
#{tagName}
148+
</foreach>
149+
)
150+
</when>
151+
<otherwise>0</otherwise>
152+
</choose> as tag_matches,
153+
<choose>
154+
<when test="metaTagNames != null and metaTagNames.size() > 0">
155+
(
156+
select count(distinct subject_meta.name)
157+
from json_table(coalesce(s.meta_tags, cast('[]' as json)),
158+
'$[*]' columns (name varchar(128) path '$')) subject_meta
159+
where subject_meta.name in
160+
<foreach collection="metaTagNames" item="metaTagName" open="(" separator="," close=")">
161+
#{metaTagName}
162+
</foreach>
163+
)
164+
</when>
165+
<otherwise>0</otherwise>
166+
</choose> as meta_matches,
167+
s.`rank`
168+
from bangumi_subject s
169+
where s.id != #{subjectId}
170+
and s.type = #{type}
171+
and s.nsfw = #{nsfw}
172+
) candidate
173+
where candidate.tag_matches > 0
174+
or candidate.meta_matches > 0
175+
or (candidate.`rank` is not null and candidate.`rank` > 0)
176+
</select>
177+
135178
</mapper>

0 commit comments

Comments
 (0)