diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/resp/CategoryParentCommonElementResp.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/resp/CategoryParentCommonElementResp.java new file mode 100644 index 0000000..ba5576a --- /dev/null +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/resp/CategoryParentCommonElementResp.java @@ -0,0 +1,39 @@ +package com.muyu.product.domain.resp; + +import com.muyu.product.domain.AttributeGroup; +import com.muyu.product.domain.AttributeInfo; +import com.muyu.product.domain.BrandInfo; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * @author DongZl + * @description: 类别父通用元素 + * @Date 2024-3-1 上午 11:02 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CategoryParentCommonElementResp { + + /** + * 属性集合 + */ + private List attributeInfoList; + + /** + * 属性组集合 + */ + private List attributeGroupList; + + /** + * 品牌集合 + */ + private List brandInfoList; + +} diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java index f466e25..3bee271 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java @@ -5,7 +5,11 @@ import java.util.function.Supplier; import javax.servlet.http.HttpServletResponse; import com.muyu.common.security.utils.SecurityUtils; +import com.muyu.product.domain.AttributeGroup; +import com.muyu.product.domain.AttributeInfo; +import com.muyu.product.domain.BrandInfo; import com.muyu.product.domain.model.CategoryInfoSaveModel; +import com.muyu.product.domain.resp.CategoryParentCommonElementResp; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -111,4 +115,18 @@ public class CategoryInfoController extends BaseController { public Result remove(@PathVariable List ids) { return toAjax(categoryInfoService.removeBatchByIds(ids)); } + + /** + * 通过品类ID获取父级以上的属性、属性组、品牌集合 + * @param categoryId 品类ID + * @return 父级以上的属性、属性组、品牌集合 + */ + @GetMapping("/parentCommonElement/{categoryId}") + @ApiOperation("通过品类ID获取父级以上的属性集合") + @ApiImplicitParam(name = "categoryId", value = "categoryId", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class, example = "1") + public Result parentCommonElement( + @PathVariable(value = "categoryId") Long categoryId + ) { + return Result.success(categoryInfoService.parentCommonElement(categoryId)); + } } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java index 514d121..e24c0cb 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java @@ -1,9 +1,14 @@ package com.muyu.product.service; import java.util.List; + +import com.muyu.product.domain.AttributeGroup; +import com.muyu.product.domain.AttributeInfo; +import com.muyu.product.domain.BrandInfo; import com.muyu.product.domain.CategoryInfo; import com.baomidou.mybatisplus.extension.service.IService; import com.muyu.product.domain.model.CategoryInfoSaveModel; +import com.muyu.product.domain.resp.CategoryParentCommonElementResp; /** * 品类信息Service接口 @@ -27,4 +32,31 @@ public interface CategoryInfoService extends IService { */ public boolean save(CategoryInfoSaveModel categoryInfoSaveModel); + /** + * 通过品类ID获取父级以上的属性组集合 + * @param categoryId 品类ID + * @return 父级以上的属性组集合 + */ + List getAttributeGroup (Long categoryId); + + /** + * 通过品类ID获取父级以上的品牌集合 + * @param categoryId 品类ID + * @return 父级以上的品牌集合 + */ + List getBrand (Long categoryId); + + /** + * 通过品类ID获取父级以上的属性集合 + * @param categoryId 品类ID + * @return 父级以上的属性集合 + */ + List getAttribute (Long categoryId); + + /** + * 通过品类ID获取父级以上的属性、属性组、品牌集合 + * @param categoryId 品类ID + * @return 父级以上的属性、属性组、品牌集合 + */ + CategoryParentCommonElementResp parentCommonElement (Long categoryId); } 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 cc94ef4..b3e704a 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,21 +1,18 @@ package com.muyu.product.service.impl; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; import com.muyu.common.core.utils.ObjUtils; -import com.muyu.product.domain.AsCategoryAttribute; -import com.muyu.product.domain.AsCategoryAttributeGroup; -import com.muyu.product.domain.AsCategoryBrand; +import com.muyu.product.domain.*; import com.muyu.product.domain.model.CategoryInfoSaveModel; -import com.muyu.product.service.AsCategoryAttributeGroupService; -import com.muyu.product.service.AsCategoryAttributeService; -import com.muyu.product.service.AsCategoryBrandService; +import com.muyu.product.domain.resp.CategoryParentCommonElementResp; +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.muyu.product.domain.CategoryInfo; -import com.muyu.product.service.CategoryInfoService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.transaction.annotation.Transactional; @@ -40,6 +37,15 @@ public class CategoryInfoServiceImpl extends ServiceImpl getAttributeGroup (Long categoryId) { + List attributeGroupList = new ArrayList<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, categoryId); + List asCategoryAttributeGroupList = asCategoryAttributeGroupService.list(queryWrapper); + 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()){ + attributeGroupList.addAll(getAttributeGroup(categoryInfo.getParentId())); + }else { + List attributeGroups = getAttributeGroup(categoryInfo.getParentId()); + attributeGroups.forEach(attributeGroup -> { + if (!attributeGroupList.contains(attributeGroup)){ + attributeGroupList.add(attributeGroup); + } + }); + } + } + return attributeGroupList; + } + + /** + * 通过品类ID获取父级以上的品牌集合 + * + * @param categoryId 品类ID + * + * @return 父级以上的品牌集合 + */ + @Override + public List getBrand (Long categoryId) { + List brandInfoList = new ArrayList<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(AsCategoryBrand::getCategoryId, categoryId); + List asCategoryBrandList = asCategoryBrandService.list(queryWrapper); + 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()){ + brandInfoList.addAll(getBrand(categoryInfo.getParentId())); + }else { + List brandInfos = getBrand(categoryInfo.getParentId()); + brandInfos.forEach(brandInfo -> { + if (!brandInfoList.contains(brandInfo)){ + brandInfoList.add(brandInfo); + } + }); + } + + } + return brandInfoList; + } + + /** + * 通过品类ID获取父级以上的属性集合 + * + * @param categoryId 品类ID + * + * @return 父级以上的属性集合 + */ + @Override + public List getAttribute (Long categoryId) { + List attributeInfoList = new ArrayList<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(AsCategoryAttribute::getCategoryId, categoryId); + List asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper); + 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()){ + attributeInfoList.addAll(getAttribute(categoryInfo.getParentId())); + }else { + List attributeInfos = getAttribute(categoryInfo.getParentId()); + attributeInfos.forEach(attributeInfoQuery -> { + if (!attributeInfoList.contains(attributeInfoQuery)){ + attributeInfoList.add(attributeInfoQuery); + } + }); + } + } + return attributeInfoList; + } + + /** + * 通过品类ID获取父级以上的属性、属性组、品牌集合 + * + * @param categoryId 品类ID + * + * @return 父级以上的属性、属性组、品牌集合 + */ + @Override + public CategoryParentCommonElementResp parentCommonElement (Long categoryId) { + return CategoryParentCommonElementResp.builder() + .attributeInfoList(this.getAttribute(categoryId)) + .attributeGroupList(this.getAttributeGroup(categoryId)) + .brandInfoList(this.getBrand(categoryId)) + .build(); + } }