商品更新问题修复
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
|
@Data
|
||||||
public class DeviceProductSkuSaveReqVO {
|
public class DeviceProductSkuSaveReqVO {
|
||||||
|
|
||||||
|
@Schema(description = "SKU 编号(编辑时传;新增可不传)", example = "261")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
@Schema(description = "商品 SKU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "测试商品")
|
@Schema(description = "商品 SKU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "测试商品")
|
||||||
@NotEmpty(message = "商品 SKU 名字不能为空")
|
@NotEmpty(message = "商品 SKU 名字不能为空")
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
+3
@@ -13,6 +13,9 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
public class ProductSkuSaveReqVO {
|
public class ProductSkuSaveReqVO {
|
||||||
|
|
||||||
|
@Schema(description = "商品 SKU 编号(编辑时传;新增可不传)", example = "261")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
@Schema(description = "商品 SKU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "清凉小短袖")
|
@Schema(description = "商品 SKU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "清凉小短袖")
|
||||||
@NotEmpty(message = "商品 SKU 名字不能为空")
|
@NotEmpty(message = "商品 SKU 名字不能为空")
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
+19
-13
@@ -242,24 +242,27 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateSkuList(Long spuId, List<ProductSkuSaveReqVO> skus) {
|
public void updateSkuList(Long spuId, List<ProductSkuSaveReqVO> skus) {
|
||||||
// 构建属性与 SKU 的映射关系;
|
// 规则(按 id 语义):
|
||||||
Map<String, Long> existsSkuMap = convertMap(productSkuMapper.selectListBySpuId(spuId),
|
// 1) id != null:更新
|
||||||
ProductSkuConvert.INSTANCE::buildPropertyKey, ProductSkuDO::getId);
|
// 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> insertSkus = new ArrayList<>();
|
||||||
List<ProductSkuDO> updateSkus = new ArrayList<>();
|
List<ProductSkuDO> updateSkus = new ArrayList<>();
|
||||||
|
Set<Long> requestIds = new HashSet<>();
|
||||||
|
|
||||||
List<ProductSkuDO> allUpdateSkus = BeanUtils.toBean(skus, ProductSkuDO.class, sku -> sku.setSpuId(spuId));
|
List<ProductSkuDO> allUpdateSkus = BeanUtils.toBean(skus, ProductSkuDO.class, sku -> sku.setSpuId(spuId));
|
||||||
allUpdateSkus.forEach(sku -> {
|
allUpdateSkus.forEach(sku -> {
|
||||||
String propertiesKey = ProductSkuConvert.INSTANCE.buildPropertyKey(sku);
|
Long id = sku.getId();
|
||||||
// 1、找得到的,进行更新
|
if (id != null && existsIds.contains(id)) {
|
||||||
Long existsSkuId = existsSkuMap.remove(propertiesKey);
|
// 更新已有
|
||||||
if (existsSkuId != null) {
|
|
||||||
sku.setId(existsSkuId);
|
|
||||||
updateSkus.add(sku);
|
updateSkus.add(sku);
|
||||||
|
requestIds.add(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 2、找不到,进行插入
|
// 新增(id 为空或不存在于当前 spu)
|
||||||
|
sku.setId(null);
|
||||||
sku.setSpuId(spuId);
|
sku.setSpuId(spuId);
|
||||||
insertSkus.add(sku);
|
insertSkus.add(sku);
|
||||||
});
|
});
|
||||||
@@ -271,8 +274,11 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
|||||||
if (CollUtil.isNotEmpty(updateSkus)) {
|
if (CollUtil.isNotEmpty(updateSkus)) {
|
||||||
updateSkus.forEach(sku -> productSkuMapper.updateById(sku));
|
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<>();
|
List<ProductSkuSaveReqVO> skus = new ArrayList<>();
|
||||||
for (DeviceProductSkuSaveReqVO deviceSku : deviceSkus) {
|
for (DeviceProductSkuSaveReqVO deviceSku : deviceSkus) {
|
||||||
ProductSkuSaveReqVO sku = new ProductSkuSaveReqVO();
|
ProductSkuSaveReqVO sku = new ProductSkuSaveReqVO();
|
||||||
|
sku.setId(deviceSku.getId());
|
||||||
sku.setName(deviceSku.getName());
|
sku.setName(deviceSku.getName());
|
||||||
sku.setPrice(deviceSku.getPrice());
|
sku.setPrice(deviceSku.getPrice());
|
||||||
sku.setMarketPrice(deviceSku.getMarketPrice());
|
sku.setMarketPrice(deviceSku.getMarketPrice());
|
||||||
|
|||||||
Reference in new issue
Block a user