更新品牌时同时更新品牌分类关系表

This commit is contained in:
Bunny 2025-07-09 16:24:55 +08:00
parent 14d77a07af
commit 8a7844fcd8
9 changed files with 76 additions and 5 deletions

View File

@ -55,8 +55,8 @@ public class CategoryController {
@Operation(summary = "添加商品三级分类", description = "添加商品三级分类")
@PostMapping()
public Result<String> addCategory(@Valid @RequestBody CategoryDto dto) {
categoryService.addCategory(dto);
public Result<String> saveCategory(@Valid @RequestBody CategoryDto dto) {
categoryService.saveCategory(dto);
return Result.success(ResultCodeEnum.ADD_SUCCESS);
}

View File

@ -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 = "分类名称")

View File

@ -29,4 +29,11 @@ public interface CategoryBrandRelationMapper extends BaseMapper<CategoryBrandRel
*/
IPage<CategoryBrandRelationVo> selectListByPage(@Param("page") Page<CategoryBrandRelationEntity> pageParams, @Param("dto") CategoryBrandRelationDto dto);
/**
* 根据品牌id更新 品牌分类关系表
* 根据分类id跟新更新分类时同时更新品牌和分类关系表
*
* @param dto 品牌分类关联的DTO对象
*/
void updateByBrandIdAndCatId(CategoryBrandRelationDto dto);
}

View File

@ -35,6 +35,8 @@ public interface BrandService extends IService<BrandEntity> {
/**
* 更新品牌
* 先更新自己的品牌数据
* 之后更新冗余表中的数据
*
* @param dto {@link BrandDto} 更新表单
*/

View File

@ -63,4 +63,5 @@ public interface CategoryBrandRelationService extends IService<CategoryBrandRela
* @return 品牌关联所有分类列表
*/
List<CategoryBrandRelationVo> getCategoryList(Long brandId);
}

View File

@ -28,10 +28,11 @@ public interface CategoryService extends IService<CategoryEntity> {
/**
* 添加商品三级分类
* 添加分类需要同时更新品牌和分类关联关系
*
* @param dto {@link CategoryDto} 添加表单
*/
void addCategory(CategoryDto dto);
void saveCategory(CategoryDto dto);
/**
* 更新商品三级分类

View File

@ -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<BrandMapper, BrandEntity> implements BrandService {
private final CategoryBrandRelationMapper categoryBrandRelationMapper;
/**
* * 品牌 服务实现类
*
@ -60,14 +67,27 @@ public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandEntity> 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);
}
}
/**

View File

@ -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<CategoryMapper, CategoryEntity> implements CategoryService {
private final CategoryServiceImplExt categoryServiceImplExt;
private final CategoryBrandRelationMapper categoryBrandRelationMapper;
/**
* * 商品三级分类 服务实现类
@ -62,7 +66,7 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEnt
* @param dto 商品三级分类添加
*/
@Override
public void addCategory(CategoryDto dto) {
public void saveCategory(CategoryDto dto) {
CategoryEntity categoryEntity = new CategoryEntity();
BeanUtils.copyProperties(dto, categoryEntity);
save(categoryEntity);
@ -78,6 +82,15 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEnt
CategoryEntity categoryEntity = new CategoryEntity();
BeanUtils.copyProperties(dto, categoryEntity);
updateById(categoryEntity);
// 根据分类id跟新更新分类时同时更新品牌和分类关系表
String name = categoryEntity.getName();
if (StringUtils.isNotBlank(name)) {
CategoryBrandRelationDto categoryBrandRelationDto = CategoryBrandRelationDto.builder()
.catelogId(dto.getCatId())
.catelogName(name).build();
categoryBrandRelationMapper.updateByBrandIdAndCatId(categoryBrandRelationDto);
}
}
/**

View File

@ -16,6 +16,30 @@
id,brand_id,catelog_id,brand_name,catelog_name
</sql>
<update id="updateByBrandIdAndCatId">
update pms_category_brand_relation
<set>
<if test="name != null and brandId != null">
brand_name = #{name},
</if>
<if test="name != null and catId != null">
catelog_name = #{name},
</if>
</set>
where
<choose>
<when test="brandId != null">
brand_id = #{brandId}
</when>
<when test="catId != null">
catelog_id = #{catId}
</when>
<otherwise>
1=0 <!-- 如果没有提供任何ID条件则不更新任何记录 -->
</otherwise>
</choose>
</update>
<!-- 分页查询品牌分类关联内容 -->
<select id="selectListByPage" resultType="com.mall.product.domain.vo.CategoryBrandRelationVo">
select