公共属性 - 属性组优化

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;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.product.domain.*;
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.resp.CategoryCommonElementResp;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import com.muyu.product.mapper.CategoryInfoMapper;
import com.muyu.product.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.ArrayList;
import java.util.List;
/**
* Service
*
@ -59,30 +58,31 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
*
*
* @param categoryInfo
*
* @return
*/
@Override
public List<CategoryInfo> list(CategoryInfo categoryInfo) {
public List<CategoryInfo> list (CategoryInfo categoryInfo) {
LambdaQueryWrapper<CategoryInfo> queryWrapper = new LambdaQueryWrapper<>();
if (ObjUtils.notNull(categoryInfo.getName())){
if (ObjUtils.notNull(categoryInfo.getName())) {
queryWrapper.like(CategoryInfo::getName, categoryInfo.getName());
}
if (ObjUtils.notNull(categoryInfo.getImage())){
if (ObjUtils.notNull(categoryInfo.getImage())) {
queryWrapper.eq(CategoryInfo::getImage, categoryInfo.getImage());
}
if (ObjUtils.notNull(categoryInfo.getParentId())){
if (ObjUtils.notNull(categoryInfo.getParentId())) {
queryWrapper.eq(CategoryInfo::getParentId, categoryInfo.getParentId());
}
if (ObjUtils.notNull(categoryInfo.getStart())){
if (ObjUtils.notNull(categoryInfo.getStart())) {
queryWrapper.eq(CategoryInfo::getStart, categoryInfo.getStart());
}
if (ObjUtils.notNull(categoryInfo.getIntroduction())){
if (ObjUtils.notNull(categoryInfo.getIntroduction())) {
queryWrapper.eq(CategoryInfo::getIntroduction, categoryInfo.getIntroduction());
}
@ -91,12 +91,12 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
@Override
@Transactional
public boolean save(CategoryInfoSaveModel categoryInfoSaveModel) {
public boolean save (CategoryInfoSaveModel categoryInfoSaveModel) {
CategoryInfo categoryInfo = CategoryInfo.saveModelBuild(categoryInfoSaveModel);
boolean save = this.save(categoryInfo);
Long categoryInfoId = categoryInfo.getId();
List<Long> attributeIdList = categoryInfoSaveModel.getAttributeIdList();
if (attributeIdList != null && !attributeIdList.isEmpty()){
if (attributeIdList != null && !attributeIdList.isEmpty()) {
asCategoryAttributeService.saveBatch(
attributeIdList.stream()
.map(attributeId -> AsCategoryAttribute.categoryBuild(categoryInfoId, attributeId))
@ -104,7 +104,7 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
);
}
List<Long> attributeGroupIdList = categoryInfoSaveModel.getAttributeGroupIdList();
if (attributeGroupIdList != null && !attributeGroupIdList.isEmpty()){
if (attributeGroupIdList != null && !attributeGroupIdList.isEmpty()) {
asCategoryAttributeGroupService.saveBatch(
attributeGroupIdList.stream()
.map(attributeGroupId -> AsCategoryAttributeGroup.categoryBuild(categoryInfoId, attributeGroupId))
@ -112,7 +112,7 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
);
}
List<Long> brandIdList = categoryInfoSaveModel.getBrandIdList();
if (brandIdList != null && !brandIdList.isEmpty()){
if (brandIdList != null && !brandIdList.isEmpty()) {
asCategoryBrandService.saveBatch(
brandIdList.stream()
.map(brandId -> AsCategoryBrand.categoryBuild(categoryInfoId, brandId))
@ -135,20 +135,20 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
LambdaQueryWrapper<AsCategoryAttributeGroup> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, categoryId);
List<AsCategoryAttributeGroup> asCategoryAttributeGroupList = asCategoryAttributeGroupService.list(queryWrapper);
if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()){
if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()) {
List<Long> attributeGroupIdList = asCategoryAttributeGroupList.stream()
.map(AsCategoryAttributeGroup::getAttributeGroupId)
.toList();
attributeGroupList.addAll(attributeGroupService.listByIds(attributeGroupIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (attributeGroupList.isEmpty()){
if (categoryInfo.getParentId() != 0) {
if (attributeGroupList.isEmpty()) {
attributeGroupList.addAll(getAttributeGroup(categoryInfo.getParentId()));
}else {
} else {
List<AttributeGroup> attributeGroups = getAttributeGroup(categoryInfo.getParentId());
attributeGroups.forEach(attributeGroup -> {
if (!attributeGroupList.contains(attributeGroup)){
if (!attributeGroupList.contains(attributeGroup)) {
attributeGroupList.add(attributeGroup);
}
});
@ -170,20 +170,20 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
LambdaQueryWrapper<AsCategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryBrand::getCategoryId, categoryId);
List<AsCategoryBrand> asCategoryBrandList = asCategoryBrandService.list(queryWrapper);
if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()){
if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()) {
List<Long> brandIdList = asCategoryBrandList.stream()
.map(AsCategoryBrand::getBrandId)
.toList();
brandInfoList.addAll(brandInfoService.listByIds(brandIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (brandInfoList.isEmpty()){
if (categoryInfo.getParentId() != 0) {
if (brandInfoList.isEmpty()) {
brandInfoList.addAll(getBrand(categoryInfo.getParentId()));
}else {
} else {
List<BrandInfo> brandInfos = getBrand(categoryInfo.getParentId());
brandInfos.forEach(brandInfo -> {
if (!brandInfoList.contains(brandInfo)){
if (!brandInfoList.contains(brandInfo)) {
brandInfoList.add(brandInfo);
}
});
@ -206,20 +206,20 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
LambdaQueryWrapper<AsCategoryAttribute> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryAttribute::getCategoryId, categoryId);
List<AsCategoryAttribute> asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper);
if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){
if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()) {
List<Long> attributeIdList = asCategoryAttributeList.stream()
.map(AsCategoryAttribute::getAttributeId)
.toList();
attributeInfoList.addAll(attributeInfoService.listByIds(attributeIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (attributeInfoList.isEmpty()){
if (categoryInfo.getParentId() != 0) {
if (attributeInfoList.isEmpty()) {
attributeInfoList.addAll(getAttribute(categoryInfo.getParentId()));
}else {
} else {
List<AttributeInfo> attributeInfos = getAttribute(categoryInfo.getParentId());
attributeInfos.forEach(attributeInfoQuery -> {
if (!attributeInfoList.contains(attributeInfoQuery)){
if (!attributeInfoList.contains(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();
QueryWrapper<AS> asQueryWrapper = new QueryWrapper<>();
asQueryWrapper.eq("category_id", categoryId);
List<AS> asList = iService.list(asQueryWrapper);
if (asList != null && !asList.isEmpty()){
if (asList != null && !asList.isEmpty()) {
List<Long> baseIdList = asList.stream()
.map(as -> {
if (as instanceof CategoryBase categoryBase){
if (as instanceof CategoryBase categoryBase) {
return categoryBase.getBaseId();
}
return null;
@ -247,13 +246,13 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
list.addAll(bsiService.listByIds(baseIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (list.isEmpty()){
if (categoryInfo.getParentId() != 0) {
if (list.isEmpty()) {
list.addAll(getCommon(categoryInfo.getParentId(), iService, bsiService));
}else {
} else {
List<T> commonList = getCommon(categoryInfo.getParentId(), iService, bsiService);
commonList.forEach(common -> {
if (!list.contains(common)){
if (!list.contains(common)) {
list.add(common);
}
});
@ -287,37 +286,26 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
*/
@Override
public CategoryCommonElementResp getTemplateAttributeByCateGoryId (Long cateGoryId) {
List<TemplateAttributeGroupModel> attributeGroupModelList = new ArrayList<>();
// 取出和品类相关联的属性组关系 - 中间表
LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
asCategoryAttributeGroupLambdaQueryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId);
LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{
eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId);
}};
List<AsCategoryAttributeGroup> categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper);
// 根据中间表取出属性组 信息
for (AsCategoryAttributeGroup asCategoryAttributeGroup : categoryAttributeGroupList) {
AttributeGroup attributeGroup = attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId());
// 根据属性组信息 取出和属性组相关联的属性信息
LambdaQueryWrapper<AsAttributeGroup> asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>();
asAttributeGroupQueryWrapper.eq(AsAttributeGroup::getGroupId, attributeGroup.getId());
List<AsAttributeGroup> asAttributeGroupList = asAttributeGroupService.list(asAttributeGroupQueryWrapper);
List<TemplateAttributeModel> attributeModelList = new ArrayList<>();
for (AsAttributeGroup asAttributeGroup : asAttributeGroupList) {
AttributeInfo attributeInfo = attributeInfoService.getById(asAttributeGroup.getAttributeId());
attributeModelList.add(
TemplateAttributeModel.builder()
.id(attributeInfo.getId())
.code(attributeInfo.getCode())
.name(attributeInfo.getName())
.build()
);
}
attributeGroupModelList.add(
TemplateAttributeGroupModel.builder()
.groupName(attributeGroup.getName())
.attributeList(attributeModelList)
.build()
);
}
List<TemplateAttributeGroupModel> 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()
))
.filter(TemplateAttributeGroupModel::isEffective)
.toList();
List<TemplateAttributeModel> templateAttributeModelList = new ArrayList<>();