Browse Source

抢购优化

xiongzhu 4 years ago
parent
commit
8467d6466e

+ 9 - 2
src/main/java/com/izouma/nineth/service/UserService.java

@@ -36,6 +36,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.context.event.EventListener;
 import org.springframework.context.event.EventListener;
+import org.springframework.core.env.Environment;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.data.jpa.domain.Specification;
@@ -75,6 +76,7 @@ public class UserService {
     private AdapayMerchantService adapayMerchantService;
     private AdapayMerchantService adapayMerchantService;
     private PointRecordRepo       pointRecordRepo;
     private PointRecordRepo       pointRecordRepo;
     private CollectionService     collectionService;
     private CollectionService     collectionService;
+    private Environment           env;
 
 
     public User update(User user) {
     public User update(User user) {
         if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN)) {
         if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN)) {
@@ -150,7 +152,9 @@ public class UserService {
             user.setPassword(new BCryptPasswordEncoder().encode(userRegister.getPassword()));
             user.setPassword(new BCryptPasswordEncoder().encode(userRegister.getPassword()));
         }
         }
         user = userRepo.saveAndFlush(user);
         user = userRepo.saveAndFlush(user);
-        nftService.createAccount(user.getId());
+        if (Arrays.asList(env.getActiveProfiles()).contains("prod")) {
+            nftService.createAccount(user.getId());
+        }
         return user;
         return user;
     }
     }
 
 
@@ -170,7 +174,10 @@ public class UserService {
         if (StringUtils.isNotBlank(inviteCode)) {
         if (StringUtils.isNotBlank(inviteCode)) {
             invite = inviteRepo.findFirstByCode(inviteCode).orElse(null);
             invite = inviteRepo.findFirstByCode(inviteCode).orElse(null);
         }
         }
-        smsService.verify(phone, code);
+        if (!Arrays.asList(env.getActiveProfiles()).contains("test")) {
+            smsService.verify(phone, code);
+        }
+
         Collection collection = null;
         Collection collection = null;
         if (collectionId != null) {
         if (collectionId != null) {
             collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
             collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));

+ 31 - 8
src/test/java/com/izouma/nineth/service/UserServiceTest.java

@@ -19,17 +19,24 @@ import com.izouma.nineth.repo.UserRepo;
 import com.izouma.nineth.security.Authority;
 import com.izouma.nineth.security.Authority;
 import com.izouma.nineth.service.storage.StorageService;
 import com.izouma.nineth.service.storage.StorageService;
 import com.izouma.nineth.utils.BankUtils;
 import com.izouma.nineth.utils.BankUtils;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.junit.Test;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 
 
 import java.io.File;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Collections;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
+import java.util.stream.Collectors;
 
 
 public class UserServiceTest extends ApplicationTests {
 public class UserServiceTest extends ApplicationTests {
 
 
@@ -187,14 +194,30 @@ public class UserServiceTest extends ApplicationTests {
     }
     }
 
 
     @Test
     @Test
-    public void test1() {
-        List<User> users = userRepo.findAll();
-        users.forEach(user -> {
-            if (user.getVipPoint() > 1) {
-                user.setVipPoint(1);
-            } else {
-
+    public void test1() throws IOException, ExecutionException, InterruptedException {
+        List<String> list = new ArrayList<>();
+        for (int i = 0; i < 10000; i++) {
+            list.add("19" + RandomStringUtils.randomNumeric(11));
+        }
+        ForkJoinPool customThreadPool = new ForkJoinPool(1000);
+        customThreadPool.submit(() -> {
+            list.parallelStream().forEach(phone -> {
+                userService.create(UserRegister.builder()
+                        .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
+                        .avatar(Constants.DEFAULT_AVATAR)
+                        .nickname(phone)
+                        .username(phone)
+                        .phone(phone)
+                        .password(new BCryptPasswordEncoder().encode("123456"))
+                        .build());
+            });
+            try {
+                FileUtils.write(new File("/Users/drew/Download/accounts.csv"),
+                        list.stream().map(s -> s + ",123456").collect(Collectors.joining("\n")),
+                        StandardCharsets.UTF_8);
+            } catch (IOException e) {
+                e.printStackTrace();
             }
             }
-        });
+        }).get();
     }
     }
 }
 }