|
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
+import com.etotem.cfc.service.support.ProductVisibilitySupport;
|
|
|
import com.etotem.cfc.util.SortUtil;
|
|
import com.etotem.cfc.util.SortUtil;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -70,6 +71,9 @@ public class ProductRecommendationService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private ContentDimensionThresholdService contentDimensionThresholdService;
|
|
private ContentDimensionThresholdService contentDimensionThresholdService;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProductVisibilitySupport productVisibilitySupport;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 维度页推荐
|
|
* 维度页推荐
|
|
|
*
|
|
*
|
|
@@ -116,13 +120,14 @@ public class ProductRecommendationService {
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
if (!mappedProductIds.isEmpty()) {
|
|
if (!mappedProductIds.isEmpty()) {
|
|
|
- List<Product> mapped = productMapper.selectList(
|
|
|
|
|
- new LambdaQueryWrapper<Product>()
|
|
|
|
|
|
|
+ LambdaQueryWrapper<Product> mappedWrapper = new LambdaQueryWrapper<Product>()
|
|
|
.in(Product::getId, mappedProductIds)
|
|
.in(Product::getId, mappedProductIds)
|
|
|
- .eq(Product::getStatus, "on_shelf")
|
|
|
|
|
- .and(w -> w.ne(Product::getProductType, "physical").or().isNull(Product::getStock).or().gt(Product::getStock, 0))
|
|
|
|
|
- .orderByAsc(Product::getSortOrder)
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ .eq(Product::getStatus, "on_shelf")
|
|
|
|
|
+ .and(w -> w.ne(Product::getProductType, "physical").or().isNull(Product::getStock).or().gt(Product::getStock, 0))
|
|
|
|
|
+ .orderByAsc(Product::getSortOrder);
|
|
|
|
|
+ // 商品可见性过滤(min_visible_level + vendor_visible),与商城列表 /api/product/list 对齐
|
|
|
|
|
+ productVisibilitySupport.applyToQuery(mappedWrapper, userId, null);
|
|
|
|
|
+ List<Product> mapped = productMapper.selectList(mappedWrapper);
|
|
|
|
|
|
|
|
HealthFilterService.FilterResult mappedFilter = healthFilterService.filterProducts(healthProfile, mapped);
|
|
HealthFilterService.FilterResult mappedFilter = healthFilterService.filterProducts(healthProfile, mapped);
|
|
|
mapped = mappedFilter.allowed;
|
|
mapped = mappedFilter.allowed;
|
|
@@ -182,6 +187,8 @@ public class ProductRecommendationService {
|
|
|
domainQw.notIn(Product::getId, purchasedIds);
|
|
domainQw.notIn(Product::getId, purchasedIds);
|
|
|
}
|
|
}
|
|
|
SortUtil.applySort(domainQw);
|
|
SortUtil.applySort(domainQw);
|
|
|
|
|
+ // 商品可见性过滤(min_visible_level + vendor_visible)
|
|
|
|
|
+ productVisibilitySupport.applyToQuery(domainQw, userId, null);
|
|
|
List<Product> domainMatched = productMapper.selectList(domainQw);
|
|
List<Product> domainMatched = productMapper.selectList(domainQw);
|
|
|
HealthFilterService.FilterResult domainFilter = healthFilterService.filterProducts(healthProfile, domainMatched);
|
|
HealthFilterService.FilterResult domainFilter = healthFilterService.filterProducts(healthProfile, domainMatched);
|
|
|
domainMatched = domainFilter.allowed;
|
|
domainMatched = domainFilter.allowed;
|
|
@@ -206,7 +213,7 @@ public class ProductRecommendationService {
|
|
|
|
|
|
|
|
// 4. 关联关系推荐(动态:画像缺口、共购、复购)
|
|
// 4. 关联关系推荐(动态:画像缺口、共购、复购)
|
|
|
List<Map<String, Object>> assocResults = queryAssociationRecommendations(
|
|
List<Map<String, Object>> assocResults = queryAssociationRecommendations(
|
|
|
- dimensionCode, familyId, memberId, purchasedIds, excludeProductIds, limit - candidates.size(), healthProfile);
|
|
|
|
|
|
|
+ dimensionCode, familyId, memberId, purchasedIds, excludeProductIds, limit - candidates.size(), healthProfile, userId);
|
|
|
|
|
|
|
|
// 合并并去重(关联推荐优先级高于维度映射,但保留维度的reason/matchScore)
|
|
// 合并并去重(关联推荐优先级高于维度映射,但保留维度的reason/matchScore)
|
|
|
if (assocResults != null && !assocResults.isEmpty()) {
|
|
if (assocResults != null && !assocResults.isEmpty()) {
|
|
@@ -242,17 +249,19 @@ public class ProductRecommendationService {
|
|
|
private List<Map<String, Object>> queryAssociationRecommendations(
|
|
private List<Map<String, Object>> queryAssociationRecommendations(
|
|
|
String dimensionCode, Long familyId, Long memberId,
|
|
String dimensionCode, Long familyId, Long memberId,
|
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
|
- HealthFilterService.HealthProfile healthProfile) {
|
|
|
|
|
|
|
+ HealthFilterService.HealthProfile healthProfile, Long userId) {
|
|
|
|
|
|
|
|
if (reqCount <= 0) return Collections.emptyList();
|
|
if (reqCount <= 0) return Collections.emptyList();
|
|
|
|
|
|
|
|
|
|
+ // 商品可见性上下文(min_visible_level + vendor_visible),一次性查库供各关联子方法复用
|
|
|
|
|
+ ProductVisibilitySupport.VisibilityContext visibilityCtx = productVisibilitySupport.buildContext(userId, null);
|
|
|
Map<String, Integer> memberScores = getMemberDimensionScores(familyId, memberId);
|
|
Map<String, Integer> memberScores = getMemberDimensionScores(familyId, memberId);
|
|
|
List<Map<String, Object>> results = new ArrayList<>();
|
|
List<Map<String, Object>> results = new ArrayList<>();
|
|
|
|
|
|
|
|
// 4.1 画像缺口关联:维度得分低时推荐对应商品
|
|
// 4.1 画像缺口关联:维度得分低时推荐对应商品
|
|
|
if (dimensionCode != null && !dimensionCode.isEmpty()) {
|
|
if (dimensionCode != null && !dimensionCode.isEmpty()) {
|
|
|
List<Map<String, Object>> profileAssocs = queryProfileGapAssociations(
|
|
List<Map<String, Object>> profileAssocs = queryProfileGapAssociations(
|
|
|
- dimensionCode, memberScores, purchasedIds, excludeProductIds, reqCount / 3, healthProfile);
|
|
|
|
|
|
|
+ dimensionCode, memberScores, purchasedIds, excludeProductIds, reqCount / 3, healthProfile, visibilityCtx);
|
|
|
results.addAll(profileAssocs);
|
|
results.addAll(profileAssocs);
|
|
|
if (results.size() >= reqCount) return results.subList(0, reqCount);
|
|
if (results.size() >= reqCount) return results.subList(0, reqCount);
|
|
|
}
|
|
}
|
|
@@ -260,7 +269,7 @@ public class ProductRecommendationService {
|
|
|
// 4.2 共购关联:买了A的用户也买了B(从订单数据挖掘)
|
|
// 4.2 共购关联:买了A的用户也买了B(从订单数据挖掘)
|
|
|
if (purchasedIds != null && !purchasedIds.isEmpty()) {
|
|
if (purchasedIds != null && !purchasedIds.isEmpty()) {
|
|
|
List<Map<String, Object>> coPurchaseAssocs = queryCoPurchaseAssociations(
|
|
List<Map<String, Object>> coPurchaseAssocs = queryCoPurchaseAssociations(
|
|
|
- purchasedIds, excludeProductIds, reqCount - results.size(), healthProfile);
|
|
|
|
|
|
|
+ purchasedIds, excludeProductIds, reqCount - results.size(), healthProfile, visibilityCtx);
|
|
|
results.addAll(coPurchaseAssocs);
|
|
results.addAll(coPurchaseAssocs);
|
|
|
if (results.size() >= reqCount) return results.subList(0, reqCount);
|
|
if (results.size() >= reqCount) return results.subList(0, reqCount);
|
|
|
}
|
|
}
|
|
@@ -268,7 +277,7 @@ public class ProductRecommendationService {
|
|
|
// 4.3 复购关联:买了A后X天再买A的关联商品
|
|
// 4.3 复购关联:买了A后X天再买A的关联商品
|
|
|
if (purchasedIds != null && !purchasedIds.isEmpty()) {
|
|
if (purchasedIds != null && !purchasedIds.isEmpty()) {
|
|
|
List<Map<String, Object>> repurchaseAssocs = queryRepurchaseAssociations(
|
|
List<Map<String, Object>> repurchaseAssocs = queryRepurchaseAssociations(
|
|
|
- purchasedIds, excludeProductIds, reqCount - results.size(), healthProfile);
|
|
|
|
|
|
|
+ purchasedIds, excludeProductIds, reqCount - results.size(), healthProfile, visibilityCtx);
|
|
|
results.addAll(repurchaseAssocs);
|
|
results.addAll(repurchaseAssocs);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -281,7 +290,8 @@ public class ProductRecommendationService {
|
|
|
private List<Map<String, Object>> queryProfileGapAssociations(
|
|
private List<Map<String, Object>> queryProfileGapAssociations(
|
|
|
String dimensionCode, Map<String, Integer> memberScores,
|
|
String dimensionCode, Map<String, Integer> memberScores,
|
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
|
- HealthFilterService.HealthProfile healthProfile) {
|
|
|
|
|
|
|
+ HealthFilterService.HealthProfile healthProfile,
|
|
|
|
|
+ ProductVisibilitySupport.VisibilityContext visibilityCtx) {
|
|
|
|
|
|
|
|
if (reqCount <= 0) return Collections.emptyList();
|
|
if (reqCount <= 0) return Collections.emptyList();
|
|
|
|
|
|
|
@@ -315,6 +325,7 @@ public class ProductRecommendationService {
|
|
|
Product product = productMapper.selectById(assoc.getTargetProductId());
|
|
Product product = productMapper.selectById(assoc.getTargetProductId());
|
|
|
if (product == null || !"on_shelf".equals(product.getStatus()) || !hasSellableStock(product)) continue;
|
|
if (product == null || !"on_shelf".equals(product.getStatus()) || !hasSellableStock(product)) continue;
|
|
|
if (!healthFilterService.checkProductBlocks(healthProfile, product).isEmpty()) continue;
|
|
if (!healthFilterService.checkProductBlocks(healthProfile, product).isEmpty()) continue;
|
|
|
|
|
+ if (!productVisibilitySupport.isVisibleTo(product, visibilityCtx)) continue; // 商品可见性(供应商专属/会员级别)
|
|
|
|
|
|
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
|
item.put("id", product.getId());
|
|
item.put("id", product.getId());
|
|
@@ -354,7 +365,8 @@ public class ProductRecommendationService {
|
|
|
*/
|
|
*/
|
|
|
private List<Map<String, Object>> queryCoPurchaseAssociations(
|
|
private List<Map<String, Object>> queryCoPurchaseAssociations(
|
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
|
- HealthFilterService.HealthProfile healthProfile) {
|
|
|
|
|
|
|
+ HealthFilterService.HealthProfile healthProfile,
|
|
|
|
|
+ ProductVisibilitySupport.VisibilityContext visibilityCtx) {
|
|
|
|
|
|
|
|
if (reqCount <= 0 || purchasedIds == null || purchasedIds.isEmpty()) return Collections.emptyList();
|
|
if (reqCount <= 0 || purchasedIds == null || purchasedIds.isEmpty()) return Collections.emptyList();
|
|
|
|
|
|
|
@@ -383,6 +395,7 @@ public class ProductRecommendationService {
|
|
|
Product product = productMapper.selectById(targetId);
|
|
Product product = productMapper.selectById(targetId);
|
|
|
if (product == null || !"on_shelf".equals(product.getStatus()) || !hasSellableStock(product)) continue;
|
|
if (product == null || !"on_shelf".equals(product.getStatus()) || !hasSellableStock(product)) continue;
|
|
|
if (!healthFilterService.checkProductBlocks(healthProfile, product).isEmpty()) continue;
|
|
if (!healthFilterService.checkProductBlocks(healthProfile, product).isEmpty()) continue;
|
|
|
|
|
+ if (!productVisibilitySupport.isVisibleTo(product, visibilityCtx)) continue; // 商品可见性(供应商专属/会员级别)
|
|
|
|
|
|
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
|
item.put("id", product.getId());
|
|
item.put("id", product.getId());
|
|
@@ -411,7 +424,8 @@ public class ProductRecommendationService {
|
|
|
*/
|
|
*/
|
|
|
private List<Map<String, Object>> queryRepurchaseAssociations(
|
|
private List<Map<String, Object>> queryRepurchaseAssociations(
|
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
Set<Long> purchasedIds, List<Long> excludeProductIds, int reqCount,
|
|
|
- HealthFilterService.HealthProfile healthProfile) {
|
|
|
|
|
|
|
+ HealthFilterService.HealthProfile healthProfile,
|
|
|
|
|
+ ProductVisibilitySupport.VisibilityContext visibilityCtx) {
|
|
|
|
|
|
|
|
if (reqCount <= 0 || purchasedIds == null || purchasedIds.isEmpty()) return Collections.emptyList();
|
|
if (reqCount <= 0 || purchasedIds == null || purchasedIds.isEmpty()) return Collections.emptyList();
|
|
|
|
|
|
|
@@ -435,6 +449,7 @@ public class ProductRecommendationService {
|
|
|
Product product = productMapper.selectById(targetId);
|
|
Product product = productMapper.selectById(targetId);
|
|
|
if (product == null || !"on_shelf".equals(product.getStatus()) || !hasSellableStock(product)) continue;
|
|
if (product == null || !"on_shelf".equals(product.getStatus()) || !hasSellableStock(product)) continue;
|
|
|
if (!healthFilterService.checkProductBlocks(healthProfile, product).isEmpty()) continue;
|
|
if (!healthFilterService.checkProductBlocks(healthProfile, product).isEmpty()) continue;
|
|
|
|
|
+ if (!productVisibilitySupport.isVisibleTo(product, visibilityCtx)) continue; // 商品可见性(供应商专属/会员级别)
|
|
|
|
|
|
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
|
item.put("id", product.getId());
|
|
item.put("id", product.getId());
|
|
@@ -962,11 +977,12 @@ public class ProductRecommendationService {
|
|
|
Set<String> sourceTagSet = new HashSet<>(sourceTags);
|
|
Set<String> sourceTagSet = new HashSet<>(sourceTags);
|
|
|
|
|
|
|
|
// 2. 查询所有在售且有库存的商品
|
|
// 2. 查询所有在售且有库存的商品
|
|
|
- List<Product> products = productMapper.selectList(
|
|
|
|
|
- new LambdaQueryWrapper<Product>()
|
|
|
|
|
- .eq(Product::getStatus, "on_shelf")
|
|
|
|
|
- .and(w -> w.ne(Product::getProductType, "physical").or().isNull(Product::getStock).or().gt(Product::getStock, 0))
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ LambdaQueryWrapper<Product> tagWrapper = new LambdaQueryWrapper<Product>()
|
|
|
|
|
+ .eq(Product::getStatus, "on_shelf")
|
|
|
|
|
+ .and(w -> w.ne(Product::getProductType, "physical").or().isNull(Product::getStock).or().gt(Product::getStock, 0));
|
|
|
|
|
+ // 商品可见性过滤(min_visible_level + vendor_visible)
|
|
|
|
|
+ productVisibilitySupport.applyToQuery(tagWrapper, userId, null);
|
|
|
|
|
+ List<Product> products = productMapper.selectList(tagWrapper);
|
|
|
if (products == null || products.isEmpty()) return Collections.emptyList();
|
|
if (products == null || products.isEmpty()) return Collections.emptyList();
|
|
|
|
|
|
|
|
// 3. 预加载用户权重(推荐曝光日志:购买=1.0,点击=0.6)
|
|
// 3. 预加载用户权重(推荐曝光日志:购买=1.0,点击=0.6)
|