公共属性 - 属性组优化

dev
DongZeLiang 2024-03-06 15:34:24 +08:00
parent 29e536f837
commit e2c7f8e070
1 changed files with 58 additions and 70 deletions

View File

@ -1,11 +1,9 @@
package com.muyu.product.service.impl; package com.muyu.product.service.impl;
import java.util.ArrayList; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.List;
import java.util.function.Function;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.ObjUtils; import com.muyu.common.core.utils.ObjUtils;
import com.muyu.product.domain.*; import com.muyu.product.domain.*;
import com.muyu.product.domain.base.CategoryBase; import com.muyu.product.domain.base.CategoryBase;
@ -14,15 +12,16 @@ import com.muyu.product.domain.model.TemplateAttributeGroupModel;
import com.muyu.product.domain.model.TemplateAttributeModel; import com.muyu.product.domain.model.TemplateAttributeModel;
import com.muyu.product.domain.resp.CategoryCommonElementResp; import com.muyu.product.domain.resp.CategoryCommonElementResp;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp; import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import com.muyu.product.mapper.CategoryInfoMapper;
import com.muyu.product.service.*; import com.muyu.product.service.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.muyu.product.mapper.CategoryInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/** /**
* Service * Service
* *
@ -59,6 +58,7 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
* *
* *
* @param categoryInfo * @param categoryInfo
*
* @return * @return
*/ */
@Override @Override
@ -229,7 +229,6 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
} }
public <T, AS> List<T> getCommon (Long categoryId, IService<AS> iService, IService<T> bsiService) { public <T, AS> List<T> getCommon (Long categoryId, IService<AS> iService, IService<T> bsiService) {
List<T> list = new ArrayList(); List<T> list = new ArrayList();
QueryWrapper<AS> asQueryWrapper = new QueryWrapper<>(); QueryWrapper<AS> asQueryWrapper = new QueryWrapper<>();
@ -287,37 +286,26 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
*/ */
@Override @Override
public CategoryCommonElementResp getTemplateAttributeByCateGoryId (Long cateGoryId) { public CategoryCommonElementResp getTemplateAttributeByCateGoryId (Long cateGoryId) {
List<TemplateAttributeGroupModel> attributeGroupModelList = new ArrayList<>();
// 取出和品类相关联的属性组关系 - 中间表 // 取出和品类相关联的属性组关系 - 中间表
LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{
asCategoryAttributeGroupLambdaQueryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId); eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId);
}};
List<AsCategoryAttributeGroup> categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper); List<AsCategoryAttributeGroup> categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper);
List<TemplateAttributeGroupModel> attributeGroupModelList = categoryAttributeGroupList.stream()
// 根据中间表取出属性组 信息 .map(asCategoryAttributeGroup -> TemplateAttributeGroupModel.attributeGroupBuild(
for (AsCategoryAttributeGroup asCategoryAttributeGroup : categoryAttributeGroupList) { attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()),
AttributeGroup attributeGroup = attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()); attributeGroupId -> attributeInfoService.listByIds(
// 根据属性组信息 取出和属性组相关联的属性信息 asAttributeGroupService.list(
LambdaQueryWrapper<AsAttributeGroup> asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>(); new LambdaQueryWrapper<>(){{
asAttributeGroupQueryWrapper.eq(AsAttributeGroup::getGroupId, attributeGroup.getId()); eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId());
List<AsAttributeGroup> asAttributeGroupList = asAttributeGroupService.list(asAttributeGroupQueryWrapper); }}
List<TemplateAttributeModel> attributeModelList = new ArrayList<>(); ).stream().map(AsAttributeGroup::getAttributeId).toList()
for (AsAttributeGroup asAttributeGroup : asAttributeGroupList) { ).stream()
AttributeInfo attributeInfo = attributeInfoService.getById(asAttributeGroup.getAttributeId()); .map(AttributeInfo::buildTemplateModel)
attributeModelList.add( .toList()
TemplateAttributeModel.builder() ))
.id(attributeInfo.getId()) .filter(TemplateAttributeGroupModel::isEffective)
.code(attributeInfo.getCode()) .toList();
.name(attributeInfo.getName())
.build()
);
}
attributeGroupModelList.add(
TemplateAttributeGroupModel.builder()
.groupName(attributeGroup.getName())
.attributeList(attributeModelList)
.build()
);
}
List<TemplateAttributeModel> templateAttributeModelList = new ArrayList<>(); List<TemplateAttributeModel> templateAttributeModelList = new ArrayList<>();