diff --git a/framework/src/main/java/cn/lili/common/enums/ResultCode.java b/framework/src/main/java/cn/lili/common/enums/ResultCode.java index 56c2ac34..d93ebb7f 100644 --- a/framework/src/main/java/cn/lili/common/enums/ResultCode.java +++ b/framework/src/main/java/cn/lili/common/enums/ResultCode.java @@ -99,7 +99,8 @@ public enum ResultCode { BRAND_DISABLE_ERROR(14003, "品牌禁用失败"), BRAND_DELETE_ERROR(14004, "品牌删除失败"), BRAND_NAME_EXIST_ERROR(20002, "品牌名称重复!"), - BRAND_USE_DISABLE_ERROR(20003, "分类已经绑定此品牌,请先解除关联"), + BRAND_USE_DISABLE_ERROR(20003, "分类已经绑定品牌,请先解除关联"), + BRAND_BIND_GOODS_ERROR(20005, "品牌已经绑定商品,请先解除关联"), BRAND_NOT_EXIST(20004, "品牌不存在"), /** 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 57a776bc..32f84616 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 @@ -35,7 +35,7 @@ public interface CategoryBrandService extends IService { * @param brandId 品牌ID * @return 分类品牌关联信息 */ - List getCategoryBrandListByBrandId(String brandId); + List getCategoryBrandListByBrandId(List brandId); /** * 保存分类品牌关系 diff --git a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java index 2c6e03fb..dbf17a60 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java @@ -20,6 +20,13 @@ import java.util.List; public interface GoodsService extends IService { + /** + * 根据品牌获取商品 + * + * @param brandIds 品牌ids + */ + List getByBrandIds(List brandIds); + /** * 下架所有商家商品 * 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 05612b92..2b66bb3b 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 @@ -5,12 +5,14 @@ import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; import cn.lili.modules.goods.entity.dos.Brand; import cn.lili.modules.goods.entity.dos.CategoryBrand; +import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dto.BrandPageDTO; import cn.lili.modules.goods.entity.vos.BrandVO; import cn.lili.modules.goods.mapper.BrandMapper; import cn.lili.modules.goods.service.BrandService; import cn.lili.modules.goods.service.CategoryBrandService; import cn.lili.modules.goods.service.CategoryService; +import cn.lili.modules.goods.service.GoodsService; import cn.lili.mybatis.util.PageUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -21,6 +23,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; @@ -44,6 +47,9 @@ public class BrandServiceImpl extends ServiceImpl implements @Autowired private CategoryService categoryService; + @Autowired + private GoodsService goodsService; + @Override public IPage getBrandsByPage(BrandPageDTO page) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); @@ -88,7 +94,9 @@ public class BrandServiceImpl extends ServiceImpl implements Brand brand = this.checkExist(brandId); //如果是要禁用,则需要先判定绑定关系 if (Boolean.TRUE.equals(disable)) { - checkoutCategory(brandId); + List ids = new ArrayList<>(); + ids.add(brandId); + checkBind(ids); } brand.setDeleteFlag(disable); return updateById(brand); @@ -96,7 +104,7 @@ public class BrandServiceImpl extends ServiceImpl implements @Override public void deleteBrands(List ids) { - ids.forEach(this::checkoutCategory); + checkBind(ids); this.removeByIds(ids); } @@ -104,16 +112,32 @@ public class BrandServiceImpl extends ServiceImpl implements /** * 校验绑定关系 * - * @param brandId + * @param brandIds */ - private void checkoutCategory(String brandId) { + private void checkBind(List brandIds) { //分了绑定关系查询 - List categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandId); + List categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandIds); if (!categoryBrands.isEmpty()) { - List brandIds = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList()); + List categoryIds = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList()); throw new ServiceException(ResultCode.BRAND_USE_DISABLE_ERROR, - JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(brandIds))); + JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(categoryIds))); } + + //分了商品绑定关系查询 + List goods = goodsService.getByBrandIds(brandIds); + if (!goods.isEmpty()) { + List goodsNames = goods.stream().map(Goods::getGoodsName).collect(Collectors.toList()); + throw new ServiceException(ResultCode.BRAND_BIND_GOODS_ERROR, + JSONUtil.toJsonStr(goodsNames)); + } + } + + /** + * 校验绑定关系 + * + * @param brandIds + */ + private void checkoutGoods(List brandIds) { } /** 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 4d0929f3..bd88e375 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 @@ -36,8 +36,8 @@ public class CategoryBrandServiceImpl extends ServiceImpl getCategoryBrandListByBrandId(String brandId) { - return this.list(new LambdaQueryWrapper().eq(CategoryBrand::getBrandId, brandId)); + public List getCategoryBrandListByBrandId(List brandId) { + return this.list(new LambdaQueryWrapper().in(CategoryBrand::getBrandId, brandId)); } @Override diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java index af7d8365..08cb2a2d 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java @@ -119,6 +119,13 @@ public class GoodsServiceImpl extends ServiceImpl implements @Autowired private Cache cache; + @Override + public List getByBrandIds(List brandIds) { + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper (); + lambdaQueryWrapper.in(Goods::getBrandId,brandIds); + return list(lambdaQueryWrapper); + } + @Override public void underStoreGoods(String storeId) { //获取商品ID列表