Commit 2fa199e4fc6fccf2b9fd2f155ba080bffa1e6c92
1 parent
a11bc16b
fix: 二级分类递归查一级分类
Showing
1 changed file
with
66 additions
and
23 deletions
shop/src/main/java/com/canrd/shop/service/impl/ProductServiceImpl.java
... | ... | @@ -137,36 +137,49 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
137 | 137 | .eq(Productcategoryrelation::getProductId, productQueryVO.getId()) |
138 | 138 | .list(); |
139 | 139 | List<String> categoryIds = productcategoryrelations.stream().map(Productcategoryrelation::getCategoryId).collect(Collectors.toList()); |
140 | + | |
140 | 141 | if (CollectionUtils.isNotEmpty(categoryIds)) { |
142 | + // 查询第一级分类 | |
141 | 143 | List<ProductCategoryDO> productCategories = categoryService.lambdaQuery() |
142 | 144 | .in(ProductCategoryDO::getId, categoryIds) |
143 | 145 | .eq(ProductCategoryDO::getParentId, "33ba6943c8bf4ca082614f299865341c") |
144 | 146 | .list(); |
145 | - ProductCategoryDO firstCategory = productCategories.get(0); | |
146 | - List<ProductCategoryDO> productCategories2 = categoryService.lambdaQuery() | |
147 | - .in(ProductCategoryDO::getId, categoryIds) | |
148 | - .eq(ProductCategoryDO::getParentId, firstCategory.getId()) | |
149 | - .list(); | |
150 | -// ProductCategoryDO secondCategory = productCategories2.get(0); | |
151 | - ProductCategoryDO secondCategory = productCategories2.stream() | |
152 | - .filter(category -> !"Not specified".equals(category.getName())) | |
153 | - .findFirst() | |
154 | - .orElse(productCategories2.get(0)); // 如果找不到,使用列表第一个元素 | |
155 | - // 从 functionService 获取 functionId | |
156 | - List<ProductFunctionDO> functions = functionService.getFirstFunction(productVO.getId()); | |
157 | - ProductFunctionDO selectedFunction = null; | |
158 | - if (functions != null && functions.size() > 0) { | |
159 | - selectedFunction = functions.stream() | |
160 | - .filter(function -> !"Not specified".equals(function.getName())) | |
147 | + ProductCategoryDO firstCategory = null; | |
148 | + if (productCategories.isEmpty()) { | |
149 | + firstCategory = findParentCategory(categoryIds, "33ba6943c8bf4ca082614f299865341c"); | |
150 | + } else { | |
151 | + firstCategory = productCategories.get(0); | |
152 | + } | |
153 | + | |
154 | + if (firstCategory != null) { | |
155 | + // 查询第二级分类 | |
156 | + List<ProductCategoryDO> productCategories2 = categoryService.lambdaQuery() | |
157 | + .in(ProductCategoryDO::getId, categoryIds) | |
158 | + .eq(ProductCategoryDO::getParentId, firstCategory.getId()) | |
159 | + .list(); | |
160 | + ProductCategoryDO secondCategory = productCategories2.stream() | |
161 | + .filter(category -> !"Not specified".equals(category.getName())) | |
161 | 162 | .findFirst() |
162 | - .orElse(functions.get(0)); // 如果找不到,使用列表第一个元素 | |
163 | + .orElse(productCategories2.isEmpty() ? null : productCategories2.get(0)); // 如果找不到,使用列表第一个元素或 null | |
164 | + | |
165 | + // 获取功能信息 | |
166 | + List<ProductFunctionDO> functions = functionService.getFirstFunction(productVO.getId()); | |
167 | + ProductFunctionDO selectedFunction = null; | |
168 | + if (CollectionUtils.isNotEmpty(functions)){ | |
169 | + selectedFunction = functions.stream() | |
170 | + .filter(function -> !"Not specified".equals(function.getName())) | |
171 | + .findFirst() | |
172 | + .orElse(functions.get(0)); // 如果找不到,使用列表第一个元素 | |
173 | + } | |
174 | + | |
175 | + // 构建面包屑信息 | |
176 | + ProductCrumbsVO productCrumbsVO = ProductCrumbsVO.builder() | |
177 | + .category1(firstCategory.getName()) | |
178 | + .category2(secondCategory != null ? secondCategory.getName() : null) | |
179 | + .function(selectedFunction != null ? selectedFunction.getName() : null) | |
180 | + .build(); | |
181 | + productVO.setProductCrumbsVO(productCrumbsVO); | |
163 | 182 | } |
164 | - ProductCrumbsVO productCrumbsVO = ProductCrumbsVO.builder() | |
165 | - .category1(firstCategory.getName()) // | |
166 | - .category2(secondCategory.getName()) | |
167 | - .function(selectedFunction.getName()) | |
168 | - .build(); | |
169 | - productVO.setProductCrumbsVO(productCrumbsVO); | |
170 | 183 | } |
171 | 184 | |
172 | 185 | List<EbCategorysRelation> cateRelations = ebCategorysRelationService.lambdaQuery() |
... | ... | @@ -198,6 +211,36 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im |
198 | 211 | return ServerResult.success(productVO); |
199 | 212 | } |
200 | 213 | |
214 | + //查找一级分类 | |
215 | + private ProductCategoryDO findParentCategory(List<String> categoryIds, String targetParentId) { | |
216 | + for (String categoryId : categoryIds) { | |
217 | + // 查询当前 categoryId 的数据 | |
218 | + ProductCategoryDO currentCategory = categoryService.lambdaQuery() | |
219 | + .eq(ProductCategoryDO::getId, categoryId) | |
220 | + .one(); | |
221 | + if (currentCategory != null) { | |
222 | + // 如果当前分类的 parentId 等于目标 parentId,返回当前分类 | |
223 | + if (targetParentId.equals(currentCategory.getParentId())) { | |
224 | + return currentCategory; | |
225 | + } | |
226 | + // 否则递归查找 parentId 的分类 | |
227 | + ProductCategoryDO parentCategory = categoryService.lambdaQuery() | |
228 | + .eq(ProductCategoryDO::getId, currentCategory.getParentId()) | |
229 | + .one(); | |
230 | + if (parentCategory != null) { | |
231 | + ProductCategoryDO foundCategory = findParentCategory( | |
232 | + Collections.singletonList(parentCategory.getId()), | |
233 | + targetParentId | |
234 | + ); | |
235 | + if (foundCategory != null) { | |
236 | + return foundCategory; | |
237 | + } | |
238 | + } | |
239 | + } | |
240 | + } | |
241 | + return null; // 如果未找到符合条件的分类,返回 null | |
242 | + } | |
243 | + | |
201 | 244 | /** |
202 | 245 | * 分页查询 |
203 | 246 | * | ... | ... |