xiongzhu 2 سال پیش
والد
کامیت
ee1e609471

+ 12 - 22
src/main/java/com/izouma/awesomeAdmin/service/VipAsyncService.java

@@ -23,32 +23,22 @@ public class VipAsyncService {
     private final DelegationService delegationService;
     private final UserVipRepo       userVipRepo;
 
-    @Async
     @Transactional
-    public Future<Void> autoTrade(UserVip userVip, Product product) {
+    public void autoTrade(UserVip userVip, Product product) {
         Long userId = userVip.getUserId();
-        try {
-            log.info("🚚 start, userId: {}, productId: {}", userId, product.getId());
-            Order order = orderService.createOrder(userId, product.getId(), null, true);
-            log.info("🚚 create order success, userId: {}, productId: {}, orderId: {}", userId, product.getId(), order.getId());
+        log.info("🚚 start, userId: {}, productId: {}", userId, product.getId());
+        Order order = orderService.createOrder(userId, product.getId(), null, true);
+        log.info("🚚 create order success, userId: {}, productId: {}, orderId: {}", userId, product.getId(), order.getId());
 
-            log.info("🚚 pay start, userId: {}, productId: {}", userId, product.getId());
-            orderService.balancePay(userId, order.getId());
-            log.info("🚚 pay success, userId: {}, productId: {}", userId, product.getId());
+        log.info("🚚 pay start, userId: {}, productId: {}", userId, product.getId());
+        orderService.balancePay(userId, order.getId());
+        log.info("🚚 pay success, userId: {}, productId: {}", userId, product.getId());
 
-            userVipRepo.increaseUsed(userId);
-            log.info("🚚 increase used limit, userId: {}, left: {}", userId, userVip.getDailyLimit() - userVip.getTodayUsed() - 1);
+        userVipRepo.increaseUsed(userId);
+        log.info("🚚 increase used limit, userId: {}, left: {}", userId, userVip.getDailyLimit() - userVip.getTodayUsed() - 1);
 
-            log.info("🚚 pay delegation start, userId: {}, productId: {}", userId, product.getId());
-            delegationService.payDelegationBalance(userId, order.getId());
-            log.info("🚚 pay delegation success, userId: {}, productId: {}", userId, product.getId());
-        } catch (Exception e) {
-            if (e instanceof BusinessException) {
-                log.error("🚚 error, message: {}", e.getMessage());
-            } else {
-                log.error("🚚 error", e);
-            }
-        }
-        return new AsyncResult<>(null);
+        log.info("🚚 pay delegation start, userId: {}, productId: {}", userId, product.getId());
+        delegationService.payDelegationBalance(userId, order.getId());
+        log.info("🚚 pay delegation success, userId: {}, productId: {}", userId, product.getId());
     }
 }

+ 1 - 2
src/main/java/com/izouma/awesomeAdmin/service/VipService.java

@@ -155,9 +155,8 @@ public class VipService {
                     continue;
                 }
                 UserVip chosenVip = vips.get(new Random().nextInt(vips.size()));
-                Future result = vipAsyncService.autoTrade(chosenVip, product);
                 try {
-                    result.get();
+                    vipAsyncService.autoTrade(chosenVip, product);
                 } catch (Exception e) {
                     log.error("🚚 auto trade error", e);
                 }

+ 2 - 0
src/main/java/com/izouma/awesomeAdmin/web/TelegramBot.java

@@ -5,6 +5,7 @@ import com.izouma.awesomeAdmin.exception.BusinessException;
 import com.izouma.awesomeAdmin.service.TelegramBotConfig;
 import com.izouma.awesomeAdmin.service.UserBalanceService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.stereotype.Service;
 import org.telegram.abilitybots.api.bot.AbilityBot;
@@ -28,6 +29,7 @@ import static org.telegram.abilitybots.api.objects.Flag.REPLY;
 @EnableConfigurationProperties(TelegramBotConfig.class)
 @Service
 @Slf4j
+@ConditionalOnProperty(name = "tg-bot.enabled", havingValue = "true")
 public class TelegramBot extends AbilityBot {
 
     private final TelegramBotConfig  config;

+ 1 - 1
src/main/resources/logback-spring.xml

@@ -11,7 +11,7 @@
         <root level="INFO">
             <appender-ref ref="CONSOLE"/>
         </root>
-        <logger name="org.hibernate.SQL" level="DEBUG"/>
+<!--        <logger name="org.hibernate.SQL" level="DEBUG"/>-->
 <!--        <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE"/>-->
 <!--        <logger name="org.springframework.jdbc.core.JdbcTemplate" level="DEBUG"/>-->
 <!--        <logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="TRACE"/>-->

+ 49 - 0
src/test/java/com/izouma/awesomeAdmin/service/VipAsyncServiceTest.java

@@ -0,0 +1,49 @@
+package com.izouma.awesomeAdmin.service;
+
+import com.izouma.awesomeAdmin.domain.Product;
+import com.izouma.awesomeAdmin.domain.UserVip;
+import com.izouma.awesomeAdmin.repo.ProductRepo;
+import com.izouma.awesomeAdmin.repo.UserVipRepo;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@ExtendWith(SpringExtension.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+public class VipAsyncServiceTest {
+
+    @Autowired
+    private VipAsyncService vipAsyncService;
+    @Autowired
+    private UserVipRepo     userVipRepo;
+    @Autowired
+    private ProductRepo     productRepo;
+
+    @Test
+    public void autoTrade() throws InterruptedException {
+        Product product = productRepo.findById(58203L).get();
+        Thread t1 = new Thread(() -> {
+            try {
+                vipAsyncService.autoTrade(userVipRepo.findByUserId(11648L), product);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+        t1.start();
+        Thread t2 = new Thread(() -> {
+            try {
+                vipAsyncService.autoTrade(userVipRepo.findByUserId(60798L), product);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+        t2.start();
+
+        t1.join();
+        t2.join();
+    }
+}