xiongzhu 3 лет назад
Родитель
Сommit
c58b193111

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

@@ -438,14 +438,16 @@ public class CollectionService {
         log.info("blindBoxDraw, userId={}, collectionId={}", userId, collectionId);
         List<BlindBoxItem> items = blindBoxItemRepo.findByBlindBoxId(collectionId);
         BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(RedisKeys.DRAW_BLIND_BOX + collectionId);
+        Map<Object, Object> entries = operations.entries();
 
         Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
         int c = 0, sum = 0;
         for (BlindBoxItem item : items) {
             if (item.getStock() > 0) {
+                int stock = Optional.ofNullable(entries.get(item.getCollectionId() + "")).map(i -> (int) i).orElse(item.getStock());
                 randomRange.put(item, Range.between(c, c + item.getStock()));
-                c += item.getStock();
-                sum += item.getStock();
+                c += stock;
+                sum += stock;
             }
         }
 

+ 2 - 1
src/test/java/com/izouma/nineth/RedisTest.java

@@ -12,7 +12,8 @@ public class RedisTest extends ApplicationTests {
 
     @Test
     public void testHash() {
-        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(RedisKeys.DRAW_BLIND_BOX + "1");
+        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(RedisKeys.DRAW_BLIND_BOX + "111");
+        System.out.println(operations.entries());
         operations.putIfAbsent("1", "1");
         operations.putIfAbsent("1", "2");
         System.out.println(operations.entries());