diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java index d76b88c..9fbd687 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java @@ -21,6 +21,8 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; +import java.util.stream.Stream; /** * 品类信息Service业务层处理 @@ -277,6 +279,15 @@ public class CategoryInfoServiceImpl extends ServiceImpl parentIdList, Long cateGoryId){ + if (cateGoryId.equals(0L)){ + return; + } + CategoryInfo categoryInfo = this.getById(cateGoryId); + parentIdList.add(categoryInfo.getId()); + getParentIdListByCateGoryId(parentIdList, categoryInfo.getParentId()); + } + /** * 通过品类ID获取品类共有属性 * @@ -286,31 +297,67 @@ public class CategoryInfoServiceImpl extends ServiceImpl cateGoryIdList = new ArrayList<>(); + getParentIdListByCateGoryId(cateGoryIdList, cateGoryId); // 取出和品类相关联的属性组关系 - 中间表 LambdaQueryWrapper asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{ - eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId); + in(AsCategoryAttributeGroup::getCategoryId, cateGoryIdList); }}; List categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper); List attributeGroupModelList = categoryAttributeGroupList.stream() .map(asCategoryAttributeGroup -> TemplateAttributeGroupModel.attributeGroupBuild( attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()), - attributeGroupId -> attributeInfoService.listByIds( - asAttributeGroupService.list( - new LambdaQueryWrapper<>(){{ - eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId()); - }} - ).stream().map(AsAttributeGroup::getAttributeId).toList() - ).stream() - .map(AttributeInfo::buildTemplateModel) - .toList() + attributeGroupId -> { + LambdaQueryWrapper asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>() {{ + eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId()); + }}; + List attributeIdList = asAttributeGroupService.list(asAttributeGroupQueryWrapper).stream() + .map(AsAttributeGroup::getAttributeId) + .toList(); + if (attributeIdList.isEmpty()){ + return new ArrayList<>(); + } + return attributeInfoService.listByIds(attributeIdList).stream() + .map(AttributeInfo::buildTemplateModel) + .toList(); + } )) .filter(TemplateAttributeGroupModel::isEffective) .toList(); List templateAttributeModelList = new ArrayList<>(); + LambdaQueryWrapper categoryAttributeQueryWrapper = new LambdaQueryWrapper<>(); + categoryAttributeQueryWrapper.in(AsCategoryAttribute::getCategoryId, cateGoryIdList); + List asCategoryAttributeList = asCategoryAttributeService.list(categoryAttributeQueryWrapper); + if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){ + templateAttributeModelList = attributeInfoService.listByIds( + asCategoryAttributeList.stream() + .map(AsCategoryAttribute::getAttributeId) + .toList() + ).stream() + .map(AttributeInfo::buildTemplateModel) + .toList(); + } List attributeModelList = new ArrayList<>(); + List attributeIdList = new ArrayList<>(); + if (!templateAttributeModelList.isEmpty()){ + attributeIdList.addAll( + templateAttributeModelList.stream().map(TemplateAttributeModel::getId).toList() + ); + } + if (!attributeGroupModelList.isEmpty()){ + attributeIdList.addAll( + attributeGroupModelList.stream() + .flatMap((Function>) templateAttributeGroupModel -> templateAttributeGroupModel.getAttributeList().stream()) + .map(TemplateAttributeModel::getId) + .toList() + ); + } + LambdaQueryWrapper attributeInfoLambdaQueryWrapper = new LambdaQueryWrapper<>(); + attributeInfoLambdaQueryWrapper.notIn(!attributeIdList.isEmpty(), AttributeInfo::getId, attributeIdList); + attributeModelList = attributeInfoService.list(attributeInfoLambdaQueryWrapper).stream().map(AttributeInfo::buildTemplateModel).toList(); return CategoryCommonElementResp.builder() .templateAttributeGroupList(attributeGroupModelList)