分类绑定品牌规范问题处理
This commit is contained in:
@@ -25,6 +25,13 @@ public interface BrandService extends IService<Brand> {
|
||||
*/
|
||||
IPage<Brand> getBrandsByPage(BrandPageDTO page);
|
||||
|
||||
/**
|
||||
* 删除品牌
|
||||
*
|
||||
* @param ids 品牌id
|
||||
*/
|
||||
void deleteBrands(List<String> ids);
|
||||
|
||||
/**
|
||||
* 根据分类ID获取品牌列表
|
||||
*
|
||||
|
||||
@@ -23,6 +23,7 @@ public interface CategoryBrandService extends IService<CategoryBrand> {
|
||||
|
||||
/**
|
||||
* 通过分类ID删除关联品牌
|
||||
*
|
||||
* @param categoryId 品牌ID
|
||||
*/
|
||||
void deleteByCategoryId(String categoryId);
|
||||
@@ -36,4 +37,12 @@ public interface CategoryBrandService extends IService<CategoryBrand> {
|
||||
*/
|
||||
List<CategoryBrand> getCategoryBrandListByBrandId(String brandId);
|
||||
|
||||
/**
|
||||
* 保存分类品牌关系
|
||||
*
|
||||
* @param categoryId 分类id
|
||||
* @param brandIds 品牌ids
|
||||
*/
|
||||
void saveCategoryBrandList(String categoryId, List<String> brandIds);
|
||||
|
||||
}
|
||||
@@ -88,20 +88,44 @@ public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements
|
||||
Brand brand = this.checkExist(brandId);
|
||||
//如果是要禁用,则需要先判定绑定关系
|
||||
if (Boolean.TRUE.equals(disable)) {
|
||||
//分了绑定关系查询
|
||||
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)));
|
||||
}
|
||||
checkoutCategory(brandId);
|
||||
}
|
||||
brand.setDeleteFlag(disable);
|
||||
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) {
|
||||
Brand brand = getById(brandId);
|
||||
if (brand == null) {
|
||||
|
||||
@@ -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<CategoryBrandMapper, C
|
||||
public List<CategoryBrand> getCategoryBrandListByBrandId(String 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user