公共属性 - 属性组优化

master
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
* *
@ -31,7 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
*/ */
@Slf4j @Slf4j
@Service @Service
public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, CategoryInfo> implements CategoryInfoService { public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, CategoryInfo> implements CategoryInfoService {
@Autowired @Autowired
@ -59,30 +58,31 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
* *
* *
* @param categoryInfo * @param categoryInfo
*
* @return * @return
*/ */
@Override @Override
public List<CategoryInfo> list(CategoryInfo categoryInfo) { public List<CategoryInfo> list (CategoryInfo categoryInfo) {
LambdaQueryWrapper<CategoryInfo> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<CategoryInfo> queryWrapper = new LambdaQueryWrapper<>();
if (ObjUtils.notNull(categoryInfo.getName())){ if (ObjUtils.notNull(categoryInfo.getName())) {
queryWrapper.like(CategoryInfo::getName, categoryInfo.getName()); queryWrapper.like(CategoryInfo::getName, categoryInfo.getName());
} }
if (ObjUtils.notNull(categoryInfo.getImage())){ if (ObjUtils.notNull(categoryInfo.getImage())) {
queryWrapper.eq(CategoryInfo::getImage, categoryInfo.getImage()); queryWrapper.eq(CategoryInfo::getImage, categoryInfo.getImage());
} }
if (ObjUtils.notNull(categoryInfo.getParentId())){ if (ObjUtils.notNull(categoryInfo.getParentId())) {
queryWrapper.eq(CategoryInfo::getParentId, categoryInfo.getParentId()); queryWrapper.eq(CategoryInfo::getParentId, categoryInfo.getParentId());
} }
if (ObjUtils.notNull(categoryInfo.getStart())){ if (ObjUtils.notNull(categoryInfo.getStart())) {
queryWrapper.eq(CategoryInfo::getStart, categoryInfo.getStart()); queryWrapper.eq(CategoryInfo::getStart, categoryInfo.getStart());
} }
if (ObjUtils.notNull(categoryInfo.getIntroduction())){ if (ObjUtils.notNull(categoryInfo.getIntroduction())) {
queryWrapper.eq(CategoryInfo::getIntroduction, categoryInfo.getIntroduction()); queryWrapper.eq(CategoryInfo::getIntroduction, categoryInfo.getIntroduction());
} }
@ -91,12 +91,12 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
@Override @Override
@Transactional @Transactional
public boolean save(CategoryInfoSaveModel categoryInfoSaveModel) { public boolean save (CategoryInfoSaveModel categoryInfoSaveModel) {
CategoryInfo categoryInfo = CategoryInfo.saveModelBuild(categoryInfoSaveModel); CategoryInfo categoryInfo = CategoryInfo.saveModelBuild(categoryInfoSaveModel);
boolean save = this.save(categoryInfo); boolean save = this.save(categoryInfo);
Long categoryInfoId = categoryInfo.getId(); Long categoryInfoId = categoryInfo.getId();
List<Long> attributeIdList = categoryInfoSaveModel.getAttributeIdList(); List<Long> attributeIdList = categoryInfoSaveModel.getAttributeIdList();
if (attributeIdList != null && !attributeIdList.isEmpty()){ if (attributeIdList != null && !attributeIdList.isEmpty()) {
asCategoryAttributeService.saveBatch( asCategoryAttributeService.saveBatch(
attributeIdList.stream() attributeIdList.stream()
.map(attributeId -> AsCategoryAttribute.categoryBuild(categoryInfoId, attributeId)) .map(attributeId -> AsCategoryAttribute.categoryBuild(categoryInfoId, attributeId))
@ -104,7 +104,7 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
); );
} }
List<Long> attributeGroupIdList = categoryInfoSaveModel.getAttributeGroupIdList(); List<Long> attributeGroupIdList = categoryInfoSaveModel.getAttributeGroupIdList();
if (attributeGroupIdList != null && !attributeGroupIdList.isEmpty()){ if (attributeGroupIdList != null && !attributeGroupIdList.isEmpty()) {
asCategoryAttributeGroupService.saveBatch( asCategoryAttributeGroupService.saveBatch(
attributeGroupIdList.stream() attributeGroupIdList.stream()
.map(attributeGroupId -> AsCategoryAttributeGroup.categoryBuild(categoryInfoId, attributeGroupId)) .map(attributeGroupId -> AsCategoryAttributeGroup.categoryBuild(categoryInfoId, attributeGroupId))
@ -112,7 +112,7 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
); );
} }
List<Long> brandIdList = categoryInfoSaveModel.getBrandIdList(); List<Long> brandIdList = categoryInfoSaveModel.getBrandIdList();
if (brandIdList != null && !brandIdList.isEmpty()){ if (brandIdList != null && !brandIdList.isEmpty()) {
asCategoryBrandService.saveBatch( asCategoryBrandService.saveBatch(
brandIdList.stream() brandIdList.stream()
.map(brandId -> AsCategoryBrand.categoryBuild(categoryInfoId, brandId)) .map(brandId -> AsCategoryBrand.categoryBuild(categoryInfoId, brandId))
@ -135,20 +135,20 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
LambdaQueryWrapper<AsCategoryAttributeGroup> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AsCategoryAttributeGroup> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, categoryId); queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, categoryId);
List<AsCategoryAttributeGroup> asCategoryAttributeGroupList = asCategoryAttributeGroupService.list(queryWrapper); List<AsCategoryAttributeGroup> asCategoryAttributeGroupList = asCategoryAttributeGroupService.list(queryWrapper);
if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()){ if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()) {
List<Long> attributeGroupIdList = asCategoryAttributeGroupList.stream() List<Long> attributeGroupIdList = asCategoryAttributeGroupList.stream()
.map(AsCategoryAttributeGroup::getAttributeGroupId) .map(AsCategoryAttributeGroup::getAttributeGroupId)
.toList(); .toList();
attributeGroupList.addAll(attributeGroupService.listByIds(attributeGroupIdList)); attributeGroupList.addAll(attributeGroupService.listByIds(attributeGroupIdList));
} }
CategoryInfo categoryInfo = this.getById(categoryId); CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){ if (categoryInfo.getParentId() != 0) {
if (attributeGroupList.isEmpty()){ if (attributeGroupList.isEmpty()) {
attributeGroupList.addAll(getAttributeGroup(categoryInfo.getParentId())); attributeGroupList.addAll(getAttributeGroup(categoryInfo.getParentId()));
}else { } else {
List<AttributeGroup> attributeGroups = getAttributeGroup(categoryInfo.getParentId()); List<AttributeGroup> attributeGroups = getAttributeGroup(categoryInfo.getParentId());
attributeGroups.forEach(attributeGroup -> { attributeGroups.forEach(attributeGroup -> {
if (!attributeGroupList.contains(attributeGroup)){ if (!attributeGroupList.contains(attributeGroup)) {
attributeGroupList.add(attributeGroup); attributeGroupList.add(attributeGroup);
} }
}); });
@ -170,20 +170,20 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
LambdaQueryWrapper<AsCategoryBrand> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AsCategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryBrand::getCategoryId, categoryId); queryWrapper.eq(AsCategoryBrand::getCategoryId, categoryId);
List<AsCategoryBrand> asCategoryBrandList = asCategoryBrandService.list(queryWrapper); List<AsCategoryBrand> asCategoryBrandList = asCategoryBrandService.list(queryWrapper);
if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()){ if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()) {
List<Long> brandIdList = asCategoryBrandList.stream() List<Long> brandIdList = asCategoryBrandList.stream()
.map(AsCategoryBrand::getBrandId) .map(AsCategoryBrand::getBrandId)
.toList(); .toList();
brandInfoList.addAll(brandInfoService.listByIds(brandIdList)); brandInfoList.addAll(brandInfoService.listByIds(brandIdList));
} }
CategoryInfo categoryInfo = this.getById(categoryId); CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){ if (categoryInfo.getParentId() != 0) {
if (brandInfoList.isEmpty()){ if (brandInfoList.isEmpty()) {
brandInfoList.addAll(getBrand(categoryInfo.getParentId())); brandInfoList.addAll(getBrand(categoryInfo.getParentId()));
}else { } else {
List<BrandInfo> brandInfos = getBrand(categoryInfo.getParentId()); List<BrandInfo> brandInfos = getBrand(categoryInfo.getParentId());
brandInfos.forEach(brandInfo -> { brandInfos.forEach(brandInfo -> {
if (!brandInfoList.contains(brandInfo)){ if (!brandInfoList.contains(brandInfo)) {
brandInfoList.add(brandInfo); brandInfoList.add(brandInfo);
} }
}); });
@ -206,20 +206,20 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
LambdaQueryWrapper<AsCategoryAttribute> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AsCategoryAttribute> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryAttribute::getCategoryId, categoryId); queryWrapper.eq(AsCategoryAttribute::getCategoryId, categoryId);
List<AsCategoryAttribute> asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper); List<AsCategoryAttribute> asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper);
if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){ if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()) {
List<Long> attributeIdList = asCategoryAttributeList.stream() List<Long> attributeIdList = asCategoryAttributeList.stream()
.map(AsCategoryAttribute::getAttributeId) .map(AsCategoryAttribute::getAttributeId)
.toList(); .toList();
attributeInfoList.addAll(attributeInfoService.listByIds(attributeIdList)); attributeInfoList.addAll(attributeInfoService.listByIds(attributeIdList));
} }
CategoryInfo categoryInfo = this.getById(categoryId); CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){ if (categoryInfo.getParentId() != 0) {
if (attributeInfoList.isEmpty()){ if (attributeInfoList.isEmpty()) {
attributeInfoList.addAll(getAttribute(categoryInfo.getParentId())); attributeInfoList.addAll(getAttribute(categoryInfo.getParentId()));
}else { } else {
List<AttributeInfo> attributeInfos = getAttribute(categoryInfo.getParentId()); List<AttributeInfo> attributeInfos = getAttribute(categoryInfo.getParentId());
attributeInfos.forEach(attributeInfoQuery -> { attributeInfos.forEach(attributeInfoQuery -> {
if (!attributeInfoList.contains(attributeInfoQuery)){ if (!attributeInfoList.contains(attributeInfoQuery)) {
attributeInfoList.add(attributeInfoQuery); attributeInfoList.add(attributeInfoQuery);
} }
}); });
@ -229,16 +229,15 @@ 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<>();
asQueryWrapper.eq("category_id", categoryId); asQueryWrapper.eq("category_id", categoryId);
List<AS> asList = iService.list(asQueryWrapper); List<AS> asList = iService.list(asQueryWrapper);
if (asList != null && !asList.isEmpty()){ if (asList != null && !asList.isEmpty()) {
List<Long> baseIdList = asList.stream() List<Long> baseIdList = asList.stream()
.map(as -> { .map(as -> {
if (as instanceof CategoryBase categoryBase){ if (as instanceof CategoryBase categoryBase) {
return categoryBase.getBaseId(); return categoryBase.getBaseId();
} }
return null; return null;
@ -247,13 +246,13 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
list.addAll(bsiService.listByIds(baseIdList)); list.addAll(bsiService.listByIds(baseIdList));
} }
CategoryInfo categoryInfo = this.getById(categoryId); CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){ if (categoryInfo.getParentId() != 0) {
if (list.isEmpty()){ if (list.isEmpty()) {
list.addAll(getCommon(categoryInfo.getParentId(), iService, bsiService)); list.addAll(getCommon(categoryInfo.getParentId(), iService, bsiService));
}else { } else {
List<T> commonList = getCommon(categoryInfo.getParentId(), iService, bsiService); List<T> commonList = getCommon(categoryInfo.getParentId(), iService, bsiService);
commonList.forEach(common -> { commonList.forEach(common -> {
if (!list.contains(common)){ if (!list.contains(common)) {
list.add(common); list.add(common);
} }
}); });
@ -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<>();