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

+ 2 - 0
src/main/java/com/izouma/nineth/repo/AssetRepo.java

@@ -101,4 +101,6 @@ public interface AssetRepo extends JpaRepository<Asset, Long>, JpaSpecificationE
     @Query(nativeQuery = true, value = "select id from asset where user_id = ?1 and collection_id in ?2 " +
             "and status = 'NORMAL' limit 1")
     Long findDiscount(Long userId, Collection<Long> ids);
+
+    List<Asset> findByStatus(AssetStatus status);
 }

+ 27 - 1
src/main/java/com/izouma/nineth/service/AssetService.java

@@ -1,6 +1,5 @@
 package com.izouma.nineth.service;
 
-import cn.hutool.core.convert.Convert;
 import com.google.common.collect.Lists;
 import com.izouma.nineth.TokenHistory;
 import com.izouma.nineth.config.Constants;
@@ -28,6 +27,7 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
@@ -39,6 +39,7 @@ import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 @Service
@@ -61,6 +62,7 @@ public class AssetService {
     private MintActivityRepo        mintActivityRepo;
     private DestroyRecordRepo       destroyRecordRepo;
     private AirDropService          airDropService;
+    private HCChainService          hcChainService;
 
     public Page<Asset> all(PageQuery pageQuery) {
         Page<Asset> all = assetRepo
@@ -816,4 +818,28 @@ public class AssetService {
         }
         return royalties;
     }
+
+    @Async
+    public void hcChain() throws ExecutionException, InterruptedException {
+        new ForkJoinPool(1000).submit(() -> {
+            AtomicInteger num = new AtomicInteger();
+            assetRepo.findByStatus(AssetStatus.NORMAL).parallelStream()
+                    .forEach(asset -> {
+                        if (asset.getHcTxHash() == null) {
+                            User user = userRepo.findById(asset.getUserId()).orElse(null);
+                            if (user != null) {
+                                if (user.getHcChainAddress() == null) {
+                                    user.setHcChainAddress(hcChainService.createAccount(asset.getUserId()));
+                                }
+                                NFT nft = hcChainService.mint(user.getHcChainAddress(), asset.getTokenId());
+                                asset.setHcTokenId(nft.getTokenId());
+                                asset.setHcTxHash(nft.getTxHash());
+                                asset.setGasUsed(nft.getGasUsed());
+                                assetRepo.save(asset);
+                            }
+                        }
+                        log.info("hcChain:" + num.getAndIncrement());
+                    });
+        }).get();
+    }
 }

+ 9 - 4
src/main/java/com/izouma/nineth/web/AssetController.java

@@ -36,11 +36,11 @@ import java.util.concurrent.ExecutionException;
 @RequestMapping("/asset")
 @AllArgsConstructor
 public class AssetController extends BaseController {
-    private AssetService assetService;
-    private AssetRepo assetRepo;
+    private AssetService     assetService;
+    private AssetRepo        assetRepo;
     private GiftOrderService giftOrderService;
-    private OrderRepo orderRepo;
-    private CacheService cacheService;
+    private OrderRepo        orderRepo;
+    private CacheService     cacheService;
 
     //@PreAuthorize("hasRole('ADMIN')")
 //    @PostMapping("/save")
@@ -217,6 +217,11 @@ public class AssetController extends BaseController {
         return assetService.getRoyalties(asset.getMinterId(), asset.getRoyalties(), SecurityUtils.getAuthenticatedUser()
                 .getId());
     }
+
+    @GetMapping("/hcChain")
+    public void hcChain() throws ExecutionException, InterruptedException {
+        assetService.hcChain();
+    }
 }
 
 

+ 27 - 1
src/test/java/com/izouma/nineth/service/AssetServiceTest.java

@@ -5,7 +5,9 @@ import com.alibaba.excel.annotation.ExcelProperty;
 import com.izouma.nineth.ApplicationTests;
 import com.izouma.nineth.TokenHistory;
 import com.izouma.nineth.domain.*;
+import com.izouma.nineth.dto.NFT;
 import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.enums.AssetStatus;
 import com.izouma.nineth.enums.TransferReason;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.*;
@@ -25,6 +27,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -51,6 +54,8 @@ class AssetServiceTest extends ApplicationTests {
     private TokenHistoryRepo  tokenHistoryRepo;
     @Autowired
     private RocketMQTemplate  rocketMQTemplate;
+    @Autowired
+    private HCChainService    hcChainService;
 
     @Test
     void createAsset() {
@@ -259,7 +264,28 @@ class AssetServiceTest extends ApplicationTests {
 
     @Test
     public void destroy() {
-        assetService.destroy(8025352L,9972L);
+        assetService.destroy(8025352L, 9972L);
     }
 
+    @Test
+    public void hcChain() throws ExecutionException, InterruptedException {
+        new ForkJoinPool(1000).submit(() -> {
+            assetRepo.findByStatus(AssetStatus.NORMAL).parallelStream()
+                    .forEach(asset -> {
+                        if (asset.getHcTxHash() == null) {
+                            User user = userRepo.findById(asset.getUserId()).orElse(null);
+                            if (user != null) {
+                                if (user.getHcChainAddress() == null) {
+                                    user.setHcChainAddress(hcChainService.createAccount(asset.getUserId()));
+                                }
+                                NFT nft = hcChainService.mint(user.getHcChainAddress(), asset.getTokenId());
+                                asset.setHcTokenId(nft.getTokenId());
+                                asset.setHcTxHash(nft.getTxHash());
+                                asset.setGasUsed(nft.getGasUsed());
+                                assetRepo.save(asset);
+                            }
+                        }
+                    });
+        }).get();
+    }
 }