Commit abb8bfd8d525f0072b940e57840772a9c9a814dd
1 parent
7a48845c
feat(Product): 优化商品详情页相关商品推荐逻辑
- 新增 subCate 方法获取产品子分类 - 在获取相关产品时,优先按关联度排序 - 当相关产品不足 10 个时,补充同子分类的产品- 优化了相关产品的推荐算法,提高推荐的准确性和多样性
Showing
1 changed file
with
37 additions
and
2 deletions
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
@@ -86,6 +86,20 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -86,6 +86,20 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
86 | @Autowired | 86 | @Autowired |
87 | private IJournalService journalService; | 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 | * 通过ID查询单条数据 | 104 | * 通过ID查询单条数据 |
91 | * <p> | 105 | * <p> |
@@ -190,12 +204,13 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -190,12 +204,13 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
190 | List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery() | 204 | List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery() |
191 | .in(EbCategorysRelation::getCategoryId, categoryIds) | 205 | .in(EbCategorysRelation::getCategoryId, categoryIds) |
192 | .list(); | 206 | .list(); |
193 | - | 207 | + //获取关联商品 |
194 | Map<String,Integer> relatedCateId2Relevance = cateRelations.stream() | 208 | Map<String,Integer> relatedCateId2Relevance = cateRelations.stream() |
195 | .collect(Collectors.toMap(EbCategorysRelation::getRelatedCategoryId, EbCategorysRelation::getRelevance,(a, b) -> a)); | 209 | .collect(Collectors.toMap(EbCategorysRelation::getRelatedCategoryId, EbCategorysRelation::getRelevance,(a, b) -> a)); |
196 | TreeMap<String, Integer> cateId2Relevance = new TreeMap<>(relatedCateId2Relevance); | 210 | TreeMap<String, Integer> cateId2Relevance = new TreeMap<>(relatedCateId2Relevance); |
197 | StringJoiner orderByJoiner = new StringJoiner(" when "," order by case pc.id when "," else 0 end "); | 211 | StringJoiner orderByJoiner = new StringJoiner(" when "," order by case pc.id when "," else 0 end "); |
198 | cateId2Relevance.forEach((k,v)->orderByJoiner.add("'"+k+"'"+" then "+v)); | 212 | cateId2Relevance.forEach((k,v)->orderByJoiner.add("'"+k+"'"+" then "+v)); |
213 | + productVO.setRelatedProductIds(new ArrayList<>()); | ||
199 | if(CollUtil.isNotEmpty(cateId2Relevance)) { | 214 | if(CollUtil.isNotEmpty(cateId2Relevance)) { |
200 | QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>() | 215 | QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>() |
201 | .eq("ismarketable",true) | 216 | .eq("ismarketable",true) |
@@ -210,7 +225,27 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | @@ -210,7 +225,27 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im | ||
210 | .distinct() | 225 | .distinct() |
211 | .limit(10) | 226 | .limit(10) |
212 | .collect(Collectors.toList()); | 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 | // List<JournalCategoryRelation> journalCategoryRelations = journalCategoryService.lambdaQuery() | 251 | // List<JournalCategoryRelation> journalCategoryRelations = journalCategoryService.lambdaQuery() |