licailing 3 anos atrás
pai
commit
0903ff5a58

+ 16 - 0
src/main/java/com/izouma/nineth/dto/PointDTO.java

@@ -0,0 +1,16 @@
+package com.izouma.nineth.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.time.LocalDateTime;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class PointDTO {
+    private Long          id;
+    private LocalDateTime createdAt;
+    private int           sum;
+}

+ 4 - 0
src/main/java/com/izouma/nineth/repo/PointRecordRepo.java

@@ -3,6 +3,10 @@ package com.izouma.nineth.repo;
 import com.izouma.nineth.domain.PointRecord;
 import org.springframework.data.jpa.repository.JpaRepository;
 
+import java.util.List;
+
 public interface PointRecordRepo extends JpaRepository<PointRecord, Long> {
     int countByUserIdAndCollectionId(Long userId, Long collectionId);
+
+    List<PointRecord> findAllByCollectionId(Long collectionId);
 }

+ 3 - 1
src/main/java/com/izouma/nineth/repo/UserRepo.java

@@ -202,10 +202,12 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
             " where collection_id = ?1" +
             " group by collection_invitor" +
             " having count(id) >= ?2")
-    List<Long> findAllByCollectionId(Long collectionId, int size);
+    List<Long> findInvitorByCollectionId(Long collectionId, int size);
 
     @Transactional
     @Modifying
     @Query("update User set vipPoint = 1 where vipPoint = 0 and id in ?1")
     void updateAllByInvitor(Collection<Long> ids);
+
+    List<User> findAllByCollectionId(Long collectionId);
 }

+ 55 - 0
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -17,6 +17,7 @@ import com.izouma.nineth.utils.SecurityUtils;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.Range;
 import org.apache.commons.lang3.StringUtils;
@@ -43,6 +44,7 @@ import java.time.ZoneId;
 import java.util.*;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 @Service
@@ -62,6 +64,7 @@ public class CollectionService {
     private GeneralProperties             generalProperties;
     private Environment                   env;
     private OrderRepo                     orderRepo;
+    private PointRecordRepo               pointRecordRepo;
 
     private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
 
@@ -470,4 +473,56 @@ public class CollectionService {
             return collectionDTO;
         }).collect(Collectors.toList());
     }
+
+
+    public void savePoint(Long collectionId) {
+        Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
+        int stock = collection.getStock();
+        int assignment = collection.getAssignment();
+        if (assignment <= 0) {
+            return;
+        }
+        List<User> users = userRepo.findAllByCollectionId(collectionId);
+        Map<Long, List<User>> userMap = users.stream()
+                .filter(user -> ObjectUtils.isNotEmpty(user.getCollectionInvitor()))
+                .collect(Collectors.groupingBy(User::getCollectionInvitor));
+
+        AtomicInteger sum = new AtomicInteger();
+        List<PointDTO> dtos = new ArrayList<>();
+        userMap.forEach((key, value) -> {
+            if (value.size() >= collection.getAssignment()) {
+                value.sort(Comparator.comparing(User::getCreatedAt));
+                User user = value.get(collection.getAssignment() - 1);
+                dtos.add(new PointDTO(key, user.getCreatedAt(), value.size()));
+                sum.getAndIncrement();
+            }
+        });
+        log.info("完成任务人数:{}", sum);
+
+        List<PointDTO> result = dtos.stream()
+                .sorted(Comparator.comparing(PointDTO::getCreatedAt))
+                .limit(stock)
+                .collect(Collectors.toList());
+        List<Long> userIds = result.stream().map(PointDTO::getId).collect(Collectors.toList());
+        Map<Long, User> resultMap = userRepo.findAllById(userIds)
+                .stream()
+                .collect(Collectors.toMap(User::getId, user -> user));
+
+        result.forEach(pointDTO -> {
+            User user = resultMap.get(pointDTO.getId());
+            if (user.getVipPoint() <= 0) {
+                user.setVipPoint(1);
+                userRepo.save(user);
+//                userRepo.updateVipPoint(pointDTO.getId(), 1);
+                pointRecordRepo.save(PointRecord.builder()
+                        .collectionId(collectionId)
+                        .userId(pointDTO.getId())
+                        .type("VIP_POINT")
+                        .point(1)
+                        .build());
+            }
+
+        });
+
+    }
 }

+ 39 - 37
src/main/java/com/izouma/nineth/service/UserService.java

@@ -46,7 +46,6 @@ import org.springframework.stereotype.Service;
 import javax.persistence.criteria.Predicate;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
-import java.time.LocalDateTime;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.regex.Pattern;
@@ -73,8 +72,6 @@ public class UserService {
     private TokenHistoryRepo      tokenHistoryRepo;
     private CollectionRepo        collectionRepo;
     private AdapayMerchantService adapayMerchantService;
-    private PointRecordRepo       pointRecordRepo;
-    private CollectionService     collectionService;
 
     public User update(User user) {
         if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN)) {
@@ -171,15 +168,19 @@ public class UserService {
             invite = inviteRepo.findFirstByCode(inviteCode).orElse(null);
         }
         smsService.verify(phone, code);
-        Collection collection = null;
+        Collection collection;
         if (collectionId != null) {
             collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
-            if (!collection.isOnShelf() || !collection.isSalable()) {
+//            if (!collection.isOnShelf() || !collection.isSalable()) {
+//                collectionId = null;
+//            } else if (collection.isScheduleSale()) {
+//                if (collection.getStartTime().isAfter(LocalDateTime.now())) {
+//                    collectionId = null;
+//                }
+//            }
+            // 只看是否开去分享
+            if (ObjectUtils.isEmpty(collection.getOpenQuota()) || !collection.getOpenQuota()) {
                 collectionId = null;
-            } else if (collection.isScheduleSale()) {
-                if (collection.getStartTime().isAfter(LocalDateTime.now())) {
-                    collectionId = null;
-                }
             }
         }
         User user = create(UserRegister.builder()
@@ -200,34 +201,34 @@ public class UserService {
         }
 
         // 加积分
-        if (collectionId != null && invitor != null) {
-            // 额度或者额度为空, 库存不为空
-            if (collection.getStock() > 0 && (collection.getVipQuota() > 0 || ObjectUtils.isEmpty(collection.getVipQuota()))) {
-                int countUser = userRepo.countAllByCollectionIdAndCollectionInvitor(collectionId, invitor);
-                // 邀请人数
-                if (countUser >= collection.getAssignment()) {
-                    int point = pointRecordRepo.countByUserIdAndCollectionId(invitor, collectionId);
-                    // 是否已有积分
-                    if (point <= 0) {
-                        long count = userRepo.countAllByCollectionIdAndCollectionInvitor(collectionId, invitor);
-                        if (count >= collection.getAssignment()) {
-                            // 扣除藏品额度
-                            if (ObjectUtils.isNotEmpty(collection.getVipQuota())) {
-                                collectionService.decreaseQuota(collectionId, 1);
-                            }
-                            userRepo.updateVipPoint(invitor, 1);
-                            pointRecordRepo.save(PointRecord.builder()
-                                    .collectionId(collectionId)
-                                    .userId(invitor)
-                                    .type("VIP_POINT")
-                                    .point(1)
-                                    .build());
-
-                        }
-                    }
-                }
-            }
-        }
+//        if (collectionId != null && invitor != null) {
+//            // 额度或者额度为空, 库存不为空
+//            if (collection.getStock() > 0 && (collection.getVipQuota() > 0 || ObjectUtils.isEmpty(collection.getVipQuota()))) {
+//                int countUser = userRepo.countAllByCollectionIdAndCollectionInvitor(collectionId, invitor);
+//                // 邀请人数
+//                if (countUser >= collection.getAssignment()) {
+//                    int point = pointRecordRepo.countByUserIdAndCollectionId(invitor, collectionId);
+//                    // 是否已有积分
+//                    if (point <= 0) {
+//                        long count = userRepo.countAllByCollectionIdAndCollectionInvitor(collectionId, invitor);
+//                        if (count >= collection.getAssignment()) {
+//                            // 扣除藏品额度
+//                            if (ObjectUtils.isNotEmpty(collection.getVipQuota())) {
+//                                collectionService.decreaseQuota(collectionId, 1);
+//                            }
+//                            userRepo.updateVipPoint(invitor, 1);
+//                            pointRecordRepo.save(PointRecord.builder()
+//                                    .collectionId(collectionId)
+//                                    .userId(invitor)
+//                                    .type("VIP_POINT")
+//                                    .point(1)
+//                                    .build());
+//
+//                        }
+//                    }
+//                }
+//            }
+//        }
         return user;
     }
 
@@ -672,4 +673,5 @@ public class UserService {
     public User my(Long id) {
         return userRepo.findById(id).orElseThrow(new BusinessException("用户不存在"));
     }
+
 }

+ 1 - 2
src/test/java/com/izouma/nineth/repo/UserRepoTest.java

@@ -12,7 +12,6 @@ import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import java.time.LocalDate;
-import java.time.LocalDateTime;
 import java.util.*;
 
 @RunWith(SpringRunner.class)
@@ -67,7 +66,7 @@ public class UserRepoTest {
 
     @Test
     public void test1() {
-        List<Long> userIds = userRepo.findAllByCollectionId(3460186L, 2);
+        List<Long> userIds = userRepo.findInvitorByCollectionId(3460186L, 2);
         System.out.println(userIds.size());
         userRepo.updateAllByInvitor(userIds);
     }

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

@@ -131,11 +131,16 @@ class CollectionServiceTest extends ApplicationTests {
         if (!collection.isOnShelf() || !collection.isSalable()) {
             collectionId = null;
         } else if (collection.isScheduleSale()) {
-            if (collection.getStartTime().isAfter(LocalDateTime.now())){
+            if (collection.getStartTime().isAfter(LocalDateTime.now())) {
                 collectionId = null;
             }
         }
         System.out.println(collectionId);
     }
 
+    @Test
+    public void savePoint() {
+        collectionService.savePoint(8012L);
+    }
+
 }