添加品牌分类关联

This commit is contained in:
Bunny 2025-07-09 15:55:06 +08:00
parent 509a1a95f7
commit 85f90b2042
6 changed files with 91 additions and 15 deletions

View File

@ -46,10 +46,17 @@ public class CategoryBrandRelationController {
return Result.success(pageResult);
}
@Operation(summary = "返回分类列表", description = "返回分类列表")
@GetMapping("category-list")
public Result<List<CategoryBrandRelationVo>> getCategoryList(Long brandId) {
List<CategoryBrandRelationVo> voList = categoryBrandRelationService.getCategoryList(brandId);
return Result.success(voList);
}
@Operation(summary = "添加品牌分类关联", description = "添加品牌分类关联")
@PostMapping()
public Result<String> addCategoryBrandRelation(@Valid @RequestBody CategoryBrandRelationDto dto) {
categoryBrandRelationService.addCategoryBrandRelation(dto);
public Result<String> saveCategoryBrandRelation(@Valid @RequestBody CategoryBrandRelationDto dto) {
categoryBrandRelationService.saveCategoryBrandRelation(dto);
return Result.success(ResultCodeEnum.ADD_SUCCESS);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -27,11 +27,20 @@ public interface CategoryBrandRelationService extends IService<CategoryBrandRela
PageResult<CategoryBrandRelationVo> getCategoryBrandRelationPage(Page<CategoryBrandRelationEntity> pageParams, CategoryBrandRelationDto dto);
/**
* 添加品牌分类关联
* 添加品牌分类关联关系
*
* @param dto {@link CategoryBrandRelationDto} 添加表单
* <p>实现逻辑
* 1. 根据品牌ID查询品牌信息获取品牌名称
* 2. 根据分类ID查询分类信息获取分类名称
* 3. 将品牌和分类的关联关系及冗余信息保存到关联表中
*
* <p>设计说明
* 采用冗余存储设计在关联表中同时存储品牌名称和分类名称
* 避免多表关联查询提升电商系统在大数据量场景下的查询性能
*
* @param dto 品牌分类关联数据 {@link CategoryBrandRelationDto}
*/
void addCategoryBrandRelation(CategoryBrandRelationDto dto);
void saveCategoryBrandRelation(CategoryBrandRelationDto dto);
/**
* 更新品牌分类关联
@ -46,4 +55,12 @@ public interface CategoryBrandRelationService extends IService<CategoryBrandRela
* @param ids 删除id列表
*/
void deleteCategoryBrandRelation(List<Long> ids);
/**
* 返回分类列表
*
* @param brandId 品牌id
* @return 品牌关联所有分类列表
*/
List<CategoryBrandRelationVo> getCategoryList(Long brandId);
}

View File

@ -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<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
private final CategoryMapper categoryMapper;
private final BrandMapper brandMapper;
/**
* * 品牌分类关联 服务实现类
*
@ -47,14 +57,34 @@ public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandR
}
/**
* 添加品牌分类关联
* 添加品牌分类关联关系
*
* @param dto 品牌分类关联添加
* <p>实现逻辑
* 1. 根据品牌ID查询品牌信息获取品牌名称
* 2. 根据分类ID查询分类信息获取分类名称
* 3. 将品牌和分类的关联关系及冗余信息保存到关联表中
*
* <p>设计说明
* 采用冗余存储设计在关联表中同时存储品牌名称和分类名称
* 避免多表关联查询提升电商系统在大数据量场景下的查询性能
*
* @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<CategoryBrandR
public void deleteCategoryBrandRelation(List<Long> ids) {
removeByIds(ids);
}
/**
* 返回分类列表
*
* @param brandId 品牌id
* @return 品牌关联所有分类列表
*/
@Override
public List<CategoryBrandRelationVo> getCategoryList(Long brandId) {
// 构建查询条件
QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(CategoryBrandRelationEntity::getBrandId, brandId);
// 查询品牌分类关联的实体类对象
List<CategoryBrandRelationEntity> list = list(wrapper);
return list.stream().map(categoryBrandRelationEntity -> {
CategoryBrandRelationVo vo = new CategoryBrandRelationVo();
BeanUtils.copyProperties(categoryBrandRelationEntity, vo);
return vo;
})
.toList();
}
}