xiongzhu 3 роки тому
батько
коміт
33bf5a7c3e

+ 29 - 34
src/main/java/com/izouma/nineth/service/AirDropService.java

@@ -20,6 +20,7 @@ import org.springframework.stereotype.Service;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 
 @Service
 @Service
 @Slf4j
 @Slf4j
@@ -47,6 +48,8 @@ public class AirDropService {
     private AssetRepo               assetRepo;
     private AssetRepo               assetRepo;
     @Autowired
     @Autowired
     private CollectionPrivilegeRepo collectionPrivilegeRepo;
     private CollectionPrivilegeRepo collectionPrivilegeRepo;
+    @Autowired
+    private SysConfigService        sysConfigService;
 
 
     public Page<AirDrop> all(PageQuery pageQuery) {
     public Page<AirDrop> all(PageQuery pageQuery) {
         return airDropRepo
         return airDropRepo
@@ -54,6 +57,7 @@ public class AirDropService {
     }
     }
 
 
     public AirDrop create(AirDrop record) {
     public AirDrop create(AirDrop record) {
+        boolean parallel = sysConfigService.getBoolean("parallel_airdrop");
         if (record.getTargets().isEmpty()) throw new BusinessException("空投对象不能为空");
         if (record.getTargets().isEmpty()) throw new BusinessException("空投对象不能为空");
         if (record.getTargets().stream().mapToInt(DropTarget::getNum).sum() > 300)
         if (record.getTargets().stream().mapToInt(DropTarget::getNum).sum() > 300)
             throw new BusinessException("空投数量不能超过300");
             throw new BusinessException("空投数量不能超过300");
@@ -82,7 +86,11 @@ public class AirDropService {
             List<User> users = userRepo.findByIdInAndDelFalse(record.getTargets().stream()
             List<User> users = userRepo.findByIdInAndDelFalse(record.getTargets().stream()
                     .map(DropTarget::getUserId).collect(Collectors.toList()));
                     .map(DropTarget::getUserId).collect(Collectors.toList()));
 
 
-            record.getTargets().forEach(target -> {
+            Stream<DropTarget> stream = record.getTargets().stream();
+            if (parallel) {
+                stream = stream.parallel();
+            }
+            stream.forEach(target -> {
                 User user = users.stream().filter(u -> u.getId().equals(target.getUserId()))
                 User user = users.stream().filter(u -> u.getId().equals(target.getUserId()))
                         .findFirst().orElse(null);
                         .findFirst().orElse(null);
                 if (user == null) return;
                 if (user == null) return;
@@ -162,6 +170,14 @@ public class AirDropService {
         drop(collectionId, userId, num, time);
         drop(collectionId, userId, num, time);
     }
     }
 
 
+    /**
+     * 空投(交易历史显示为购买)
+     *
+     * @param collectionId 藏品id
+     * @param userId       用户id
+     * @param num          空投数量
+     * @param time         购买时间
+     */
     public void drop(Long collectionId, Long userId, int num, LocalDateTime time) {
     public void drop(Long collectionId, Long userId, int num, LocalDateTime time) {
         Collection collection = collectionRepo.findById(collectionId)
         Collection collection = collectionRepo.findById(collectionId)
                 .orElseThrow(new BusinessException("藏品不存在"));
                 .orElseThrow(new BusinessException("藏品不存在"));
@@ -184,8 +200,18 @@ public class AirDropService {
                 assetRepo.save(asset);
                 assetRepo.save(asset);
                 for (TokenHistory tokenHistory : tokenHistoryRepo
                 for (TokenHistory tokenHistory : tokenHistoryRepo
                         .findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
                         .findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
-                    tokenHistory.setCreatedAt(asset.getCreatedAt());
-                    tokenHistoryRepo.save(tokenHistory);
+                    new Thread(() -> {
+                        try {
+                            Thread.sleep(1000);
+                        } catch (InterruptedException e) {
+                            throw new RuntimeException(e);
+                        }
+                        tokenHistoryRepo.findById(tokenHistory.getId())
+                                .ifPresent(t -> {
+                                    t.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
+                                    tokenHistoryRepo.save(t);
+                                });
+                    }).start();
                 }
                 }
                 log.info("空投成功{}/{} collectionId={}, userId={}", i + 1, num, collectionId, userId);
                 log.info("空投成功{}/{} collectionId={}, userId={}", i + 1, num, collectionId, userId);
             }
             }
@@ -193,35 +219,4 @@ public class AirDropService {
             log.error("空投出错", e);
             log.error("空投出错", e);
         }
         }
     }
     }
-
-    public void drop(List<Collection> collections, String phone, LocalDateTime time) {
-//        List<Collection> collections = collectionRepo.findAllById(collectionId);
-        User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("用户不存在"));
-        try {
-            for (Collection collection : collections) {
-                Asset asset;
-                if (collection.getType() == CollectionType.BLIND_BOX) {
-                    BlindBoxItem winItem = collectionService.draw(user.getId(), collection.getId());
-                    asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
-                            collectionService.getNextNumber(winItem), collection.getHoldDays(), false);
-                } else {
-                    asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
-                            collectionService.getNextNumber(collection), false);
-                }
-                assetRepo.flush();
-                tokenHistoryRepo.flush();
-
-                asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
-                assetRepo.save(asset);
-
-                for (TokenHistory tokenHistory : tokenHistoryRepo
-                        .findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
-                    tokenHistory.setCreatedAt(asset.getCreatedAt());
-                    tokenHistoryRepo.save(tokenHistory);
-                }
-            }
-        } catch (Exception e) {
-            log.error("空投出错", e);
-        }
-    }
 }
 }

+ 1 - 0
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -497,6 +497,7 @@ public class CollectionService {
                 operations.putIfAbsent(winItem.getCollectionId() + "", winItem.getStock());
                 operations.putIfAbsent(winItem.getCollectionId() + "", winItem.getStock());
                 int stock = Math.toIntExact(operations.increment(winItem.getCollectionId() + "", -1));
                 int stock = Math.toIntExact(operations.increment(winItem.getCollectionId() + "", -1));
                 if (stock < 0) {
                 if (stock < 0) {
+                    log.info("over draw {} {}", stock, winItem.getCollectionId());
                     operations.increment(winItem.getCollectionId() + "", 1);
                     operations.increment(winItem.getCollectionId() + "", 1);
                     winItem = null;
                     winItem = null;
                 }
                 }

+ 8 - 0
src/main/java/com/izouma/nineth/service/SysConfigService.java

@@ -251,6 +251,14 @@ public class SysConfigService {
                     .options("payEase,sandPay")
                     .options("payEase,sandPay")
                     .build());
                     .build());
         }
         }
+        if (list.stream().noneMatch(i -> i.getName().equals("parallel_airdrop"))) {
+            sysConfigRepo.save(SysConfig.builder()
+                    .name("parallel_airdrop")
+                    .desc("并发空投")
+                    .type(SysConfig.ValueType.BOOLEAN)
+                    .value("1")
+                    .build());
+        }
         SearchMode searchMode = SearchMode.valueOf(sysConfigRepo.findByName("default_search_mode").get().getValue());
         SearchMode searchMode = SearchMode.valueOf(sysConfigRepo.findByName("default_search_mode").get().getValue());
         JpaUtils.setDefaultSearchMode(searchMode);
         JpaUtils.setDefaultSearchMode(searchMode);