Commit abb8bfd8d525f0072b940e57840772a9c9a814dd

Authored by 曾国涛
1 parent 7a48845c

feat(Product): 优化商品详情页相关商品推荐逻辑

- 新增 subCate 方法获取产品子分类
- 在获取相关产品时,优先按关联度排序
- 当相关产品不足 10 个时,补充同子分类的产品- 优化了相关产品的推荐算法,提高推荐的准确性和多样性
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
... ... @@ -86,6 +86,20 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
86 86 @Autowired
87 87 private IJournalService journalService;
88 88  
  89 +
  90 + public List<ProductCategoryDO> subCate(String productId){
  91 + List<Productcategoryrelation> list = productCategoryRelationService.lambdaQuery()
  92 + .eq(Productcategoryrelation::getProductId, productId)
  93 + .list();
  94 + List<String> categoryIds = list.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList());
  95 + List<ProductCategoryDO> categoryDOS = categoryService.lambdaQuery()
  96 + .in(ProductCategoryDO::getId, categoryIds)
  97 + .list();
  98 + Set<String> parentIds = categoryDOS.stream().map(ProductCategoryDO::getParentId).collect(Collectors.toSet());
  99 + List<ProductCategoryDO> collect = categoryDOS.stream().filter(cate -> !parentIds.contains(cate.getId())).collect(Collectors.toList());
  100 + return collect;
  101 + }
  102 +
89 103 /**
90 104 * 通过ID查询单条数据
91 105 * <p>
... ... @@ -190,12 +204,13 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im
190 204 List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery()
191 205 .in(EbCategorysRelation::getCategoryId, categoryIds)
192 206 .list();
193   -
  207 + //获取关联商品
194 208 Map<String,Integer> relatedCateId2Relevance = cateRelations.stream()
195 209 .collect(Collectors.toMap(EbCategorysRelation::getRelatedCategoryId, EbCategorysRelation::getRelevance,(a, b) -> a));
196 210 TreeMap<String, Integer> cateId2Relevance = new TreeMap<>(relatedCateId2Relevance);
197 211 StringJoiner orderByJoiner = new StringJoiner(" when "," order by case pc.id when "," else 0 end ");
198 212 cateId2Relevance.forEach((k,v)->orderByJoiner.add("'"+k+"'"+" then "+v));
  213 + productVO.setRelatedProductIds(new ArrayList<>());
199 214 if(CollUtil.isNotEmpty(cateId2Relevance)) {
200 215 QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>()
201 216 .eq("ismarketable",true)
... ... @@ -210,7 +225,27 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im
210 225 .distinct()
211 226 .limit(10)
212 227 .collect(Collectors.toList());
213   - productVO.setRelatedProductIds(relatedProductIds);
  228 + productVO.getRelatedProductIds().addAll(relatedProductIds);
  229 + }
  230 + if(productVO.getRelatedProductIds().size()<10){
  231 + List<ProductCategoryDO> productCategoryDOS = subCate(productQueryVO.getId());
  232 + List<String> cateIds = productCategoryDOS.stream()
  233 + .map(ProductCategoryDO::getId)
  234 + .collect(Collectors.toList());
  235 + List<String> collect = productCategoryRelationService.lambdaQuery()
  236 + .select(Productcategoryrelation::getProductId)
  237 + .in(Productcategoryrelation::getCategoryId, cateIds)
  238 + .notIn(Productcategoryrelation::getProductId,productVO.getRelatedProductIds())
  239 + .list().stream()
  240 + .map(Productcategoryrelation::getProductId)
  241 + .collect(Collectors.toList());
  242 + List<String> records = this.lambdaQuery()
  243 + .in(ProductDO::getId, collect)
  244 + .eq(ProductDO::getIsmarketable, true)
  245 + .select(ProductDO::getId)
  246 + .page(new Page<>(1, 10 - productVO.getRelatedProductIds().size())).getRecords().stream()
  247 + .map(ProductDO::getId).collect(Collectors.toList());
  248 + productVO.getRelatedProductIds().addAll(records);
214 249 }
215 250  
216 251 // List<JournalCategoryRelation> journalCategoryRelations = journalCategoryService.lambdaQuery()
... ...