xiongzhu пре 3 година
родитељ
комит
7d8c6e8e81

+ 36 - 0
src/main/java/com/izouma/nineth/service/AirDropService.java

@@ -1,5 +1,6 @@
 package com.izouma.nineth.service;
 
+import com.izouma.nineth.TokenHistory;
 import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.enums.AirDropType;
@@ -13,6 +14,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 @Service
@@ -28,6 +30,8 @@ public class AirDropService {
     private AssetService      assetService;
     private CollectionService collectionService;
     private ShowroomService   showroomService;
+    private TokenHistoryRepo  tokenHistoryRepo;
+    private AssetRepo         assetRepo;
 
     public Page<AirDrop> all(PageQuery pageQuery) {
         return airDropRepo.findAll(JpaUtils.toSpecification(pageQuery, AirDrop.class), JpaUtils.toPageRequest(pageQuery));
@@ -87,4 +91,36 @@ public class AirDropService {
         }
         return airDropRepo.save(record);
     }
+
+    public void drop(Long collectionId, Long userId, int num, LocalDateTime time) {
+        Collection collection = collectionRepo.findById(collectionId)
+                .orElseThrow(new BusinessException("藏品不存在"));
+        User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
+        try {
+            for (int i = 0; i < num; i++) {
+                Asset asset;
+                if (collection.getType() == CollectionType.BLIND_BOX) {
+                    BlindBoxItem winItem = collectionService.draw(collection.getId());
+                    asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
+                            winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
+                            collection.getHoldDays());
+                } else {
+                    asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
+                            collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
+                }
+                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);
+        }
+    }
 }

+ 21 - 0
src/test/java/com/izouma/nineth/service/AirDropServiceTest.java

@@ -0,0 +1,21 @@
+package com.izouma.nineth.service;
+
+import com.izouma.nineth.ApplicationTests;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.time.LocalDateTime;
+
+import static org.junit.Assert.*;
+
+
+public class AirDropServiceTest extends ApplicationTests {
+    @Autowired
+    AirDropService airDropService;
+
+    @Test
+    public void drop() {
+        airDropService.drop(4235490L, 4273750L, 498,
+                LocalDateTime.of(2022, 3, 25, 17, 30));
+    }
+}