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 9fbd687..9868a25 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 @@ -20,7 +20,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.function.Function; import java.util.stream.Stream; @@ -324,30 +326,11 @@ public class CategoryInfoServiceImpl extends ServiceImpl 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() - ); - } + // 查重集合 + Set attributeIdSet = new HashSet<>(); + // 获取组内所有的属性Id if (!attributeGroupModelList.isEmpty()){ - attributeIdList.addAll( + attributeIdSet.addAll( attributeGroupModelList.stream() .flatMap((Function>) templateAttributeGroupModel -> templateAttributeGroupModel.getAttributeList().stream()) .map(TemplateAttributeModel::getId) @@ -355,8 +338,30 @@ public class CategoryInfoServiceImpl extends ServiceImpl templateAttributeModelList = new ArrayList<>(); + LambdaQueryWrapper categoryAttributeQueryWrapper = new LambdaQueryWrapper<>(); + categoryAttributeQueryWrapper.in(AsCategoryAttribute::getCategoryId, cateGoryIdList); + List asCategoryAttributeList = asCategoryAttributeService.list(categoryAttributeQueryWrapper); + if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){ + List templateAttributeIdList = asCategoryAttributeList.stream() + .map(AsCategoryAttribute::getAttributeId) + .filter(templateAttributeId -> !attributeIdSet.contains(templateAttributeId)) + .toList(); + templateAttributeModelList = attributeInfoService.listByIds(templateAttributeIdList).stream() + .map(AttributeInfo::buildTemplateModel) + .toList(); + } + + List attributeModelList = new ArrayList<>(); + + if (!templateAttributeModelList.isEmpty()){ + attributeIdSet.addAll( + templateAttributeModelList.stream().map(TemplateAttributeModel::getId).toList() + ); + } + LambdaQueryWrapper attributeInfoLambdaQueryWrapper = new LambdaQueryWrapper<>(); - attributeInfoLambdaQueryWrapper.notIn(!attributeIdList.isEmpty(), AttributeInfo::getId, attributeIdList); + attributeInfoLambdaQueryWrapper.notIn(!attributeIdSet.isEmpty(), AttributeInfo::getId, attributeIdSet); attributeModelList = attributeInfoService.list(attributeInfoLambdaQueryWrapper).stream().map(AttributeInfo::buildTemplateModel).toList(); return CategoryCommonElementResp.builder()