|
|
@@ -830,9 +830,36 @@ public class ProductRecommendationService {
|
|
|
* @param limit 返回数量
|
|
|
* @return 关联商品列表
|
|
|
*/
|
|
|
- public List<Map<String, Object>> getArticleRelatedProducts(Long articleId, Long userId, Long familyId, int limit) {
|
|
|
+ public List<Map<String, Object>> getArticleRelatedProducts(Long articleId, Long userId, Long familyId, Long memberId, int limit) {
|
|
|
if (articleId == null || limit <= 0) return Collections.emptyList();
|
|
|
|
|
|
+ // 1. 优先按文章标签匹配商品;若无标签关联商品,兜底返回首页推荐商品
|
|
|
+ List<Map<String, Object>> tagged = getArticleTagMatchedProducts(articleId, userId, familyId, limit);
|
|
|
+ if (tagged != null && !tagged.isEmpty()) {
|
|
|
+ return tagged;
|
|
|
+ }
|
|
|
+ log.info("文章[{}]无标签关联商品,按首页推荐商品兜底返回", articleId);
|
|
|
+ List<Map<String, Object>> fallback = getDimensionRecommendations(null, familyId, memberId, null, limit, userId);
|
|
|
+ if (fallback == null || fallback.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ for (Map<String, Object> item : fallback) {
|
|
|
+ item.put("reason", "暂无标签关联商品,按首页推荐展示");
|
|
|
+ item.put("source", "home_fallback");
|
|
|
+ }
|
|
|
+ return fallback;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文章标签匹配商品推荐:通过文章标签与商品标签匹配,按 标签匹配数 > 用户权重 > 家庭权重 > 销量 排序
|
|
|
+ *
|
|
|
+ * @param articleId 文章ID
|
|
|
+ * @param userId 当前用户ID(可为空,用于用户权重)
|
|
|
+ * @param familyId 家庭ID(可为空,用于家庭权重)
|
|
|
+ * @param limit 返回数量
|
|
|
+ * @return 关联商品列表(无匹配时返回空列表,由调用方决定是否兜底)
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> getArticleTagMatchedProducts(Long articleId, Long userId, Long familyId, int limit) {
|
|
|
// 1. 获取文章标签
|
|
|
Article article = articleMapper.selectById(articleId);
|
|
|
if (article == null || article.getTags() == null || article.getTags().trim().isEmpty()) {
|