|
@@ -42,15 +42,17 @@ public class CesGoodStockRecordsServiceImpl extends ServiceImpl<CesGoodStockReco
|
|
|
|
|
|
|
|
@Transactional(rollbackOn = Exception.class)
|
|
@Transactional(rollbackOn = Exception.class)
|
|
|
public Result create(CesGoodStockRecordsDto dto){
|
|
public Result create(CesGoodStockRecordsDto dto){
|
|
|
- //查询商品是否存在
|
|
|
|
|
- CesGoods goods = goodsService.fetchByIdAndHotelId(dto.getGoodId(),dto.getHotelId());
|
|
|
|
|
- if(ObjectUtils.isEmpty(goods)) return Result.error("商品数据未找到!");
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 这里需要锁资源 分布式事务锁 采用redis 实现 考虑到数据操作的原子性 (设置redis 锁一定要同步设置过期时间 3 秒 )
|
|
|
|
|
|
|
+ * 这里需要锁资源 分布式事务锁 采用redis 实现 考虑到数据操作的原子性 (设置redis 锁 统一要同步设置过期时间 60 秒 )
|
|
|
*/
|
|
*/
|
|
|
String key = String.format("stock::record::create:%s:%s", dto.getGoodId(),dto.getHotelId());
|
|
String key = String.format("stock::record::create:%s:%s", dto.getGoodId(),dto.getHotelId());
|
|
|
Lock lock = redisLockRegistry.obtain(key); //获取锁资源
|
|
Lock lock = redisLockRegistry.obtain(key); //获取锁资源
|
|
|
|
|
+
|
|
|
|
|
+ //查询商品是否存在
|
|
|
|
|
+ CesGoods goods = goodsService.fetchByIdAndHotelId(dto.getGoodId(),dto.getHotelId());
|
|
|
|
|
+ if(ObjectUtils.isEmpty(goods)) return Result.error("商品数据未找到!");
|
|
|
|
|
+
|
|
|
logger.info("lock begin");
|
|
logger.info("lock begin");
|
|
|
logger.info("创建 redis 资源锁 key:{}", key);
|
|
logger.info("创建 redis 资源锁 key:{}", key);
|
|
|
try{
|
|
try{
|
|
@@ -72,7 +74,8 @@ public class CesGoodStockRecordsServiceImpl extends ServiceImpl<CesGoodStockReco
|
|
|
goods.setInventory(count - num);
|
|
goods.setInventory(count - num);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
- //先保存 记录数据 再去扣除库存 加锁
|
|
|
|
|
|
|
+
|
|
|
|
|
+ //先保存 记录数据 再去扣除库存
|
|
|
CesGoodStockRecords records = new CesGoodStockRecords();
|
|
CesGoodStockRecords records = new CesGoodStockRecords();
|
|
|
BeanUtil.copyProperties(dto,records);
|
|
BeanUtil.copyProperties(dto,records);
|
|
|
records.setCreateAt(LocalDateTime.now());
|
|
records.setCreateAt(LocalDateTime.now());
|
|
@@ -84,7 +87,7 @@ public class CesGoodStockRecordsServiceImpl extends ServiceImpl<CesGoodStockReco
|
|
|
if(num > 0){ //必须是大于0 再去修改
|
|
if(num > 0){ //必须是大于0 再去修改
|
|
|
goods.setUpdateAt(LocalDateTime.now());
|
|
goods.setUpdateAt(LocalDateTime.now());
|
|
|
boolean flag = goodsService.updateById(goods);
|
|
boolean flag = goodsService.updateById(goods);
|
|
|
- if(!flag) throw new JeecgBootException("修改商品失败!");
|
|
|
|
|
|
|
+ if(!flag) throw new JeecgBootException("修改商品失败!");
|
|
|
}
|
|
}
|
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
|
return Result.error(e.getMessage());
|
|
return Result.error(e.getMessage());
|
|
@@ -97,4 +100,7 @@ public class CesGoodStockRecordsServiceImpl extends ServiceImpl<CesGoodStockReco
|
|
|
return Result.ok("创建成功!");
|
|
return Result.ok("创建成功!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|