fix(service): 修正推荐主题查询逻辑避免重复数据

优化 getRecommendedThemes 方法,使用 limit(8) 替代 SQL 的 LIMIT 8,防止分页插件干扰,确保只返回 8 条未购买的热门主题。
This commit is contained in:
2025-12-11 16:43:19 +08:00
parent fb94c2069d
commit 5121bf3455

View File

@@ -1,6 +1,7 @@
package com.yolo.keyborad.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yolo.keyborad.model.entity.KeyboardThemePurchase;
import com.yolo.keyborad.service.KeyboardThemePurchaseService;
import jakarta.annotation.Resource;
@@ -132,15 +133,23 @@ public class KeyboardThemesServiceImpl extends ServiceImpl<KeyboardThemesMapper,
.map(KeyboardThemePurchase::getThemeId)
.collect(Collectors.toSet());
// 查询推荐主题列表未删除、已启用、未购买、按真实下载量降序、限制8条
return this.lambdaQuery()
// 构建查询器
LambdaQueryChainWrapper<KeyboardThemes> queryWrapper = this.lambdaQuery()
.eq(KeyboardThemes::getDeleted, false)
.eq(KeyboardThemes::getThemeStatus, true)
.notIn(!purchasedThemeIds.isEmpty(), KeyboardThemes::getId, purchasedThemeIds)
.orderByDesc(KeyboardThemes::getRealDownloadCount)
.last("LIMIT 8")
.list()
.stream()
.orderByDesc(KeyboardThemes::getRealDownloadCount);
// 如果有已购买的主题,排除它们
if (!purchasedThemeIds.isEmpty()) {
queryWrapper.notIn(KeyboardThemes::getId, purchasedThemeIds);
}
// 查询推荐主题列表限制8条
List<KeyboardThemes> themesList = queryWrapper.list();
// 只取前8条数据
return themesList.stream()
.limit(8)
.map(theme -> {
KeyboardThemesRespVO vo = BeanUtil.copyProperties(theme, KeyboardThemesRespVO.class);
// 推荐列表中的主题均为未购买状态