From e39ccff7e28db456095256649622b1f5a89db022 Mon Sep 17 00:00:00 2001 From: zjf <1799650030@qq.com> Date: Tue, 19 Aug 2025 14:37:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E5=B8=82=E5=BA=93=E5=AD=98=E5=95=86?= =?UTF-8?q?=E5=93=81=EF=BC=8C=E5=8F=A3=E5=91=B3=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/SmErpTasteStockEntity.java | 2 +- .../service/impl/SmErpSpuServiceImpl.java | 188 ++++++++++++++---- .../mapper/SmErpTasteStockMapper.xml | 2 +- 3 files changed, 151 insertions(+), 41 deletions(-) diff --git a/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/entity/SmErpTasteStockEntity.java b/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/entity/SmErpTasteStockEntity.java index acbd8bb7b..1eda0ad86 100644 --- a/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/entity/SmErpTasteStockEntity.java +++ b/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/entity/SmErpTasteStockEntity.java @@ -39,7 +39,7 @@ public class SmErpTasteStockEntity extends Model { * 口味名称 */ @Schema(description="口味名称") - private String taste; + private String tasteName; /** * 该口味的库存数量 diff --git a/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java b/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java index cb229824a..e72a3a528 100644 --- a/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java +++ b/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java @@ -103,6 +103,11 @@ public class SmErpSpuServiceImpl extends ServiceImpl tasteWrapper = new LambdaQueryWrapper<>(); + tasteWrapper.eq(SmErpTasteStockEntity::getSpuId, spu.getId()); + tasteWrapper.eq(SmErpTasteStockEntity::getTasteName, dto.getTaste()); + SmErpTasteStockEntity existingTaste = smErpTasteStockMapper.selectOne(tasteWrapper); + + if (existingTaste != null) { + tasteId = existingTaste.getId(); + } else { + // 创建新口味 + SmErpTasteStockEntity newTaste = new SmErpTasteStockEntity(); + newTaste.setSpuId(spu.getId()); + newTaste.setTasteName(dto.getTaste()); + smErpTasteStockMapper.insert(newTaste); + tasteId = newTaste.getId(); + } + } + + // 6. 处理包装单位 SmErpUnitEntity unit = new SmErpUnitEntity(); unit.setSpuId(spu.getId()); unit.setUnit(dto.getUnit()); unit.setPackageNum(dto.getPackageNum()); unit.setIsBase(true); // 默认为基础单位 - unit.setBarCode(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getBarCode()) ? dto.getBarCode() : null); // 条码 - unit.setTaste(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getTaste()) ? dto.getTaste() : null); // 口味 - unit.setSpec(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getSpec()) ? dto.getSpec() : null); // 规格 + unit.setBarCode(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getBarCode()) ? dto.getBarCode() : null); + unit.setTaste(dto.getTaste()); // 保持兼容性 + unit.setSpec(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getSpec()) ? dto.getSpec() : null); + unit.setTasteId(tasteId); // 设置口味ID unit.setPurchasePrice(dto.getPurchasePrice() != null ? BigDecimal.valueOf(dto.getPurchasePrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); unit.setRetailPrice(dto.getRetailPrice() != null ? @@ -1062,8 +1095,6 @@ public class SmErpSpuServiceImpl extends ServiceImpl existingUnitWrapper = new LambdaQueryWrapper<>(); @@ -1097,7 +1128,7 @@ public class SmErpSpuServiceImpl extends ServiceImpl auxiliaryBarcodes = new ArrayList<>(); if (org.apache.commons.lang3.StringUtils.isNotBlank(dto.getAuxiliaryBarcode1())) { auxiliaryBarcodes.add(dto.getAuxiliaryBarcode1()); @@ -1136,30 +1167,62 @@ public class SmErpSpuServiceImpl extends ServiceImpl 0) { + // 更新口味库存记录 + SmErpTasteStockEntity tasteStock = smErpTasteStockMapper.selectById(tasteId); + if (tasteStock != null) { + tasteStock.setStock(dto.getStock()); + tasteStock.setId(tasteId); // 设置包装单位ID + smErpTasteStockMapper.updateById(tasteStock); + } + } + + // 9. 处理基础库存设置 // 检查库存记录是否已存在 LambdaQueryWrapper existingStockWrapper = new LambdaQueryWrapper<>(); existingStockWrapper.eq(SmErpStockEntity::getErpSpuId, spu.getId()); SmErpStockEntity existingStock = smErpStockService.getOne(existingStockWrapper); if (existingStock != null) { - // 库存记录已存在,累加包装单位库存到总库存 - Double currentTotalStock = existingStock.getCurrentStock() != null ? existingStock.getCurrentStock() : 0.0; - Double newTotalStock = currentTotalStock + totalUnitStock; + // 库存记录已存在,重新计算总库存 + Double newTotalStock; + + if (tasteId != null) { + // 有口味:统计所有口味库存的总和 + List allTasteStocks = smErpTasteStockMapper.selectList( + new LambdaQueryWrapper() + .eq(SmErpTasteStockEntity::getSpuId, spu.getId()) + ); + newTotalStock = allTasteStocks.stream() + .mapToDouble(ts -> ts.getStock() != null ? ts.getStock() : 0.0) + .sum(); + } else { + // 无口味:累加当前导入的库存 + Double currentTotalStock = existingStock.getCurrentStock() != null ? existingStock.getCurrentStock() : 0.0; + newTotalStock = currentTotalStock + (dto.getStock() != null ? dto.getStock() : 0.0); + } + existingStock.setCurrentStock(newTotalStock); existingStock.setBaseStock(newTotalStock); smErpStockService.updateById(existingStock); } else { - // 创建新的库存记录,总库存等于包装单位库存 + // 创建新的库存记录 SmErpStockEntity stock = new SmErpStockEntity(); stock.setErpSpuId(spu.getId()); stock.setInventoryUpperLimit(dto.getInventoryUpperLimit() != null ? dto.getInventoryUpperLimit() : 1000); stock.setInventoryLowerLimit(dto.getInventoryLowerLimit() != null ? dto.getInventoryLowerLimit() : 10); - stock.setBaseStock(totalUnitStock); - stock.setCurrentStock(totalUnitStock); + + if (tasteId != null) { + // 有口味:使用口味库存 + stock.setBaseStock(dto.getStock() != null ? dto.getStock() : 0.0); + stock.setCurrentStock(dto.getStock() != null ? dto.getStock() : 0.0); + } else { + // 无口味:直接使用导入的库存 + stock.setBaseStock(dto.getStock() != null ? dto.getStock() : 0.0); + stock.setCurrentStock(dto.getStock() != null ? dto.getStock() : 0.0); + } + smErpStockService.save(stock); } @@ -1279,7 +1342,28 @@ public class SmErpSpuServiceImpl extends ServiceImpl tasteWrapper = new LambdaQueryWrapper<>(); + tasteWrapper.eq(SmErpTasteStockEntity::getSpuId, spu.getId()); + tasteWrapper.eq(SmErpTasteStockEntity::getTasteName, dto.getTaste()); + SmErpTasteStockEntity existingTaste = smErpTasteStockMapper.selectOne(tasteWrapper); + + if (existingTaste != null) { + tasteId = existingTaste.getId(); + } else { + // 创建新口味 + SmErpTasteStockEntity newTaste = new SmErpTasteStockEntity(); + newTaste.setSpuId(spu.getId()); + newTaste.setTasteName(dto.getTaste()); + smErpTasteStockMapper.insert(newTaste); + tasteId = newTaste.getId(); + } + } + + // 6. 处理包装单位 SmErpUnitEntity unit = new SmErpUnitEntity(); unit.setSpuId(spu.getId()); unit.setUnit(dto.getUnit()); @@ -1288,6 +1372,7 @@ public class SmErpSpuServiceImpl extends ServiceImpl existingUnitWrapper = new LambdaQueryWrapper<>(); @@ -1313,18 +1396,18 @@ public class SmErpSpuServiceImpl extends ServiceImpl auxiliaryBarcodes = new ArrayList<>(); if (org.apache.commons.lang3.StringUtils.isNotBlank(dto.getAuxiliaryBarcode1())) { auxiliaryBarcodes.add(dto.getAuxiliaryBarcode1()); @@ -1352,18 +1435,29 @@ public class SmErpSpuServiceImpl extends ServiceImpl 0) { + // 更新口味库存记录 + SmErpTasteStockEntity tasteStock = smErpTasteStockMapper.selectById(tasteId); + if (tasteStock != null) { + tasteStock.setStock(dto.getStock()); + tasteStock.setId(tasteId); + smErpTasteStockMapper.updateById(tasteStock); + } + } + + // 9. 处理基础库存设置 - 总库存应该是所有包装单位口味的库存的总和 // 计算该SPU下所有包装单位的库存总和 Double totalUnitStock = dto.getStock() != null ? dto.getStock() : 0.0; @@ -1374,14 +1468,21 @@ public class SmErpSpuServiceImpl extends ServiceImpl allUnits = smErpUnitService.list( - new LambdaQueryWrapper() - .eq(SmErpUnitEntity::getSpuId, spu.getId()) - ); - Double totalStock = allUnits.stream() - .mapToDouble(u -> u.getStock() != null ? u.getStock() : 0.0) - .sum(); + Double totalStock; + + if (tasteId != null) { + // 有口味:统计所有口味库存的总和 + List allTasteStocks = smErpTasteStockMapper.selectList( + new LambdaQueryWrapper() + .eq(SmErpTasteStockEntity::getSpuId, spu.getId()) + ); + totalStock = allTasteStocks.stream() + .mapToDouble(ts -> ts.getStock() != null ? ts.getStock() : 0.0) + .sum(); + } else { + // 无口味:直接使用当前导入的库存数量 + totalStock = dto.getStock() != null ? dto.getStock() : 0.0; + } existingStock.setInventoryUpperLimit(dto.getInventoryUpperLimit() != null ? dto.getInventoryUpperLimit() : 1000); existingStock.setInventoryLowerLimit(dto.getInventoryLowerLimit() != null ? dto.getInventoryLowerLimit() : 10); @@ -1394,8 +1495,17 @@ public class SmErpSpuServiceImpl extends ServiceImpl - +