超市库存商品,口味重构

This commit is contained in:
zjf committed 2025-08-19 16:42:01 +08:00
1 parent e39ccff7e2
commit b19ceb5f3c
2 files changed
+73 -1

No files matched your search

@@ -321,6 +321,11 @@ public class SmErpSpuEntity extends Model<SmErpSpuEntity> {
@TableField(exist = false)
private List<SmErpUnitEntity> smErpUnitEntityList;
@Schema(description="口味集合")
@TableField(exist = false)
private List<SmErpTasteStockEntity> smErpTasteStockEntityList;
/**
* 库存上限
*/
@@ -129,7 +129,6 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
baseMapper.insert(supermarketSpu);
//库存表
SmErpStockEntity smErpStockEntity = new SmErpStockEntity();
smErpStockEntity.setErpSpuId(supermarketSpu.getId());
@@ -139,6 +138,19 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
smErpStockEntity.setInventoryUpperLimit(supermarketSpu.getInventoryUpperLimit());
smErpStockMapper.insert(smErpStockEntity);
// 处理口味集合
List<SmErpTasteStockEntity> tasteList = supermarketSpu.getSmErpTasteStockEntityList();
if (CollectionUtils.isNotEmpty(tasteList)) {
tasteList.forEach(taste -> {
taste.setSpuId(supermarketSpu.getId());
// 设置默认库存为0
if (taste.getStock() == null) {
taste.setStock(0.0);
}
});
smErpTasteStockService.saveBatch(tasteList);
}
//新增包装类型
List<SmErpUnitEntity> smErpUnitEntityList = supermarketSpu.getSmErpUnitEntityList();
List<SmErpUnitEntity> isBaseNum = smErpUnitEntityList.stream().filter(smErpUnitEntity -> smErpUnitEntity.getIsBase() == true).collect(Collectors.toList());
@@ -349,6 +361,61 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
}
}
// 处理口味集合
List<SmErpTasteStockEntity> tasteList = supermarketSpu.getSmErpTasteStockEntityList();
if (CollectionUtils.isNotEmpty(tasteList)) {
// 分离新增和更新的口味
List<SmErpTasteStockEntity> insertTastes = tasteList.stream()
.filter(taste -> taste.getId() == null)
.collect(Collectors.toList());
List<SmErpTasteStockEntity> updateTastes = tasteList.stream()
.filter(taste -> taste.getId() != null)
.collect(Collectors.toList());
// 处理新增口味
if (CollectionUtils.isNotEmpty(insertTastes)) {
insertTastes.forEach(taste -> {
taste.setSpuId(supermarketSpu.getId());
// 设置默认库存为0
if (taste.getStock() == null) {
taste.setStock(0.0);
}
});
smErpTasteStockService.saveBatch(insertTastes);
}
// 处理更新口味
if (CollectionUtils.isNotEmpty(updateTastes)) {
updateTastes.forEach(taste -> {
taste.setSpuId(supermarketSpu.getId());
});
smErpTasteStockService.updateBatchById(updateTastes);
}
// 获取现有所有口味ID
List<Long> existingTasteIds = smErpTasteStockService.list(
new LambdaQueryWrapper<SmErpTasteStockEntity>()
.eq(SmErpTasteStockEntity::getSpuId, supermarketSpu.getId())
).stream().map(SmErpTasteStockEntity::getId).collect(Collectors.toList());
// 收集本次提交的口味ID(排除新增的)
List<Long> submittedTasteIds = tasteList.stream()
.map(SmErpTasteStockEntity::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
// 计算需要删除的口味ID
List<Long> toDeleteTasteIds = existingTasteIds.stream()
.filter(id -> !submittedTasteIds.contains(id))
.collect(Collectors.toList());
// 批量删除失效口味
if (!toDeleteTasteIds.isEmpty()) {
smErpTasteStockService.removeByIds(toDeleteTasteIds);
}
}
return R.ok();
}