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 92e7d01..d76b88c 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 @@ -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业务层处理 * @@ -31,7 +30,7 @@ import org.springframework.transaction.annotation.Transactional; */ @Slf4j @Service -public class CategoryInfoServiceImpl extends ServiceImpl implements CategoryInfoService { +public class CategoryInfoServiceImpl extends ServiceImpl implements CategoryInfoService { @Autowired @@ -59,30 +58,31 @@ public class CategoryInfoServiceImpl extends ServiceImpl list(CategoryInfo categoryInfo) { + public List list (CategoryInfo categoryInfo) { LambdaQueryWrapper 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 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 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 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 queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, categoryId); List asCategoryAttributeGroupList = asCategoryAttributeGroupService.list(queryWrapper); - if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()){ + if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()) { List 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 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 queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AsCategoryBrand::getCategoryId, categoryId); List asCategoryBrandList = asCategoryBrandService.list(queryWrapper); - if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()){ + if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()) { List 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 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 queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AsCategoryAttribute::getCategoryId, categoryId); List asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper); - if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){ + if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()) { List 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 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 List getCommon(Long categoryId,IService iService,IService bsiService){ + public List getCommon (Long categoryId, IService iService, IService bsiService) { List list = new ArrayList(); QueryWrapper asQueryWrapper = new QueryWrapper<>(); asQueryWrapper.eq("category_id", categoryId); List asList = iService.list(asQueryWrapper); - if (asList != null && !asList.isEmpty()){ + if (asList != null && !asList.isEmpty()) { List 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 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 attributeGroupModelList = new ArrayList<>(); // 取出和品类相关联的属性组关系 - 中间表 - LambdaQueryWrapper asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(); - asCategoryAttributeGroupLambdaQueryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId); + LambdaQueryWrapper asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{ + eq(AsCategoryAttributeGroup::getCategoryId, cateGoryId); + }}; List categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper); - - // 根据中间表取出属性组 信息 - for (AsCategoryAttributeGroup asCategoryAttributeGroup : categoryAttributeGroupList) { - AttributeGroup attributeGroup = attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()); - // 根据属性组信息 取出和属性组相关联的属性信息 - LambdaQueryWrapper asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>(); - asAttributeGroupQueryWrapper.eq(AsAttributeGroup::getGroupId, attributeGroup.getId()); - List asAttributeGroupList = asAttributeGroupService.list(asAttributeGroupQueryWrapper); - List 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 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 templateAttributeModelList = new ArrayList<>();