超市库存商品,口味重构

This commit is contained in:
zjf committed 2025-08-19 14:37:27 +08:00
1 parent 27f9b73cd5
commit e39ccff7e2
3 files changed
+151 -41

No files matched your search

@@ -39,7 +39,7 @@ public class SmErpTasteStockEntity extends Model<SmErpTasteStockEntity> {
* 口味名称
*/
@Schema(description="口味名称")
private String taste;
private String tasteName;
/**
* 该口味的库存数量
@@ -103,6 +103,11 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
private final SmErpCommonService smErpCommonService;
private final SmErpTasteStockMapper smErpTasteStockMapper;
private final SmErpTasteStockService smErpTasteStockService;
@Autowired
private Sequence sequence;
@@ -399,9 +404,15 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
}
//还要对当前包装单位的库存进行扣减,因为有可能这个包装单位有很多口味,每一种口味也对应着自己的库存
//先查询当前包装单位是否设置了库存,因为存在没有设置的情况,则说明不包含同种规格口味不一样的情况
if (smErpUnitEntity.getStock() != null){
smErpUnitEntity.setStock(smErpUnitEntity.getStock() -incrCount1 );
smErpUnitMapper.updateById(smErpUnitEntity);
Long tasteId = smErpUnitEntity.getTasteId();
if (tasteId != null){
SmErpTasteStockEntity smErpTasteStockEntity = smErpTasteStockMapper.selectById(tasteId);
if (smErpTasteStockEntity != null) {
smErpTasteStockEntity.setStock(smErpTasteStockEntity.getStock() - incrCount);
smErpTasteStockMapper.updateById(smErpTasteStockEntity);
}else {
return R.failed("该商品没有设置口味库存");
}
}
//todo 要求先允许超卖,过后在入库回来
// 检查库存是否足够
@@ -1045,15 +1056,37 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
this.save(spu);
}
// 5. 处理包装单位
// 5. 处理口味(如果有口味信息)
Long tasteId = null;
if (StringUtils.isNotBlank(dto.getTaste())) {
// 检查口味是否已存在
LambdaQueryWrapper<SmErpTasteStockEntity> 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<SmErpSpuMapper, SmErpSpuEnt
BigDecimal.valueOf(dto.getPromotionPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0);
unit.setCostPrice(dto.getCostPrice() != null ?
BigDecimal.valueOf(dto.getCostPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0);
// 设置包装单位自己的库存
unit.setStock(dto.getStock() != null ? dto.getStock() : 0.0);
// 检查单位是否已存在:条码优先,其次使用 单位+口味+规格 的组合
LambdaQueryWrapper<SmErpUnitEntity> existingUnitWrapper = new LambdaQueryWrapper<>();
@@ -1097,7 +1128,7 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
smErpUnitService.save(unit);
}
// 6. 处理辅助条码(多个辅助条码)
// 7. 处理辅助条码(多个辅助条码)
List<String> 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<SmErpSpuMapper, SmErpSpuEnt
}
}
// 7. 处理库存设置 - 总库存应该是所有包装单位库存的总和
// 先查询该SPU下所有包装单位的库存总和
Double totalUnitStock = dto.getStock() != null ? dto.getStock() : 0.0;
// 8. 处理口味库存(如果有口味信息)
if (tasteId != null && dto.getStock() != null && dto.getStock() > 0) {
// 更新口味库存记录
SmErpTasteStockEntity tasteStock = smErpTasteStockMapper.selectById(tasteId);
if (tasteStock != null) {
tasteStock.setStock(dto.getStock());
tasteStock.setId(tasteId); // 设置包装单位ID
smErpTasteStockMapper.updateById(tasteStock);
}
}
// 9. 处理基础库存设置
// 检查库存记录是否已存在
LambdaQueryWrapper<SmErpStockEntity> 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<SmErpTasteStockEntity> allTasteStocks = smErpTasteStockMapper.selectList(
new LambdaQueryWrapper<SmErpTasteStockEntity>()
.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<SmErpSpuMapper, SmErpSpuEnt
this.save(spu);
}
// 5. 处理包装单位
// 5. 处理口味(如果有口味信息)
Long tasteId = null;
if (StringUtils.isNotBlank(dto.getTaste())) {
// 检查口味是否已存在
LambdaQueryWrapper<SmErpTasteStockEntity> 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<SmErpSpuMapper, SmErpSpuEnt
unit.setBarCode(dto.getBarCode()); // 包装单位的条码来自主条码
unit.setTaste(dto.getTaste()); // 设置口味
unit.setSpec(dto.getSpec()); // 设置规格字段
unit.setTasteId(tasteId); // 设置口味ID
unit.setPurchasePrice(dto.getPurchasePrice() != null ?
BigDecimal.valueOf(dto.getPurchasePrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0);
unit.setRetailPrice(dto.getRetailPrice() != null ?
@@ -1296,8 +1381,6 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
BigDecimal.valueOf(dto.getPromotionPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0);
unit.setCostPrice(dto.getCostPrice() != null ?
BigDecimal.valueOf(dto.getCostPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0);
// 设置包装单位自己的库存
unit.setStock(dto.getStock() != null ? dto.getStock() : 0.0);
// 检查单位是否已存在(根据单位名称、口味和规格)
LambdaQueryWrapper<SmErpUnitEntity> existingUnitWrapper = new LambdaQueryWrapper<>();
@@ -1313,18 +1396,18 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
existingUnit.setBarCode(dto.getBarCode()); // 更新条码
existingUnit.setTaste(dto.getTaste()); // 更新口味
existingUnit.setSpec(dto.getSpec()); // 更新规格
existingUnit.setTasteId(tasteId); // 更新口味ID
existingUnit.setPurchasePrice(unit.getPurchasePrice());
existingUnit.setRetailPrice(unit.getRetailPrice());
existingUnit.setPromotionPrice(unit.getPromotionPrice());
existingUnit.setCostPrice(unit.getCostPrice());
// 更新包装单位库存
existingUnit.setStock(unit.getStock());
smErpUnitService.updateById(existingUnit);
unit = existingUnit; // 使用更新后的单位
} else {
smErpUnitService.save(unit);
}
// 6. 处理辅助条码(多个辅助条码)
// 7. 处理辅助条码(多个辅助条码)
List<String> 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<SmErpSpuMapper, SmErpSpuEnt
if (existingBarcode != null) {
// 更新现有辅助条码
existingBarcode.setUnitId(existingUnit != null ? existingUnit.getId() : unit.getId());
existingBarcode.setUnitId(unit.getId());
smErpAuxiliaryCodeService.updateById(existingBarcode);
} else {
SmErpAuxiliaryCodeEntity auxiliaryCode = new SmErpAuxiliaryCodeEntity();
auxiliaryCode.setSpuId(spu.getId());
auxiliaryCode.setBarCode(auxiliaryBarcode);
auxiliaryCode.setUnitId(existingUnit != null ? existingUnit.getId() : unit.getId());
auxiliaryCode.setUnitId(unit.getId());
smErpAuxiliaryCodeService.save(auxiliaryCode);
}
}
// 7. 处理库存设置 - 总库存应该是所有包装单位库存的总和
// 8. 处理口味库存(如果有口味信息)
if (tasteId != null && dto.getStock() != null && dto.getStock() > 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<SmErpSpuMapper, SmErpSpuEnt
if (existingStock != null) {
// 更新现有库存记录 - 重新计算总库存
// 查询该SPU下所有包装单位的库存总和
List<SmErpUnitEntity> allUnits = smErpUnitService.list(
new LambdaQueryWrapper<SmErpUnitEntity>()
.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<SmErpTasteStockEntity> allTasteStocks = smErpTasteStockMapper.selectList(
new LambdaQueryWrapper<SmErpTasteStockEntity>()
.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<SmErpSpuMapper, SmErpSpuEnt
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);
}
@@ -6,7 +6,7 @@
<resultMap id="smErpTasteStockMap" type="com.bsd.pjcloud.supermarket.entity.SmErpTasteStockEntity">
<id property="id" column="id"/>
<result property="spuId" column="spu_id"/>
<result property="taste" column="taste"/>
<result property="tasteName" column="taste_name"/>
<result property="stock" column="stock"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>