diff --git a/framework/src/main/java/cn/lili/modules/goods/service/BrandService.java b/framework/src/main/java/cn/lili/modules/goods/service/BrandService.java index 622d735d..87421f65 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/BrandService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/BrandService.java @@ -25,6 +25,13 @@ public interface BrandService extends IService { */ IPage getBrandsByPage(BrandPageDTO page); + /** + * 删除品牌 + * + * @param ids 品牌id + */ + void deleteBrands(List ids); + /** * 根据分类ID获取品牌列表 * diff --git a/framework/src/main/java/cn/lili/modules/goods/service/CategoryBrandService.java b/framework/src/main/java/cn/lili/modules/goods/service/CategoryBrandService.java index 95703167..57a776bc 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/CategoryBrandService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/CategoryBrandService.java @@ -23,6 +23,7 @@ public interface CategoryBrandService extends IService { /** * 通过分类ID删除关联品牌 + * * @param categoryId 品牌ID */ void deleteByCategoryId(String categoryId); @@ -36,4 +37,12 @@ public interface CategoryBrandService extends IService { */ List getCategoryBrandListByBrandId(String brandId); + /** + * 保存分类品牌关系 + * + * @param categoryId 分类id + * @param brandIds 品牌ids + */ + void saveCategoryBrandList(String categoryId, List brandIds); + } \ No newline at end of file diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java index dd849c1e..f7b76d8f 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java @@ -88,20 +88,44 @@ public class BrandServiceImpl extends ServiceImpl implements Brand brand = this.checkExist(brandId); //如果是要禁用,则需要先判定绑定关系 if (Boolean.TRUE.equals(disable)) { - //分了绑定关系查询 - List categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandId); - if (!categoryBrands.isEmpty()) { - List brandIds = categoryBrands.stream().map(categoryBrand -> { - return categoryBrand.getCategoryId(); - }).collect(Collectors.toList()); - throw new ServiceException(ResultCode.BRAND_USE_DISABLE_ERROR, - JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(brandIds))); - } + checkoutCategory(brandId); } brand.setDeleteFlag(disable); return updateById(brand); } + @Override + public void deleteBrands(List ids) { + ids.forEach(id -> { + checkoutCategory(id); + }); + this.removeByIds(ids); + } + + + /** + * 校验绑定关系 + * + * @param brandId + */ + private void checkoutCategory(String brandId) { + //分了绑定关系查询 + List categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandId); + if (!categoryBrands.isEmpty()) { + List 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) { Brand brand = getById(brandId); if (brand == null) { diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryBrandServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryBrandServiceImpl.java index 622ebd20..4d0929f3 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryBrandServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryBrandServiceImpl.java @@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; /** @@ -38,4 +39,18 @@ public class CategoryBrandServiceImpl extends ServiceImpl getCategoryBrandListByBrandId(String brandId) { return this.list(new LambdaQueryWrapper().eq(CategoryBrand::getBrandId, brandId)); } + + @Override + public void saveCategoryBrandList(String categoryId, List brandIds) { + //删除分类品牌绑定信息 + this.deleteByCategoryId(categoryId); + //绑定品牌信息 + if (!brandIds.isEmpty()) { + List categoryBrands = new ArrayList<>(); + for (String brandId : brandIds) { + categoryBrands.add(new CategoryBrand(categoryId, brandId)); + } + this.saveBatch(categoryBrands); + } + } } \ No newline at end of file diff --git a/manager-api/src/main/java/cn/lili/controller/goods/BrandManagerController.java b/manager-api/src/main/java/cn/lili/controller/goods/BrandManagerController.java index 169a0e7d..9cab9a3c 100644 --- a/manager-api/src/main/java/cn/lili/controller/goods/BrandManagerController.java +++ b/manager-api/src/main/java/cn/lili/controller/goods/BrandManagerController.java @@ -97,7 +97,7 @@ public class BrandManagerController { @ApiImplicitParam(name = "ids", value = "品牌ID", required = true, dataType = "String", allowMultiple = true, paramType = "path") @DeleteMapping(value = "/delByIds/{ids}") public ResultMessage delAllByIds(@PathVariable List ids) { - brandService.removeByIds(ids); + brandService.deleteBrands(ids); return ResultUtil.success(ResultCode.SUCCESS); } diff --git a/manager-api/src/main/java/cn/lili/controller/goods/CategoryBrandManagerController.java b/manager-api/src/main/java/cn/lili/controller/goods/CategoryBrandManagerController.java index 0256364c..f7db55ee 100644 --- a/manager-api/src/main/java/cn/lili/controller/goods/CategoryBrandManagerController.java +++ b/manager-api/src/main/java/cn/lili/controller/goods/CategoryBrandManagerController.java @@ -46,14 +46,8 @@ public class CategoryBrandManagerController { @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, paramType = "path", dataType = "String"), @ApiImplicitParam(name = "categoryBrands", value = "品牌id数组", required = true, paramType = "query", dataType = "String[]") }) - public ResultMessage saveCategoryBrand(@PathVariable String categoryId, @RequestParam String[] categoryBrands) { - //删除分类品牌绑定信息 - this.categoryBrandService.remove(new QueryWrapper().eq("category_id", categoryId)); - //绑定品牌信息 - for (String brandId : categoryBrands) { - CategoryBrand categoryBrand = new CategoryBrand(categoryId, brandId); - categoryBrandService.save(categoryBrand); - } + public ResultMessage saveCategoryBrand(@PathVariable String categoryId, @RequestParam List categoryBrands) { + categoryBrandService.saveCategoryBrandList(categoryId,categoryBrands); return ResultUtil.success(); }