分类绑定品牌规范问题处理

This commit is contained in:
Chopper 2021-09-22 12:13:04 +08:00
parent 57a5c5a0f1
commit 50b52f07c6
6 changed files with 67 additions and 18 deletions

View File

@ -25,6 +25,13 @@ public interface BrandService extends IService<Brand> {
*/ */
IPage<Brand> getBrandsByPage(BrandPageDTO page); IPage<Brand> getBrandsByPage(BrandPageDTO page);
/**
* 删除品牌
*
* @param ids 品牌id
*/
void deleteBrands(List<String> ids);
/** /**
* 根据分类ID获取品牌列表 * 根据分类ID获取品牌列表
* *

View File

@ -23,6 +23,7 @@ public interface CategoryBrandService extends IService<CategoryBrand> {
/** /**
* 通过分类ID删除关联品牌 * 通过分类ID删除关联品牌
*
* @param categoryId 品牌ID * @param categoryId 品牌ID
*/ */
void deleteByCategoryId(String categoryId); void deleteByCategoryId(String categoryId);
@ -36,4 +37,12 @@ public interface CategoryBrandService extends IService<CategoryBrand> {
*/ */
List<CategoryBrand> getCategoryBrandListByBrandId(String brandId); List<CategoryBrand> getCategoryBrandListByBrandId(String brandId);
/**
* 保存分类品牌关系
*
* @param categoryId 分类id
* @param brandIds 品牌ids
*/
void saveCategoryBrandList(String categoryId, List<String> brandIds);
} }

View File

@ -88,20 +88,44 @@ public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements
Brand brand = this.checkExist(brandId); Brand brand = this.checkExist(brandId);
//如果是要禁用则需要先判定绑定关系 //如果是要禁用则需要先判定绑定关系
if (Boolean.TRUE.equals(disable)) { if (Boolean.TRUE.equals(disable)) {
//分了绑定关系查询 checkoutCategory(brandId);
List<CategoryBrand> categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandId);
if (!categoryBrands.isEmpty()) {
List<String> brandIds = categoryBrands.stream().map(categoryBrand -> {
return categoryBrand.getCategoryId();
}).collect(Collectors.toList());
throw new ServiceException(ResultCode.BRAND_USE_DISABLE_ERROR,
JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(brandIds)));
}
} }
brand.setDeleteFlag(disable); brand.setDeleteFlag(disable);
return updateById(brand); return updateById(brand);
} }
@Override
public void deleteBrands(List<String> ids) {
ids.forEach(id -> {
checkoutCategory(id);
});
this.removeByIds(ids);
}
/**
* 校验绑定关系
*
* @param brandId
*/
private void checkoutCategory(String brandId) {
//分了绑定关系查询
List<CategoryBrand> categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandId);
if (!categoryBrands.isEmpty()) {
List<String> brandIds = categoryBrands.stream().map(categoryBrand -> {
return categoryBrand.getCategoryId();
}).collect(Collectors.toList());
throw new ServiceException(ResultCode.BRAND_USE_DISABLE_ERROR,
JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(brandIds)));
}
}
/**
* 校验是否存在
*
* @param brandId
* @return
*/
private Brand checkExist(String brandId) { private Brand checkExist(String brandId) {
Brand brand = getById(brandId); Brand brand = getById(brandId);
if (brand == null) { if (brand == null) {

View File

@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -38,4 +39,18 @@ public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, C
public List<CategoryBrand> getCategoryBrandListByBrandId(String brandId) { public List<CategoryBrand> getCategoryBrandListByBrandId(String brandId) {
return this.list(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getBrandId, brandId)); return this.list(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getBrandId, brandId));
} }
@Override
public void saveCategoryBrandList(String categoryId, List<String> brandIds) {
//删除分类品牌绑定信息
this.deleteByCategoryId(categoryId);
//绑定品牌信息
if (!brandIds.isEmpty()) {
List<CategoryBrand> categoryBrands = new ArrayList<>();
for (String brandId : brandIds) {
categoryBrands.add(new CategoryBrand(categoryId, brandId));
}
this.saveBatch(categoryBrands);
}
}
} }

View File

@ -97,7 +97,7 @@ public class BrandManagerController {
@ApiImplicitParam(name = "ids", value = "品牌ID", required = true, dataType = "String", allowMultiple = true, paramType = "path") @ApiImplicitParam(name = "ids", value = "品牌ID", required = true, dataType = "String", allowMultiple = true, paramType = "path")
@DeleteMapping(value = "/delByIds/{ids}") @DeleteMapping(value = "/delByIds/{ids}")
public ResultMessage<Object> delAllByIds(@PathVariable List<String> ids) { public ResultMessage<Object> delAllByIds(@PathVariable List<String> ids) {
brandService.removeByIds(ids); brandService.deleteBrands(ids);
return ResultUtil.success(ResultCode.SUCCESS); return ResultUtil.success(ResultCode.SUCCESS);
} }

View File

@ -46,14 +46,8 @@ public class CategoryBrandManagerController {
@ApiImplicitParam(name = "categoryId", value = "分类id", required = true, paramType = "path", dataType = "String"), @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, paramType = "path", dataType = "String"),
@ApiImplicitParam(name = "categoryBrands", value = "品牌id数组", required = true, paramType = "query", dataType = "String[]") @ApiImplicitParam(name = "categoryBrands", value = "品牌id数组", required = true, paramType = "query", dataType = "String[]")
}) })
public ResultMessage<Object> saveCategoryBrand(@PathVariable String categoryId, @RequestParam String[] categoryBrands) { public ResultMessage<Object> saveCategoryBrand(@PathVariable String categoryId, @RequestParam List<String> categoryBrands) {
//删除分类品牌绑定信息 categoryBrandService.saveCategoryBrandList(categoryId,categoryBrands);
this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("category_id", categoryId));
//绑定品牌信息
for (String brandId : categoryBrands) {
CategoryBrand categoryBrand = new CategoryBrand(categoryId, brandId);
categoryBrandService.save(categoryBrand);
}
return ResultUtil.success(); return ResultUtil.success();
} }