diff --git a/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryBrandRelationController.java b/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryBrandRelationController.java index 97e77d5..0f5de28 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryBrandRelationController.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryBrandRelationController.java @@ -46,10 +46,17 @@ public class CategoryBrandRelationController { return Result.success(pageResult); } + @Operation(summary = "返回分类列表", description = "返回分类列表") + @GetMapping("category-list") + public Result> getCategoryList(Long brandId) { + List voList = categoryBrandRelationService.getCategoryList(brandId); + return Result.success(voList); + } + @Operation(summary = "添加品牌分类关联", description = "添加品牌分类关联") @PostMapping() - public Result addCategoryBrandRelation(@Valid @RequestBody CategoryBrandRelationDto dto) { - categoryBrandRelationService.addCategoryBrandRelation(dto); + public Result saveCategoryBrandRelation(@Valid @RequestBody CategoryBrandRelationDto dto) { + categoryBrandRelationService.saveCategoryBrandRelation(dto); return Result.success(ResultCodeEnum.ADD_SUCCESS); } diff --git a/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryBrandRelationDto.java b/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryBrandRelationDto.java index 593a14c..1c29bf3 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryBrandRelationDto.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryBrandRelationDto.java @@ -22,10 +22,10 @@ public class CategoryBrandRelationDto { @Schema(name = "catelogId", title = "分类id") private Long catelogId; - @Schema(name = "brandName", title = "") + @Schema(name = "brandName", title = "品牌名称") private String brandName; - @Schema(name = "catelogName", title = "") + @Schema(name = "catelogName", title = "分类名称") private String catelogName; } \ No newline at end of file diff --git a/mall-services/mall-product/src/main/java/com/mall/product/domain/entity/CategoryBrandRelationEntity.java b/mall-services/mall-product/src/main/java/com/mall/product/domain/entity/CategoryBrandRelationEntity.java index d519a6c..364d15b 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/domain/entity/CategoryBrandRelationEntity.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/domain/entity/CategoryBrandRelationEntity.java @@ -25,10 +25,10 @@ public class CategoryBrandRelationEntity { @Schema(name = "catelogId", title = "分类id") private Long catelogId; - @Schema(name = "brandName", title = "") + @Schema(name = "brandName", title = "品牌名称") private String brandName; - @Schema(name = "catelogName", title = "") + @Schema(name = "catelogName", title = "分类名称") private String catelogName; } \ No newline at end of file diff --git a/mall-services/mall-product/src/main/java/com/mall/product/domain/vo/CategoryBrandRelationVo.java b/mall-services/mall-product/src/main/java/com/mall/product/domain/vo/CategoryBrandRelationVo.java index e8b40fe..254baab 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/domain/vo/CategoryBrandRelationVo.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/domain/vo/CategoryBrandRelationVo.java @@ -2,7 +2,6 @@ package com.mall.product.domain.vo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; -import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @@ -21,10 +20,10 @@ public class CategoryBrandRelationVo { @Schema(name = "catelogId", title = "分类id") private Long catelogId; - @Schema(name = "brandName", title = "") + @Schema(name = "brandName", title = "品牌名称") private String brandName; - @Schema(name = "catelogName", title = "") + @Schema(name = "catelogName", title = "分类名称") private String catelogName; } diff --git a/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryBrandRelationService.java b/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryBrandRelationService.java index da3dc3d..661f7cc 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryBrandRelationService.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryBrandRelationService.java @@ -27,11 +27,20 @@ public interface CategoryBrandRelationService extends IService getCategoryBrandRelationPage(Page pageParams, CategoryBrandRelationDto dto); /** - * 添加品牌分类关联 + * 添加品牌与分类的关联关系 * - * @param dto {@link CategoryBrandRelationDto} 添加表单 + *

实现逻辑: + * 1. 根据品牌ID查询品牌信息,获取品牌名称 + * 2. 根据分类ID查询分类信息,获取分类名称 + * 3. 将品牌和分类的关联关系及冗余信息保存到关联表中 + * + *

设计说明: + * 采用冗余存储设计,在关联表中同时存储品牌名称和分类名称, + * 避免多表关联查询,提升电商系统在大数据量场景下的查询性能 + * + * @param dto 品牌分类关联数据 {@link CategoryBrandRelationDto} */ - void addCategoryBrandRelation(CategoryBrandRelationDto dto); + void saveCategoryBrandRelation(CategoryBrandRelationDto dto); /** * 更新品牌分类关联 @@ -46,4 +55,12 @@ public interface CategoryBrandRelationService extends IService ids); + + /** + * 返回分类列表 + * + * @param brandId 品牌id + * @return 品牌关联所有分类列表 + */ + List getCategoryList(Long brandId); } diff --git a/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryBrandRelationServiceImpl.java b/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryBrandRelationServiceImpl.java index ac610b4..18bebe7 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryBrandRelationServiceImpl.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryBrandRelationServiceImpl.java @@ -1,14 +1,20 @@ package com.mall.product.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mall.common.domain.vo.result.PageResult; import com.mall.product.domain.dto.CategoryBrandRelationDto; +import com.mall.product.domain.entity.BrandEntity; import com.mall.product.domain.entity.CategoryBrandRelationEntity; +import com.mall.product.domain.entity.CategoryEntity; import com.mall.product.domain.vo.CategoryBrandRelationVo; +import com.mall.product.mapper.BrandMapper; import com.mall.product.mapper.CategoryBrandRelationMapper; +import com.mall.product.mapper.CategoryMapper; import com.mall.product.service.CategoryBrandRelationService; +import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -25,8 +31,12 @@ import java.util.List; */ @Service @Transactional +@RequiredArgsConstructor public class CategoryBrandRelationServiceImpl extends ServiceImpl implements CategoryBrandRelationService { + private final CategoryMapper categoryMapper; + private final BrandMapper brandMapper; + /** * * 品牌分类关联 服务实现类 * @@ -47,14 +57,34 @@ public class CategoryBrandRelationServiceImpl extends ServiceImpl实现逻辑: + * 1. 根据品牌ID查询品牌信息,获取品牌名称 + * 2. 根据分类ID查询分类信息,获取分类名称 + * 3. 将品牌和分类的关联关系及冗余信息保存到关联表中 + * + *

设计说明: + * 采用冗余存储设计,在关联表中同时存储品牌名称和分类名称, + * 避免多表关联查询,提升电商系统在大数据量场景下的查询性能 + * + * @param dto 品牌分类关联数据 {@link CategoryBrandRelationDto} */ @Override - public void addCategoryBrandRelation(CategoryBrandRelationDto dto) { + public void saveCategoryBrandRelation(CategoryBrandRelationDto dto) { + // 根据品牌id查询品牌信息 + Long brandId = dto.getBrandId(); + BrandEntity brandEntity = brandMapper.selectById(brandId); + + // 根据分类id查询分类信息 + Long catelogId = dto.getCatelogId(); + CategoryEntity categoryEntity = categoryMapper.selectById(catelogId); + + // 将查询到的 CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity(); BeanUtils.copyProperties(dto, categoryBrandRelationEntity); + categoryBrandRelationEntity.setBrandName(brandEntity.getName()); + categoryBrandRelationEntity.setCatelogName(categoryEntity.getName()); save(categoryBrandRelationEntity); } @@ -79,4 +109,27 @@ public class CategoryBrandRelationServiceImpl extends ServiceImpl ids) { removeByIds(ids); } + + /** + * 返回分类列表 + * + * @param brandId 品牌id + * @return 品牌关联所有分类列表 + */ + @Override + public List getCategoryList(Long brandId) { + // 构建查询条件 + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.lambda().eq(CategoryBrandRelationEntity::getBrandId, brandId); + + // 查询品牌分类关联的实体类对象 + List list = list(wrapper); + return list.stream().map(categoryBrandRelationEntity -> { + CategoryBrandRelationVo vo = new CategoryBrandRelationVo(); + BeanUtils.copyProperties(categoryBrandRelationEntity, vo); + return vo; + }) + .toList(); + } + } \ No newline at end of file