查询品牌的关联分类
需求
后端
AdminCategoryBrandRelationController.java
/**
* 获取当前品牌关联的所有分类列表
*/
@ApiOperation("获取当前品牌关联的所有分类列表")
@GetMapping("/category/list/{brandId}")
public R categoryList(@PathVariable Long brandId){
QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("brand_id",brandId);
List<CategoryBrandRelationEntity> data = relationService.list(queryWrapper);
return R.ok().put("data", data);
}
新增品牌的关联分类
需求
后端
controller
AdminCategoryBrandRelationController.java
/**
* 保存
*/
@PostMapping("/save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
relationService.saveDetail(categoryBrandRelation);
return R.ok();
}
service
CategoryBrandRelationService.java
void saveDetail(CategoryBrandRelationEntity categoryBrandRelation);
CategoryBrandRelationServiceImpl.java
@Override
public void saveDetail(CategoryBrandRelationEntity relation) {
Long brandId = relation.getBrandId();
Long categoryId = relation.getCategoryId();
// 1、查询详细名字
BrandEntity brand = brandService.getBaseMapper().selectById(brandId);
CategoryEntity category = categoryService.getBaseMapper().selectById(categoryId);
relation.setBrandName(brand.getName());
relation.setCategoryName(category.getName());
this.save(relation);
}
优化更新品牌
更新品牌时,还需要更新品牌与分类关联表中的品牌数据,否则会导致数据不一致
品牌
AdminBrandController.java
/**
* 修改
*/
@ApiOperation("修改品牌")
@PostMapping("/update")
public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
brandService.updateDetail(brand);
return R.ok();
}
BrandService.java
void updateDetail(BrandEntity brand);
BrandServiceImpl.java
@Autowired
private CategoryBrandRelationService relationService;
@Transactional
@Override
public void updateDetail(BrandEntity brand) {
// 保证冗余字段的数据一致
this.updateById(brand);
if (StringUtils.isNotEmpty(brand.getName())) {
// 同步更新其它关联表的数据
relationService.updateBrand(brand.getBrandId(), brand.getName());
// TODO 更新其它关联
}
}
品牌分类关联
CategoryBrandRelationService.java
void updateBrand(Long brandId, String name);
CategoryBrandRelationServiceImpl.java
@Override
public void updateBrand(Long brandId, String name) {
CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
relation.setBrandId(brandId);
relation.setBrandName(name);
UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("brand_id",brandId);
this.update(relation,updateWrapper);
}
优化更新分类
更新分类时,还需要更新品牌与分类关联表中的分类数据,否则会导致数据不一致
分类
AdminCategoryController.java
@ApiOperation("修改分类")
@PostMapping("/update")
public R update(@RequestBody CategoryEntity category) {
categoryService.updateDetail(category);
return R.ok();
}
CategoryService.java
void updateDetail(CategoryEntity category);
CategoryServiceImpl.java
@Autowired
private CategoryBrandRelationService relationService;
@Transactional
@Override
public void updateDetail(CategoryEntity category) {
this.updateById(category);
if (StringUtils.isNotEmpty(category.getName())) {
relationService.updateCategory(category.getCatId(),category.getName());
// TODO 更新其它关联
}
}
品牌分类关联
CategoryBrandRelationService.java
void updateCategory(Long catId, String name);
CategoryBrandRelationServiceImpl.java
@Override
public void updateCategory(Long catId, String name) {
CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
relation.setCategoryId(catId);
relation.setCategoryName(name);
UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("category_id",catId);
this.update(relation,updateWrapper);
}