公共元素优化

master
DongZeLiang 2024-03-06 17:13:34 +08:00
parent 74b67dae1c
commit dc1e669dc5
1 changed files with 57 additions and 10 deletions

View File

@ -21,6 +21,8 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
/** /**
* Service * Service
@ -277,6 +279,15 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
.build(); .build();
} }
private void getParentIdListByCateGoryId(List<Long> parentIdList, Long cateGoryId){
if (cateGoryId.equals(0L)){
return;
}
CategoryInfo categoryInfo = this.getById(cateGoryId);
parentIdList.add(categoryInfo.getId());
getParentIdListByCateGoryId(parentIdList, categoryInfo.getParentId());
}
/** /**
* ID * ID
* *
@ -286,31 +297,67 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
*/ */
@Override @Override
public CategoryCommonElementResp getTemplateAttributeByCateGoryId (Long cateGoryId) { public CategoryCommonElementResp getTemplateAttributeByCateGoryId (Long cateGoryId) {
List<Long> cateGoryIdList = new ArrayList<>();
getParentIdListByCateGoryId(cateGoryIdList, cateGoryId);
// 取出和品类相关联的属性组关系 - 中间表 // 取出和品类相关联的属性组关系 - 中间表
LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{ LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{
eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId); in(AsCategoryAttributeGroup::getCategoryId, cateGoryIdList);
}}; }};
List<AsCategoryAttributeGroup> categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper); List<AsCategoryAttributeGroup> categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper);
List<TemplateAttributeGroupModel> attributeGroupModelList = categoryAttributeGroupList.stream() List<TemplateAttributeGroupModel> attributeGroupModelList = categoryAttributeGroupList.stream()
.map(asCategoryAttributeGroup -> TemplateAttributeGroupModel.attributeGroupBuild( .map(asCategoryAttributeGroup -> TemplateAttributeGroupModel.attributeGroupBuild(
attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()), attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()),
attributeGroupId -> attributeInfoService.listByIds( attributeGroupId -> {
asAttributeGroupService.list( LambdaQueryWrapper<AsAttributeGroup> asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>() {{
new LambdaQueryWrapper<>(){{ eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId());
eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId()); }};
}} List<Long> attributeIdList = asAttributeGroupService.list(asAttributeGroupQueryWrapper).stream()
).stream().map(AsAttributeGroup::getAttributeId).toList() .map(AsAttributeGroup::getAttributeId)
).stream() .toList();
.map(AttributeInfo::buildTemplateModel) if (attributeIdList.isEmpty()){
.toList() return new ArrayList<>();
}
return attributeInfoService.listByIds(attributeIdList).stream()
.map(AttributeInfo::buildTemplateModel)
.toList();
}
)) ))
.filter(TemplateAttributeGroupModel::isEffective) .filter(TemplateAttributeGroupModel::isEffective)
.toList(); .toList();
List<TemplateAttributeModel> templateAttributeModelList = new ArrayList<>(); List<TemplateAttributeModel> templateAttributeModelList = new ArrayList<>();
LambdaQueryWrapper<AsCategoryAttribute> categoryAttributeQueryWrapper = new LambdaQueryWrapper<>();
categoryAttributeQueryWrapper.in(AsCategoryAttribute::getCategoryId, cateGoryIdList);
List<AsCategoryAttribute> asCategoryAttributeList = asCategoryAttributeService.list(categoryAttributeQueryWrapper);
if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){
templateAttributeModelList = attributeInfoService.listByIds(
asCategoryAttributeList.stream()
.map(AsCategoryAttribute::getAttributeId)
.toList()
).stream()
.map(AttributeInfo::buildTemplateModel)
.toList();
}
List<TemplateAttributeModel> attributeModelList = new ArrayList<>(); List<TemplateAttributeModel> attributeModelList = new ArrayList<>();
List<Long> attributeIdList = new ArrayList<>();
if (!templateAttributeModelList.isEmpty()){
attributeIdList.addAll(
templateAttributeModelList.stream().map(TemplateAttributeModel::getId).toList()
);
}
if (!attributeGroupModelList.isEmpty()){
attributeIdList.addAll(
attributeGroupModelList.stream()
.flatMap((Function<TemplateAttributeGroupModel, Stream<TemplateAttributeModel>>) templateAttributeGroupModel -> templateAttributeGroupModel.getAttributeList().stream())
.map(TemplateAttributeModel::getId)
.toList()
);
}
LambdaQueryWrapper<AttributeInfo> attributeInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
attributeInfoLambdaQueryWrapper.notIn(!attributeIdList.isEmpty(), AttributeInfo::getId, attributeIdList);
attributeModelList = attributeInfoService.list(attributeInfoLambdaQueryWrapper).stream().map(AttributeInfo::buildTemplateModel).toList();
return CategoryCommonElementResp.builder() return CategoryCommonElementResp.builder()
.templateAttributeGroupList(attributeGroupModelList) .templateAttributeGroupList(attributeGroupModelList)