分类绑定品牌规范问题处理
This commit is contained in:
parent
57a5c5a0f1
commit
50b52f07c6
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Object> delAllByIds(@PathVariable List<String> ids) {
|
||||
brandService.removeByIds(ids);
|
||||
brandService.deleteBrands(ids);
|
||||
return ResultUtil.success(ResultCode.SUCCESS);
|
||||
}
|
||||
|
||||
|
@ -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<Object> saveCategoryBrand(@PathVariable String categoryId, @RequestParam String[] categoryBrands) {
|
||||
//删除分类品牌绑定信息
|
||||
this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("category_id", categoryId));
|
||||
//绑定品牌信息
|
||||
for (String brandId : categoryBrands) {
|
||||
CategoryBrand categoryBrand = new CategoryBrand(categoryId, brandId);
|
||||
categoryBrandService.save(categoryBrand);
|
||||
}
|
||||
public ResultMessage<Object> saveCategoryBrand(@PathVariable String categoryId, @RequestParam List<String> categoryBrands) {
|
||||
categoryBrandService.saveCategoryBrandList(categoryId,categoryBrands);
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user