商品更新问题修复
This commit is contained in:
1 parent
7175dc17be
commit
addad72e2e
4 files changed
+26
-13
No files matched your search
+3
@@ -15,6 +15,9 @@ import javax.validation.constraints.NotNull;
|
||||
@Data
|
||||
public class DeviceProductSkuSaveReqVO {
|
||||
|
||||
@Schema(description = "SKU 编号(编辑时传;新增可不传)", example = "261")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "商品 SKU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "测试商品")
|
||||
@NotEmpty(message = "商品 SKU 名字不能为空")
|
||||
private String name;
|
||||
|
||||
+3
@@ -13,6 +13,9 @@ import java.util.List;
|
||||
@Data
|
||||
public class ProductSkuSaveReqVO {
|
||||
|
||||
@Schema(description = "商品 SKU 编号(编辑时传;新增可不传)", example = "261")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "商品 SKU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "清凉小短袖")
|
||||
@NotEmpty(message = "商品 SKU 名字不能为空")
|
||||
private String name;
|
||||
|
||||
+19
-13
@@ -242,24 +242,27 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateSkuList(Long spuId, List<ProductSkuSaveReqVO> skus) {
|
||||
// 构建属性与 SKU 的映射关系;
|
||||
Map<String, Long> existsSkuMap = convertMap(productSkuMapper.selectListBySpuId(spuId),
|
||||
ProductSkuConvert.INSTANCE::buildPropertyKey, ProductSkuDO::getId);
|
||||
|
||||
// 拆分三个集合,新插入的、需要更新的、需要删除的
|
||||
// 规则(按 id 语义):
|
||||
// 1) id != null:更新
|
||||
// 2) id == null:新增
|
||||
// 3) 库里存在但本次请求没带到的 id:删除
|
||||
List<ProductSkuDO> existsSkus = productSkuMapper.selectListBySpuId(spuId);
|
||||
Set<Long> existsIds = convertSet(existsSkus, ProductSkuDO::getId);
|
||||
List<ProductSkuDO> insertSkus = new ArrayList<>();
|
||||
List<ProductSkuDO> updateSkus = new ArrayList<>();
|
||||
Set<Long> requestIds = new HashSet<>();
|
||||
|
||||
List<ProductSkuDO> allUpdateSkus = BeanUtils.toBean(skus, ProductSkuDO.class, sku -> sku.setSpuId(spuId));
|
||||
allUpdateSkus.forEach(sku -> {
|
||||
String propertiesKey = ProductSkuConvert.INSTANCE.buildPropertyKey(sku);
|
||||
// 1、找得到的,进行更新
|
||||
Long existsSkuId = existsSkuMap.remove(propertiesKey);
|
||||
if (existsSkuId != null) {
|
||||
sku.setId(existsSkuId);
|
||||
Long id = sku.getId();
|
||||
if (id != null && existsIds.contains(id)) {
|
||||
// 更新已有
|
||||
updateSkus.add(sku);
|
||||
requestIds.add(id);
|
||||
return;
|
||||
}
|
||||
// 2、找不到,进行插入
|
||||
// 新增(id 为空或不存在于当前 spu)
|
||||
sku.setId(null);
|
||||
sku.setSpuId(spuId);
|
||||
insertSkus.add(sku);
|
||||
});
|
||||
@@ -271,8 +274,11 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
||||
if (CollUtil.isNotEmpty(updateSkus)) {
|
||||
updateSkus.forEach(sku -> productSkuMapper.updateById(sku));
|
||||
}
|
||||
if (CollUtil.isNotEmpty(existsSkuMap)) {
|
||||
productSkuMapper.deleteBatchIds(existsSkuMap.values());
|
||||
// 删除:库里有、请求没带的
|
||||
Set<Long> deleteIds = new HashSet<>(existsIds);
|
||||
deleteIds.removeAll(requestIds);
|
||||
if (CollUtil.isNotEmpty(deleteIds)) {
|
||||
productSkuMapper.deleteBatchIds(deleteIds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -276,6 +276,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
List<ProductSkuSaveReqVO> skus = new ArrayList<>();
|
||||
for (DeviceProductSkuSaveReqVO deviceSku : deviceSkus) {
|
||||
ProductSkuSaveReqVO sku = new ProductSkuSaveReqVO();
|
||||
sku.setId(deviceSku.getId());
|
||||
sku.setName(deviceSku.getName());
|
||||
sku.setPrice(deviceSku.getPrice());
|
||||
sku.setMarketPrice(deviceSku.getMarketPrice());
|
||||
|
||||
Reference in new issue
Block a user