refactor(service): 优化推荐逻辑不再排除已购买主题

This commit is contained in:
2025-12-23 15:43:27 +08:00
parent 1b7374e959
commit e90078791c

View File

@@ -118,22 +118,13 @@ public class KeyboardThemesServiceImpl extends ServiceImpl<KeyboardThemesMapper,
@Override @Override
/* /*
获取推荐主题列表 获取推荐主题列表
<p>推荐规则:根据真实下载量降序排序,排除用户已购买的主题和当前查看的主题最多返回8个主题</p> <p>推荐规则根据真实下载量降序排序排除当前查看的主题最多返回8个主题</p>
@param userId 用户ID @param userId 用户ID
@param themeId 当前主题ID需要从推荐列表中排除 @param themeId 当前主题ID需要从推荐列表中排除
* @return 推荐主题列表,包含主题详情和购买状态(推荐列表中的主题购买状态均为未购买) * @return 推荐主题列表,包含主题详情和购买状态
*/ */
public List<KeyboardThemesRespVO> getRecommendedThemes(Long userId, Long themeId) { public List<KeyboardThemesRespVO> getRecommendedThemes(Long userId, Long themeId) {
// 查询用户已购买的主题ID集合
Set<Long> purchasedThemeIds = purchaseService.lambdaQuery()
.eq(KeyboardThemePurchase::getUserId, userId)
.eq(KeyboardThemePurchase::getPayStatus, (short) 1)
.list()
.stream()
.map(KeyboardThemePurchase::getThemeId)
.collect(Collectors.toSet());
// 构建查询器 // 构建查询器
LambdaQueryChainWrapper<KeyboardThemes> queryWrapper = this.lambdaQuery() LambdaQueryChainWrapper<KeyboardThemes> queryWrapper = this.lambdaQuery()
.eq(KeyboardThemes::getDeleted, false) .eq(KeyboardThemes::getDeleted, false)
@@ -145,21 +136,25 @@ public class KeyboardThemesServiceImpl extends ServiceImpl<KeyboardThemesMapper,
queryWrapper.ne(KeyboardThemes::getId, themeId); queryWrapper.ne(KeyboardThemes::getId, themeId);
} }
// 如果有已购买的主题,排除它们
if (!purchasedThemeIds.isEmpty()) {
queryWrapper.notIn(KeyboardThemes::getId, purchasedThemeIds);
}
// 查询推荐主题列表限制8条 // 查询推荐主题列表限制8条
List<KeyboardThemes> themesList = queryWrapper.list(); List<KeyboardThemes> themesList = queryWrapper.list();
// 只取前8条数据 // 查询用户已购买的主题ID集合
Set<Long> purchasedThemeIds = purchaseService.lambdaQuery()
.eq(KeyboardThemePurchase::getUserId, userId)
.eq(KeyboardThemePurchase::getPayStatus, (short) 1)
.list()
.stream()
.map(KeyboardThemePurchase::getThemeId)
.collect(Collectors.toSet());
// 只取前8条数据并设置购买状态
return themesList.stream() return themesList.stream()
.limit(8) .limit(8)
.map(theme -> { .map(theme -> {
KeyboardThemesRespVO vo = BeanUtil.copyProperties(theme, KeyboardThemesRespVO.class); KeyboardThemesRespVO vo = BeanUtil.copyProperties(theme, KeyboardThemesRespVO.class);
// 推荐列表中的主题均为未购买状态 // 设置主题的实际购买状态
vo.setIsPurchased(false); vo.setIsPurchased(purchasedThemeIds.contains(theme.getId()));
return vo; return vo;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }