Browse Source

fix: ContentRecommendService 商品/活动推荐改用 dimension_weights JSON 列查询

recommendProducts() 和 recommendActivities() 此前仅使用旧字段
(domain / dimension_code) 进行维度匹配,导致配置了 dimension_weights
但未设置旧字段的商品/活动在推荐系统中被遗漏。

改为与 ProductService/ActivityService 一致的 JSON_EXTRACT + 旧字段
OR 回退模式,确保权重感知查询全面覆盖。
Xiaogang Liao 3 months ago
parent
commit
6c2667ce9d

+ 10 - 4
cfc-backend/src/main/java/com/etotem/cfc/service/ContentRecommendService.java

@@ -249,15 +249,18 @@ public class ContentRecommendService {
 
     // ==================== 商品推荐 ====================
 
-    private List<RecommendItemVO> recommendProducts(String dimensionCode, Long userId,
-                                                     Long familyId, Set<Long> excludeIds) {
+private List<RecommendItemVO> recommendProducts(String dimensionCode, Long userId,
+                                                      Long familyId, Set<Long> excludeIds) {
         LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                 .eq(Product::getStatus, "on_shelf")
                 .orderByDesc(Product::getSalesCount)
                 .last("LIMIT 50");
 
         if (dimensionCode != null && !dimensionCode.trim().isEmpty()) {
-            wrapper.eq(Product::getDomain, dimensionCode);
+            // P1: 权重感知查询 — 匹配 dimension_weights > 0 或兼容旧版 domain
+            wrapper.and(w -> w
+                .apply("COALESCE(JSON_EXTRACT(dimension_weights, CONCAT('$.', {0})), 0) > 0", dimensionCode.trim())
+                .or().eq(Product::getDomain, dimensionCode.trim()));
         }
 
         List<Product> allProducts = productMapper.selectList(wrapper);
@@ -356,7 +359,10 @@ public class ContentRecommendService {
                 .last("LIMIT 50");
 
         if (dimensionCode != null && !dimensionCode.trim().isEmpty()) {
-            wrapper.eq(Activity::getDimensionCode, dimensionCode);
+            // P1: 权重感知查询 — 匹配 dimension_weights > 0 或兼容旧版 dimension_code
+            wrapper.and(w -> w
+                .apply("COALESCE(JSON_EXTRACT(dimension_weights, CONCAT('$.', {0})), 0) > 0", dimensionCode.trim())
+                .or().eq(Activity::getDimensionCode, dimensionCode.trim()));
         }
 
         List<Activity> allActivities = activityMapper.selectList(wrapper);