Commit a6f513b96b6a60428fe7793e4046e9f26c22f541

Authored by 曾国涛
Committed by boyang
1 parent 38d9ec92

feat(product): 添加产品关联

- 在 ProductVO 中添加 relatedProductIds 字段,用于存储关联产品 ID
- 在 ProductServiceImpl 中实现关联产品的查询逻辑
- 使用 category 关系和自定义排序来获取最多 10 个关联产品
shop/src/main/java/com/canrd/shop/module/vo/ProductVO.java
... ... @@ -143,4 +143,6 @@ public class ProductVO implements Serializable {
143 143 */
144 144 private List<TickeyTypeVO> ticketTypes;
145 145  
  146 + private List<String> RelatedProductIds;
  147 +
146 148 }
... ...
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
... ... @@ -117,6 +117,7 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im
117 117 });
118 118 productVO.setProductAttributeList(productAttributeVOS);
119 119 }
  120 +
120 121 // 金额处理
121 122 Boolean productPriceShow = switchControlService.getEnabledByName(SwitchControlConstants.PRODUCT_PRICE_SHOW);
122 123 List<TicketTypeDO> ticketTypeDOS = ticketTypeService.selectByProductId(productDO.getId());
... ... @@ -128,6 +129,35 @@ public class ProductServiceImpl extends ServiceImpl&lt;ProductMapper, ProductDO&gt; im
128 129 productVO.setTicketTypes(typeVOS);
129 130 }
130 131  
  132 + List<Productcategoryrelation> productcategoryrelations = productCategoryRelationService.lambdaQuery()
  133 + .eq(Productcategoryrelation::getProductId, productQueryVO.getId())
  134 + .list();
  135 + List<String> categoryIds = productcategoryrelations.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList());
  136 +
  137 + List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery()
  138 + .in(EbCategorysRelation::getCategoryId, categoryIds)
  139 + .list();
  140 +
  141 + Map<String,Integer> relatedCateId2Relevance = cateRelations.stream()
  142 + .collect(Collectors.toMap(EbCategorysRelation::getRelatedCategoryId, EbCategorysRelation::getRelevance,(a, b) -> a));
  143 + TreeMap<String, Integer> cateId2Relevance = new TreeMap<>(relatedCateId2Relevance);
  144 + StringJoiner orderByJoiner = new StringJoiner(" when "," order by case pc.id when "," else 0 end ");
  145 + cateId2Relevance.forEach((k,v)->orderByJoiner.add("'"+k+"'"+" then "+v));
  146 + if(CollUtil.isNotEmpty(cateId2Relevance)){
  147 + QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>()
  148 + .in("pc.id", cateId2Relevance.keySet())
  149 + .select("distinct p.id","pc.id")
  150 + .last(orderByJoiner.toString());
  151 + List<ProductDO> relatedProducts = this
  152 + .list(objectQueryWrapper);
  153 + List<String> relatedProductIds = relatedProducts.stream()
  154 + .map(ProductDO::getId)
  155 + .distinct()
  156 + .limit(10)
  157 + .collect(Collectors.toList());
  158 + productVO.setRelatedProductIds(relatedProductIds);
  159 + }
  160 +
131 161 productVO.setPriceShow(productPriceShow);
132 162  
133 163  
... ...