xiongzhu 4 years ago
parent
commit
bcf03cd521
1 changed files with 8 additions and 4 deletions
  1. 8 4
      src/main/java/com/izouma/nineth/service/CollectionService.java

+ 8 - 4
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -351,7 +351,8 @@ public class CollectionService {
 
     public Long increaseStock(Long id, int number) {
         if (redisTemplate.opsForValue().get("collectionStock::" + id) == null) {
-            redisTemplate.opsForValue().set("collectionStock::" + id, collectionRepo.getStock(id));
+            redisTemplate.opsForValue().set("collectionStock::" + id,
+                    Optional.ofNullable(collectionRepo.getStock(id)).orElse(0));
         }
         Long stock = redisTemplate.opsForValue().increment("collectionStock::" + id, number);
         rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
@@ -360,7 +361,8 @@ public class CollectionService {
 
     public Long decreaseStock(Long id, int number) {
         if (redisTemplate.opsForValue().get("collectionStock::" + id) == null) {
-            redisTemplate.opsForValue().set("collectionStock::" + id, collectionRepo.getStock(id));
+            redisTemplate.opsForValue().set("collectionStock::" + id,
+                    Optional.ofNullable(collectionRepo.getStock(id)).orElse(0));
         }
         Long stock = redisTemplate.opsForValue().decrement("collectionStock::" + id, number);
         rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
@@ -369,7 +371,8 @@ public class CollectionService {
 
     public Long increaseSale(Long id, int number) {
         if (redisTemplate.opsForValue().get("collectionSale::" + id) == null) {
-            redisTemplate.opsForValue().set("collectionSale::" + id, collectionRepo.getSale(id));
+            redisTemplate.opsForValue().set("collectionSale::" + id,
+                    Optional.ofNullable(collectionRepo.getSale(id)).orElse(0));
         }
         Long sale = redisTemplate.opsForValue().increment("collectionSale::" + id, number);
         rocketMQTemplate.convertAndSend(generalProperties.getUpdateSaleTopic(), id);
@@ -378,7 +381,8 @@ public class CollectionService {
 
     public Long decreaseSale(Long id, int number) {
         if (redisTemplate.opsForValue().get("collectionSale::" + id) == null) {
-            redisTemplate.opsForValue().set("collectionSale::" + id, collectionRepo.getSale(id));
+            redisTemplate.opsForValue().set("collectionSale::" + id,
+                    Optional.ofNullable(collectionRepo.getSale(id)).orElse(0));
         }
         Long sale = redisTemplate.opsForValue().decrement("collectionSale::" + id, number);
         rocketMQTemplate.convertAndSend(generalProperties.getUpdateSaleTopic(), id);