|
|
@@ -5,10 +5,7 @@ import com.izouma.awesomeAdmin.domain.*;
|
|
|
import com.izouma.awesomeAdmin.dto.PageQuery;
|
|
|
import com.izouma.awesomeAdmin.enums.ProductStatus;
|
|
|
import com.izouma.awesomeAdmin.exception.BusinessException;
|
|
|
-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.repo.*;
|
|
|
import com.izouma.awesomeAdmin.utils.Translator;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -25,6 +22,7 @@ import javax.transaction.Transactional;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.Future;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -49,6 +47,7 @@ public class VipService {
|
|
|
private VipAsyncService vipAsyncService;
|
|
|
private RedissonClient redissonClient;
|
|
|
private SysConfigService sysConfigService;
|
|
|
+ private UserRepo userRepo;
|
|
|
|
|
|
@Transactional
|
|
|
public void purchaseVip(Long userId, Long vipTypeId, Long vipConfigId, int num) {
|
|
|
@@ -140,7 +139,7 @@ public class VipService {
|
|
|
try {
|
|
|
log.info("🚚 schedule");
|
|
|
List<Product> list = productService.list(null, 11548L, Collections.singletonList(ProductStatus.IN_STOCK),
|
|
|
- PageRequest.ofSize(100).withSort(Sort.by("modifiedAt").descending()))
|
|
|
+ PageRequest.ofSize(100).withSort(Sort.by("modifiedAt").descending()), null)
|
|
|
.getContent().stream()
|
|
|
.filter(product -> product.getDelayTo() == null
|
|
|
|| product.getDelayTo().isBefore(LocalDateTime.now()))
|
|
|
@@ -149,6 +148,11 @@ public class VipService {
|
|
|
log.info("🚚 schedule, no product");
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ BigDecimal period = sysConfigService.getBigDecimal("freshness_period_for_new");
|
|
|
+ BigDecimal newUserMax = sysConfigService.getBigDecimal("new_user_max");
|
|
|
+ BigDecimal oldUserMin = sysConfigService.getBigDecimal("old_user_min");
|
|
|
+
|
|
|
for (int i = 10; i < list.size(); i++) {
|
|
|
Product product = list.get(i);
|
|
|
List<UserVip> vips = userVipRepo.findMatchVip(product.getCurrentPrice(), product.getUserId());
|
|
|
@@ -156,8 +160,22 @@ public class VipService {
|
|
|
continue;
|
|
|
}
|
|
|
UserVip chosenVip = vips.get(new Random().nextInt(vips.size()));
|
|
|
+
|
|
|
+ User user = userRepo.findById(chosenVip.getUserId()).orElseThrow(new BusinessException(Translator.toLocale("user.not_found")));
|
|
|
+ BigDecimal intervals = new BigDecimal(ChronoUnit.DAYS.between(user.getCreatedAt(), LocalDateTime.now()));
|
|
|
+
|
|
|
try {
|
|
|
- vipAsyncService.autoTrade(chosenVip, product);
|
|
|
+ if (intervals.compareTo(period) < 1) {
|
|
|
+ //new
|
|
|
+ if (product.getCurrentPrice().compareTo(newUserMax) < 1) {
|
|
|
+ vipAsyncService.autoTrade(chosenVip, product);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //old
|
|
|
+ if (oldUserMin.compareTo(product.getCurrentPrice()) < 1) {
|
|
|
+ vipAsyncService.autoTrade(chosenVip, product);
|
|
|
+ }
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
log.error("🚚 auto trade error", e);
|
|
|
}
|