|
|
@@ -3,6 +3,7 @@ package com.izouma.nineth.service;
|
|
|
import com.izouma.nineth.annotations.Debounce;
|
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
|
import com.izouma.nineth.config.RedisKeys;
|
|
|
+import com.izouma.nineth.domain.Collection;
|
|
|
import com.izouma.nineth.domain.MintActivity;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.repo.MintActivityRepo;
|
|
|
@@ -13,9 +14,16 @@ import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.scheduling.TaskScheduler;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.concurrent.ScheduledFuture;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -23,11 +31,14 @@ import java.util.concurrent.TimeUnit;
|
|
|
@AllArgsConstructor
|
|
|
public class MintActivityService {
|
|
|
|
|
|
- private MintActivityRepo mintActivityRepo;
|
|
|
- private RedisTemplate<String, Object> redisTemplate;
|
|
|
- private CacheService cacheService;
|
|
|
- private RocketMQTemplate rocketMQTemplate;
|
|
|
- private GeneralProperties generalProperties;
|
|
|
+ private MintActivityRepo mintActivityRepo;
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+ private CacheService cacheService;
|
|
|
+ private RocketMQTemplate rocketMQTemplate;
|
|
|
+ private GeneralProperties generalProperties;
|
|
|
+ private TaskScheduler taskScheduler;
|
|
|
+ private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
|
|
|
+
|
|
|
|
|
|
public Page<MintActivity> all(PageQuery pageQuery) {
|
|
|
return mintActivityRepo.findAll(JpaUtils.toSpecification(pageQuery, MintActivity.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
@@ -74,4 +85,25 @@ public class MintActivityService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void onShelfTask(MintActivity record) {
|
|
|
+ ScheduledFuture<?> task = tasks.get(record.getId());
|
|
|
+ if (task != null) {
|
|
|
+ if (!task.cancel(true)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (record.isScheduleSale()) {
|
|
|
+ if (record.getStartTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
|
|
|
+ Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ ScheduledFuture<?> future = taskScheduler.schedule(() -> {
|
|
|
+ mintActivityRepo.scheduleOnShelf(record.getId());
|
|
|
+ tasks.remove(record.getId());
|
|
|
+ }, date);
|
|
|
+ tasks.put(record.getId(), future);
|
|
|
+ } else {
|
|
|
+ mintActivityRepo.scheduleOnShelf(record.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|