Commit fbee21de5f14190d05e7ed4f4ecef022782b905b
1 parent
c2579104
mall: 商品的代码 review
Showing
1 changed file
with
21 additions
and
2 deletions
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java
| 1 | package cn.iocoder.yudao.module.product.service.sku; | 1 | package cn.iocoder.yudao.module.product.service.sku; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 3 | import cn.hutool.core.collection.CollectionUtil; | 4 | import cn.hutool.core.collection.CollectionUtil; |
| 4 | import cn.hutool.core.util.ObjectUtil; | 5 | import cn.hutool.core.util.ObjectUtil; |
| 6 | +import cn.hutool.core.util.StrUtil; | ||
| 5 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 7 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 6 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | 8 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 7 | import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyRespVO; | 9 | import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyRespVO; |
| @@ -22,9 +24,11 @@ import org.springframework.validation.annotation.Validated; | @@ -22,9 +24,11 @@ import org.springframework.validation.annotation.Validated; | ||
| 22 | 24 | ||
| 23 | import javax.annotation.Resource; | 25 | import javax.annotation.Resource; |
| 24 | import java.util.*; | 26 | import java.util.*; |
| 27 | +import java.util.function.Function; | ||
| 25 | import java.util.stream.Collectors; | 28 | import java.util.stream.Collectors; |
| 26 | 29 | ||
| 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 30 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 31 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | ||
| 28 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*; | 32 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*; |
| 29 | 33 | ||
| 30 | /** | 34 | /** |
| @@ -145,7 +149,8 @@ public class ProductSkuServiceImpl implements ProductSkuService { | @@ -145,7 +149,8 @@ public class ProductSkuServiceImpl implements ProductSkuService { | ||
| 145 | public void updateProductSkus(Long spuId, List<ProductSkuCreateOrUpdateReqVO> skus) { | 149 | public void updateProductSkus(Long spuId, List<ProductSkuCreateOrUpdateReqVO> skus) { |
| 146 | // 查询 SPU 下已经存在的 SKU 的集合 | 150 | // 查询 SPU 下已经存在的 SKU 的集合 |
| 147 | List<ProductSkuDO> existsSkus = productSkuMapper.selectListBySpuId(spuId); | 151 | List<ProductSkuDO> existsSkus = productSkuMapper.selectListBySpuId(spuId); |
| 148 | - | 152 | + // 构建规格与 SKU 的映射关系; |
| 153 | + // TODO @luowenfeng: 可以下 existsSkuMap2; 会简洁一点; 另外, 可以考虑抽一个小方法, 用于 Properties 生成一个串; 这样 177 也可以复用了 | ||
| 149 | Map<String, Long> existsSkuMap = existsSkus.stream() | 154 | Map<String, Long> existsSkuMap = existsSkus.stream() |
| 150 | .map(v -> { | 155 | .map(v -> { |
| 151 | String collect = v.getProperties() == null? "null": v.getProperties() | 156 | String collect = v.getProperties() == null? "null": v.getProperties() |
| @@ -155,7 +160,12 @@ public class ProductSkuServiceImpl implements ProductSkuService { | @@ -155,7 +160,12 @@ public class ProductSkuServiceImpl implements ProductSkuService { | ||
| 155 | return String.join("-", collect, String.valueOf(v.getId())); | 160 | return String.join("-", collect, String.valueOf(v.getId())); |
| 156 | }) | 161 | }) |
| 157 | .collect(Collectors.toMap(v -> v.split("-")[0], v -> Long.valueOf(v.split("-")[1]))); | 162 | .collect(Collectors.toMap(v -> v.split("-")[0], v -> Long.valueOf(v.split("-")[1]))); |
| 158 | - | 163 | +// Map<String, Long> existsSkuMap2 = CollectionUtils.convertMap(existsSkus, productSkuDO -> { |
| 164 | +// if (CollUtil.isEmpty(productSkuDO.getProperties())) { | ||
| 165 | +// return ""; | ||
| 166 | +// } | ||
| 167 | +// return StrUtil.join("-", convertList(productSkuDO.getProperties(), ProductSkuDO.Property::getValueId)); | ||
| 168 | +// }, ProductSkuDO::getId); | ||
| 159 | 169 | ||
| 160 | // 拆分三个集合,新插入的、需要更新的、需要删除的 | 170 | // 拆分三个集合,新插入的、需要更新的、需要删除的 |
| 161 | List<ProductSkuDO> insertSkus = new ArrayList<>(); | 171 | List<ProductSkuDO> insertSkus = new ArrayList<>(); |
| @@ -192,5 +202,14 @@ public class ProductSkuServiceImpl implements ProductSkuService { | @@ -192,5 +202,14 @@ public class ProductSkuServiceImpl implements ProductSkuService { | ||
| 192 | } | 202 | } |
| 193 | } | 203 | } |
| 194 | 204 | ||
| 205 | + public static void main(String[] args) { | ||
| 206 | + List<Integer> ids = new ArrayList<>(); | ||
| 207 | + ids.add(1); | ||
| 208 | + | ||
| 209 | + List<Integer> ids2 = new ArrayList<>(); | ||
| 210 | + ids2.add(2); | ||
| 211 | + | ||
| 212 | + System.out.println(ids.equals(ids2)); | ||
| 213 | + } | ||
| 195 | 214 | ||
| 196 | } | 215 | } |