From 8a7844fcd8f9527d1e077c30f308e76dbe4d1228 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Wed, 9 Jul 2025 16:24:55 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=9B=B4=E6=96=B0=E5=93=81?= =?UTF-8?q?=E7=89=8C=E6=97=B6=E5=90=8C=E6=97=B6=E6=9B=B4=E6=96=B0=E5=93=81?= =?UTF-8?q?=E7=89=8C=E5=88=86=E7=B1=BB=E5=85=B3=E7=B3=BB=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CategoryController.java | 4 ++-- .../mall/product/domain/dto/CategoryDto.java | 3 +++ .../mapper/CategoryBrandRelationMapper.java | 7 ++++++ .../mall/product/service/BrandService.java | 2 ++ .../service/CategoryBrandRelationService.java | 1 + .../mall/product/service/CategoryService.java | 3 ++- .../service/impl/BrandServiceImpl.java | 22 ++++++++++++++++- .../service/impl/CategoryServiceImpl.java | 15 +++++++++++- .../mapper/CategoryBrandRelationMapper.xml | 24 +++++++++++++++++++ 9 files changed, 76 insertions(+), 5 deletions(-) diff --git a/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryController.java b/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryController.java index dbe052e..36200f2 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryController.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/controller/CategoryController.java @@ -55,8 +55,8 @@ public class CategoryController { @Operation(summary = "添加商品三级分类", description = "添加商品三级分类") @PostMapping() - public Result addCategory(@Valid @RequestBody CategoryDto dto) { - categoryService.addCategory(dto); + public Result saveCategory(@Valid @RequestBody CategoryDto dto) { + categoryService.saveCategory(dto); return Result.success(ResultCodeEnum.ADD_SUCCESS); } diff --git a/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryDto.java b/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryDto.java index 894db94..0a7bda3 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryDto.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/domain/dto/CategoryDto.java @@ -1,5 +1,7 @@ package com.mall.product.domain.dto; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -14,6 +16,7 @@ import lombok.NoArgsConstructor; public class CategoryDto { @Schema(name = "catId", title = "分类id") + @TableId(type = IdType.ASSIGN_ID) private Long catId; @Schema(name = "name", title = "分类名称") diff --git a/mall-services/mall-product/src/main/java/com/mall/product/mapper/CategoryBrandRelationMapper.java b/mall-services/mall-product/src/main/java/com/mall/product/mapper/CategoryBrandRelationMapper.java index e21ae69..e0c61b8 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/mapper/CategoryBrandRelationMapper.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/mapper/CategoryBrandRelationMapper.java @@ -29,4 +29,11 @@ public interface CategoryBrandRelationMapper extends BaseMapper selectListByPage(@Param("page") Page pageParams, @Param("dto") CategoryBrandRelationDto dto); + /** + * 根据品牌id更新 品牌分类关系表 + * 根据分类id跟新,更新分类时同时更新品牌和分类关系表 + * + * @param dto 品牌分类关联的DTO对象 + */ + void updateByBrandIdAndCatId(CategoryBrandRelationDto dto); } diff --git a/mall-services/mall-product/src/main/java/com/mall/product/service/BrandService.java b/mall-services/mall-product/src/main/java/com/mall/product/service/BrandService.java index 89cc09e..fa71a75 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/service/BrandService.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/service/BrandService.java @@ -35,6 +35,8 @@ public interface BrandService extends IService { /** * 更新品牌 + * 先更新自己的品牌数据 + * 之后更新冗余表中的数据 * * @param dto {@link BrandDto} 更新表单 */ 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 661f7cc..6c5c02a 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 @@ -63,4 +63,5 @@ public interface CategoryBrandRelationService extends IService getCategoryList(Long brandId); + } diff --git a/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryService.java b/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryService.java index aa6e187..ae3bf9c 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryService.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/service/CategoryService.java @@ -28,10 +28,11 @@ public interface CategoryService extends IService { /** * 添加商品三级分类 + * 添加分类需要同时更新:品牌和分类关联关系 * * @param dto {@link CategoryDto} 添加表单 */ - void addCategory(CategoryDto dto); + void saveCategory(CategoryDto dto); /** * 更新商品三级分类 diff --git a/mall-services/mall-product/src/main/java/com/mall/product/service/impl/BrandServiceImpl.java b/mall-services/mall-product/src/main/java/com/mall/product/service/impl/BrandServiceImpl.java index 53de709..006ea65 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/service/impl/BrandServiceImpl.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/service/impl/BrandServiceImpl.java @@ -5,10 +5,14 @@ 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.BrandDto; +import com.mall.product.domain.dto.CategoryBrandRelationDto; import com.mall.product.domain.entity.BrandEntity; import com.mall.product.domain.vo.BrandVo; import com.mall.product.mapper.BrandMapper; +import com.mall.product.mapper.CategoryBrandRelationMapper; import com.mall.product.service.BrandService; +import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -25,8 +29,11 @@ import java.util.List; */ @Service @Transactional +@RequiredArgsConstructor public class BrandServiceImpl extends ServiceImpl implements BrandService { + private final CategoryBrandRelationMapper categoryBrandRelationMapper; + /** * * 品牌 服务实现类 * @@ -60,14 +67,27 @@ public class BrandServiceImpl extends ServiceImpl impl /** * 更新品牌 + * 先更新自己的品牌数据 + * 之后更新冗余表中的数据 * - * @param dto 品牌更新 + * @param dto {@link BrandDto} 更新表单 */ @Override public void updateBrand(BrandDto dto) { BrandEntity brandEntity = new BrandEntity(); BeanUtils.copyProperties(dto, brandEntity); updateById(brandEntity); + + // 品牌名不为空保存品牌名 + String name = brandEntity.getName(); + if (StringUtils.isNotBlank(name)) { + // 根据品牌id更新 品牌分类关系表 + CategoryBrandRelationDto categoryBrandRelationDto = CategoryBrandRelationDto.builder() + .brandId(dto.getBrandId()) + .brandName(name) + .build(); + categoryBrandRelationMapper.updateByBrandIdAndCatId(categoryBrandRelationDto); + } } /** diff --git a/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryServiceImpl.java b/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryServiceImpl.java index 5ab985e..391a254 100644 --- a/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryServiceImpl.java +++ b/mall-services/mall-product/src/main/java/com/mall/product/service/impl/CategoryServiceImpl.java @@ -4,13 +4,16 @@ 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.dto.CategoryDto; import com.mall.product.domain.entity.CategoryEntity; import com.mall.product.domain.vo.CategoryVo; +import com.mall.product.mapper.CategoryBrandRelationMapper; import com.mall.product.mapper.CategoryMapper; import com.mall.product.service.CategoryService; import com.mall.product.service.ext.CategoryServiceImplExt; import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -36,6 +39,7 @@ import static com.mall.product.service.ext.CategoryServiceImplExt.getCategoryTre public class CategoryServiceImpl extends ServiceImpl implements CategoryService { private final CategoryServiceImplExt categoryServiceImplExt; + private final CategoryBrandRelationMapper categoryBrandRelationMapper; /** * * 商品三级分类 服务实现类 @@ -62,7 +66,7 @@ public class CategoryServiceImpl extends ServiceImpl + + update pms_category_brand_relation + + + brand_name = #{name}, + + + catelog_name = #{name}, + + + where + + + brand_id = #{brandId} + + + catelog_id = #{catId} + + + 1=0 + + + +