xiongzhu пре 4 година
родитељ
комит
f1f59ef5b5

+ 31 - 0
src/main/java/com/izouma/nineth/event/AccountCreatedEvent.java

@@ -0,0 +1,31 @@
+package com.izouma.nineth.event;
+
+import com.izouma.nineth.dto.NFTAccount;
+import org.springframework.context.ApplicationEvent;
+
+public class AccountCreatedEvent extends ApplicationEvent {
+    private final Long       userId;
+    private final NFTAccount account;
+
+    /**
+     * Create a new {@code ApplicationEvent}.
+     *
+     * @param source  the object on which the event initially occurred or with
+     *                which the event is associated (never {@code null})
+     * @param userId
+     * @param account
+     */
+    public AccountCreatedEvent(Object source, Long userId, NFTAccount account) {
+        super(source);
+        this.userId = userId;
+        this.account = account;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public NFTAccount getAccount() {
+        return account;
+    }
+}

+ 5 - 1
src/main/java/com/izouma/nineth/listener/MintListener.java

@@ -20,6 +20,10 @@ public class MintListener implements RocketMQListener<Long> {
 
     @Override
     public void onMessage(Long assetId) {
-        assetMintService.mint(assetId);
+        try {
+            assetMintService.mint(assetId);
+        } catch (Exception e) {
+            log.error("铸造失败 " + assetId, e);
+        }
     }
 }

+ 20 - 0
src/main/java/com/izouma/nineth/service/NFTService.java

@@ -13,13 +13,16 @@ import com.izouma.nineth.config.Constants;
 import com.izouma.nineth.config.GeneralProperties;
 import com.izouma.nineth.dto.NFT;
 import com.izouma.nineth.dto.NFTAccount;
+import com.izouma.nineth.event.AccountCreatedEvent;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.utils.SnowflakeIdWorker;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.binary.Hex;
+import org.springframework.context.ApplicationContext;
 import org.springframework.retry.annotation.Backoff;
 import org.springframework.retry.annotation.Retryable;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import java.math.BigInteger;
@@ -34,6 +37,23 @@ public class NFTService {
     private final RestClientProperties restClientProperties;
     private final GeneralProperties    generalProperties;
     private final SnowflakeIdWorker    snowflakeIdWorker;
+    private final ApplicationContext   context;
+
+    @Async
+    public void createAccount(Long userId) {
+        NFTAccount account = null;
+        for (int i = 0; i < 10; i++) {
+            try {
+                Thread.sleep(5000);
+                account = createAccount("account_" + userId);
+                break;
+            } catch (Exception e) {
+            }
+        }
+        if (account != null) {
+            context.publishEvent(new AccountCreatedEvent(this, userId, account));
+        }
+    }
 
     @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 5000), value = BusinessException.class)
     public NFTAccount createAccount(String username) {

+ 4 - 5
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -325,13 +325,12 @@ public class OrderService {
         if (collection.getSource().equals(CollectionSource.TRANSFER)) {
             Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("无记录"));
             User owner = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("拥有者用户不存在"));
-            restAmount = divMoney(restAmount, divMembers, "0", BigDecimal.valueOf(1), true); // gas费
             if (collection.getServiceCharge() + collection.getRoyalties() > 0) {
-                // 手续费和服务
-                restAmount = divMoney(totalAmount, restAmount, divMembers, "0",
-                        collection.getServiceCharge() + collection.getRoyalties(), true);
+                // 扣除手续费、服务费、GAS
+                restAmount = divMoney(totalAmount, restAmount, divMembers, owner.getMemberId(),
+                        100 - (collection.getServiceCharge() + collection.getRoyalties()), true);
             }
-            restAmount = divMoney(restAmount, divMembers, owner.getMemberId(), restAmount, false);
+            restAmount = divMoney(restAmount, divMembers, "0", restAmount, false);
         } else {
             if (invitor != null && invitor.getShareRatio() != null
                     && invitor.getShareRatio().compareTo(BigDecimal.ZERO) > 0) {

+ 27 - 26
src/main/java/com/izouma/nineth/service/UserService.java

@@ -12,6 +12,7 @@ import com.izouma.nineth.domain.User;
 import com.izouma.nineth.dto.*;
 import com.izouma.nineth.enums.AuthStatus;
 import com.izouma.nineth.enums.AuthorityName;
+import com.izouma.nineth.event.AccountCreatedEvent;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.*;
 import com.izouma.nineth.security.Authority;
@@ -33,6 +34,8 @@ import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.EventListener;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.jpa.domain.Specification;
@@ -49,23 +52,24 @@ import java.util.stream.Collectors;
 @Slf4j
 @AllArgsConstructor
 public class UserService {
-    private UserRepo          userRepo;
-    private WxMaService       wxMaService;
-    private WxMpService       wxMpService;
-    private SmsService        smsService;
-    private StorageService    storageService;
-    private JwtTokenUtil      jwtTokenUtil;
-    private CaptchaService    captchaService;
-    private FollowService     followService;
-    private FollowRepo        followRepo;
-    private IdentityAuthRepo  identityAuthRepo;
-    private SysConfigService  sysConfigService;
-    private CollectionService collectionService;
-    private AdapayService     adapayService;
-    private UserBankCardRepo  userBankCardRepo;
-    private InviteRepo        inviteRepo;
-    private NFTService        nftService;
-    private CacheService      cacheService;
+    private UserRepo           userRepo;
+    private WxMaService        wxMaService;
+    private WxMpService        wxMpService;
+    private SmsService         smsService;
+    private StorageService     storageService;
+    private JwtTokenUtil       jwtTokenUtil;
+    private CaptchaService     captchaService;
+    private FollowService      followService;
+    private FollowRepo         followRepo;
+    private IdentityAuthRepo   identityAuthRepo;
+    private SysConfigService   sysConfigService;
+    private CollectionService  collectionService;
+    private AdapayService      adapayService;
+    private UserBankCardRepo   userBankCardRepo;
+    private InviteRepo         inviteRepo;
+    private NFTService         nftService;
+    private CacheService       cacheService;
+    private ApplicationContext context;
 
     public User update(User user) {
         User orig = userRepo.findById(user.getId()).orElseThrow(new BusinessException("无记录"));
@@ -119,18 +123,15 @@ public class UserService {
         if (StringUtils.isNotBlank(userRegister.getPassword())) {
             user.setPassword(new BCryptPasswordEncoder().encode(userRegister.getPassword()));
         }
-        user = userRepo.save(user);
-        User finalUser = user;
-        new Thread(() -> {
-            NFTAccount account = nftService.createAccount(finalUser.getUsername() + "_");
-            finalUser.setNftAccount(account.getAccountId());
-            finalUser.setKmsId(account.getAccountKmsId());
-            finalUser.setPublicKey(account.getPublicKey());
-            userRepo.save(finalUser);
-        }).start();
+        user = userRepo.saveAndFlush(user);
+        nftService.createAccount(user.getId());
         return user;
     }
 
+    @EventListener
+    public void accountCreated(AccountCreatedEvent event) {
+    }
+
     public User phoneRegister(String phone, String code, String password, String inviteCode) {
         String name = "9th_" + RandomStringUtils.randomAlphabetic(8);
         Invite invite = null;

+ 4 - 4
src/test/java/com/izouma/nineth/service/AdapayServiceTest.java

@@ -36,7 +36,7 @@ public class AdapayServiceTest extends ApplicationTests {
         ForkJoinPool customThreadPool = new ForkJoinPool(100);
         customThreadPool.submit(() -> {
 
-            List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/merTransDetail_0284905900625472_20220112_20220119_1642583914.xlsx")
+            List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/merTransDetail_0284905900625472_20220121_20220121_1642753409.xlsx")
                     .head(AdaTrade.class).sheet().doReadSync();
             list = list.parallelStream().filter(adaTrade -> {
                 return adaTrade.get交易金额().equals("1.00")
@@ -56,7 +56,7 @@ public class AdapayServiceTest extends ApplicationTests {
     public void refund() {
         List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/1.xlsx")
                 .head(AdaTrade.class).sheet().doReadSync();
-        for (AdaTrade adaTrade : list) {
+        list.parallelStream().forEach(adaTrade -> {
             Map<String, Object> refundParams = new HashMap<>();
             refundParams.put("refund_amt", adaTrade.get交易金额());
             refundParams.put("refund_order_no", snowflakeIdWorker.nextId() + "");
@@ -65,12 +65,12 @@ public class AdapayServiceTest extends ApplicationTests {
             } catch (BaseAdaPayException e) {
                 e.printStackTrace();
             }
-        }
+        });
     }
 
     @Test
     public void verifyRefund() throws BaseAdaPayException {
-        List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/2.xlsx")
+        List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/1.xlsx")
                 .head(AdaTrade.class).sheet().doReadSync();
         list = list.parallelStream().filter(adaTrade -> {
             boolean success = false;

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

@@ -231,7 +231,7 @@ public class OrderServiceTest extends ApplicationTests {
 
     @Test
     public void asdf() {
-        orderRepo.findByCollectionId(1175138L).stream().filter(o -> o.getStatus() == OrderStatus.FINISH).parallel()
+        orderRepo.findByCollectionId(1226466L).stream().filter(o -> o.getStatus() == OrderStatus.FINISH).parallel()
                 .forEach(order -> {
                     List<Asset> assets = assetRepo.findByOrderId(order.getId());
                     if (assets.size() > 1) {