Commit c146806b1684a4e6b70da507554e83b8085fc606
1 parent
38d9ec92
feat(product): 添加产品关联
- 在 ProductVO 中添加 relatedProductIds 字段,用于存储关联产品 ID - 在 ProductServiceImpl 中实现关联产品的查询逻辑 - 使用 category 关系和自定义排序来获取最多 10 个关联产品
Showing
2 changed files
with
30 additions
and
0 deletions
shop/src/main/java/com/canrd/shop/module/vo/ProductVO.java
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
... | ... | @@ -117,6 +117,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> 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,33 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> 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 | + QueryWrapper<ProductDO> objectQueryWrapper = new QueryWrapper<ProductDO>() | |
147 | + .in("pc.id", cateId2Relevance.keySet()) | |
148 | + .select("distinct p.id","pc.id") | |
149 | + .last(orderByJoiner.toString()); | |
150 | + List<ProductDO> relatedProducts = this | |
151 | + .list(objectQueryWrapper); | |
152 | + List<String> relatedProductIds = relatedProducts.stream() | |
153 | + .map(ProductDO::getId) | |
154 | + .distinct() | |
155 | + .limit(10) | |
156 | + .collect(Collectors.toList()); | |
157 | + productVO.setRelatedProductIds(relatedProductIds); | |
158 | + | |
131 | 159 | productVO.setPriceShow(productPriceShow); |
132 | 160 | |
133 | 161 | ... | ... |