From 77a676ee07c181027eab6ddb59dbecad949069a8 Mon Sep 17 00:00:00 2001 From: luowenfeng <1092164058@qq.com> Date: Wed, 21 Sep 2022 19:40:07 +0800 Subject: [PATCH] =?UTF-8?q?feature(=E7=AE=A1=E7=90=86=E5=90=8E=E5=8F=B0):?= =?UTF-8?q?=20=E5=95=86=E5=93=81=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../category/ProductCategoryServiceImpl.java | 38 +++++----------- .../service/sku/ProductSkuServiceImpl.java | 44 +++++++++++++------ .../src/views/mall/product/spu/save.vue | 2 +- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java index ebfe599d6..ac84f755a 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.module.product.service.category; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO; import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO; @@ -93,35 +92,22 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { @Override public void validateCategoryLevel(Long id) { - Integer level = getProductCategoryLevel(id, 1); - if (level < 3){ + Long parentId = id; + for (int i = 0; i < 3; i++) { + if (Objects.equals(parentId, ProductCategoryDO.PARENT_ID_NULL)) { + throw exception(CATEGORY_LEVEL_ERROR); + } + ProductCategoryDO category = productCategoryMapper.selectById(parentId); + if (category == null) { + throw exception(CATEGORY_NOT_EXISTS); + } + parentId = category.getParentId(); + } + if (!Objects.equals(parentId, ProductCategoryDO.PARENT_ID_NULL)) { throw exception(CATEGORY_LEVEL_ERROR); } } - // TODO @Luowenfeng:建议使用 for 循环,避免递归 - /** - * 获得商品分类的级别 - * - * @param id 商品分类的编号 - * @return 级别 - */ - private Integer getProductCategoryLevel(Long id, int level){ - ProductCategoryDO category = productCategoryMapper.selectById(id); - if (category == null) { - throw exception(CATEGORY_NOT_EXISTS); - } - // TODO Luowenfeng:去掉是否开启,它不影响级别哈 - if (ObjectUtil.notEqual(category.getStatus(), CommonStatusEnum.ENABLE.getStatus())) { - throw exception(CATEGORY_DISABLED); - } - // TODO Luowenfeng:不使用 0 直接比较哈,使用枚举 - if (category.getParentId() == 0) { - return level; - } - return getProductCategoryLevel(category.getParentId(), ++level); - } - @Override public ProductCategoryDO getCategory(Long id) { return productCategoryMapper.selectById(id); diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java index be205178d..373d5884d 100755 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java @@ -144,36 +144,52 @@ public class ProductSkuServiceImpl implements ProductSkuService { public void updateProductSkus(Long spuId, List skus) { // 查询 SPU 下已经存在的 SKU 的集合 List existsSkus = productSkuMapper.selectListBySpuId(spuId); - Map existsSkuMap = CollectionUtils.convertMap(existsSkus, ProductSkuDO::getId); + + Map existsSkuMap = existsSkus.stream() + .map(v -> { + String collect = v.getProperties() == null? "null": v.getProperties() + .stream() + .map(m -> String.valueOf(m.getValueId())) + .collect(Collectors.joining()); + return String.join("-", collect, String.valueOf(v.getId())); + }) + .collect(Collectors.toMap(v -> v.split("-")[0], v -> Long.valueOf(v.split("-")[1]))); + // 拆分三个集合,新插入的、需要更新的、需要删除的 List insertSkus = new ArrayList<>(); - List updateSkus = new ArrayList<>(); // TODO Luowenfeng:使用 Long 即可 - List deleteSkus = new ArrayList<>(); + List updateSkus = new ArrayList<>(); + List deleteSkus = new ArrayList<>(); - // TODO @Luowenfeng:是不是基于规格匹配会比较好。可以参考下 onemall 的 ProductSpuServiceImpl 的 updateProductSpu 逻辑 List allUpdateSkus = ProductSkuConvert.INSTANCE.convertSkuDOList(skus); allUpdateSkus.forEach(p -> { - if (p.getId() != null) { - if (existsSkuMap.containsKey(p.getId())) { - updateSkus.add(p); - return; - } - deleteSkus.add(p); + String propertiesKey = p.getProperties() == null? "null": p.getProperties().stream().map(m -> String.valueOf(m.getValueId())).collect(Collectors.joining()); + // 1、找得到的,进行更新 + if (existsSkuMap.containsKey(propertiesKey)) { + updateSkus.add(p); + existsSkuMap.remove(propertiesKey); return; } + // 2、找不到,进行插入 p.setSpuId(spuId); insertSkus.add(p); }); + // 3、多余的,删除 + if(!existsSkuMap.isEmpty()){ + deleteSkus = new ArrayList<>(existsSkuMap.values()); + } - if (CollectionUtil.isNotEmpty(insertSkus)) { + // 4、执行修改 Sku + if (!insertSkus.isEmpty()) { productSkuMapper.insertBatch(insertSkus); } - if (updateSkus.size() > 0) { + if (!updateSkus.isEmpty()) { updateSkus.forEach(p -> productSkuMapper.updateById(p)); } - if (deleteSkus.size() > 0) { - productSkuMapper.deleteBatchIds(deleteSkus.stream().map(ProductSkuDO::getId).collect(Collectors.toList())); + if (!deleteSkus.isEmpty()) { + productSkuMapper.deleteBatchIds(deleteSkus); } } + + } diff --git a/yudao-ui-admin/src/views/mall/product/spu/save.vue b/yudao-ui-admin/src/views/mall/product/spu/save.vue index 0940b7c52..522a6947d 100644 --- a/yudao-ui-admin/src/views/mall/product/spu/save.vue +++ b/yudao-ui-admin/src/views/mall/product/spu/save.vue @@ -361,7 +361,7 @@ export default { if (!valid) { return; } - let rates = this.ratesForm.rates; + let rates = JSON.parse(JSON.stringify(this.ratesForm.rates)); // 价格元转分 rates.forEach(r => {