|
|
@@ -8,21 +8,21 @@ import com.izouma.awesomeAdmin.repo.UserVipRepo;
|
|
|
import com.izouma.awesomeAdmin.repo.VipConfigRepo;
|
|
|
import com.izouma.awesomeAdmin.repo.VipPurchaseRepo;
|
|
|
import com.izouma.awesomeAdmin.repo.VipTypeRepo;
|
|
|
-import com.izouma.awesomeAdmin.utils.ObjUtils;
|
|
|
import com.izouma.awesomeAdmin.utils.Translator;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.transaction.Transactional;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.Future;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -38,6 +38,8 @@ public class VipService {
|
|
|
private VipPurchaseRepo vipPurchaseRepo;
|
|
|
private OrderService orderService;
|
|
|
private ProductService productService;
|
|
|
+ private DelegationService delegationService;
|
|
|
+ private VipAsyncService vipAsyncService;
|
|
|
|
|
|
@Transactional
|
|
|
public void purchaseVip(Long userId, Long vipTypeId, Long vipConfigId, int num) {
|
|
|
@@ -61,16 +63,17 @@ public class VipService {
|
|
|
.typeId(vipTypeId)
|
|
|
.autoTradeEnabled(false)
|
|
|
.startAt(LocalDateTime.now())
|
|
|
- .dailyFrequency(vipType.getDailyFrequency())
|
|
|
- .leftFrequency(vipType.getDailyFrequency())
|
|
|
+ .dailyLimit(vipType.getDailyLimit())
|
|
|
+ .todayUsed(0)
|
|
|
.weight(userVipRepo.minWeight())
|
|
|
.build();
|
|
|
} else if (userVip.isExpired()) {
|
|
|
userVip.setStartAt(LocalDateTime.now());
|
|
|
userVip.setTypeId(vipTypeId);
|
|
|
userVip.setAutoTradeEnabled(false);
|
|
|
- userVip.setDailyFrequency(vipType.getDailyFrequency());
|
|
|
- userVip.setLeftFrequency(vipType.getDailyFrequency());
|
|
|
+ userVip.setDailyLimit(vipType.getDailyLimit());
|
|
|
+ userVip.setTodayUsed(userVip.getExpireAt().toLocalDate().isEqual(LocalDate.now())
|
|
|
+ ? userVip.getTodayUsed() : 0);
|
|
|
userVip.setWeight(userVipRepo.minWeight());
|
|
|
}
|
|
|
BigDecimal totalPrice = vipConfig.getPrice().multiply(BigDecimal.valueOf(num));
|
|
|
@@ -93,13 +96,33 @@ public class VipService {
|
|
|
.build());
|
|
|
}
|
|
|
|
|
|
- @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
|
|
|
- public void scheduleVip() {
|
|
|
- List<Product> list = productService.list(null, 11548L, Arrays.asList(ProductStatus.IN_STOCK), Pageable.ofSize(100))
|
|
|
- .getContent().stream().filter(product -> product.getDelayTo() == null
|
|
|
- || product.getDelayTo().isBefore(LocalDateTime.now()))
|
|
|
+ @Scheduled(fixedRate = 10, timeUnit = TimeUnit.SECONDS)
|
|
|
+ public synchronized void scheduleVip() {
|
|
|
+ log.info("🚚 schedule");
|
|
|
+ List<Product> list = productService.list(null, 11548L, Collections.singletonList(ProductStatus.IN_STOCK),
|
|
|
+ PageRequest.ofSize(100).withSort(Sort.by("modifiedAt").descending()))
|
|
|
+ .getContent().stream()
|
|
|
+ .filter(product -> product.getDelayTo() == null
|
|
|
+ || product.getDelayTo().isBefore(LocalDateTime.now()))
|
|
|
.collect(Collectors.toList());
|
|
|
-
|
|
|
+ if (list.size() <= 10) {
|
|
|
+ log.info("🚚 schedule, no product");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (int i = 10; i < list.size(); i++) {
|
|
|
+ Product product = list.get(i);
|
|
|
+ List<UserVip> vips = userVipRepo.findMatchVip(product.getCurrentPrice(), product.getUserId());
|
|
|
+ if (vips.isEmpty()) {
|
|
|
+ log.info("🚚 no vip match, productId: {}, price: {}", product.getId(), product.getCurrentPrice());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ UserVip chosenVip = vips.get(new Random().nextInt(vips.size()));
|
|
|
+ Future result = vipAsyncService.autoTrade(chosenVip, product);
|
|
|
+ try {
|
|
|
+ result.get();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("🚚 auto trade error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|