|
|
@@ -0,0 +1,79 @@
|
|
|
+package com.izouma.nineth.service.scheduledTask;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.izouma.nineth.domain.Collection;
|
|
|
+import com.izouma.nineth.repo.CollectionRepo;
|
|
|
+import io.jsonwebtoken.lang.Collections;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.Trigger;
|
|
|
+import org.springframework.scheduling.TriggerContext;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
|
+import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
+import org.springframework.scheduling.support.CronTrigger;
|
|
|
+import org.springframework.scheduling.support.PeriodicTrigger;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableScheduling
|
|
|
+public class SubscribedTask implements SchedulingConfigurer {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CollectionRepo collectionRepo;
|
|
|
+
|
|
|
+
|
|
|
+ private List<Collection> collections = new ArrayList();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
|
|
+ taskRegistrar.addTriggerTask(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ LocalDateTime startTime = LocalDateTime.now();
|
|
|
+ LocalDateTime endTime = startTime.plusMinutes(10);
|
|
|
+ List<Collection> collectionList = collectionRepo.selectStartTimeAfter(startTime, endTime);
|
|
|
+ collectionList.forEach(collection -> {
|
|
|
+ if (collection.getStartTime().isBefore(LocalDateTime.now()) && collection.getEndTime().isAfter(LocalDateTime.now())){
|
|
|
+ collectionRepo.setState(collection.getId(),"OPENSUBSCRIBE");
|
|
|
+ }
|
|
|
+ if (collection.getEndTime().isBefore(LocalDateTime.now()) && collection.getPublishTime().isAfter(LocalDateTime.now())){
|
|
|
+ collectionRepo.setState(collection.getId(),"CLOSESUBSCRIBE");
|
|
|
+ }
|
|
|
+ if (collection.getPublishTime().isBefore(LocalDateTime.now()) && collection.getPurchaseTime().isAfter(LocalDateTime.now())){
|
|
|
+ collectionRepo.setState(collection.getId(),"PUBLISH");
|
|
|
+ }
|
|
|
+ if (collection.getPurchaseTime().isBefore(LocalDateTime.now())){
|
|
|
+ collectionRepo.setState(collection.getId(),"PURCHASE");
|
|
|
+ }
|
|
|
+ if (Collections.isEmpty(collections)){
|
|
|
+ collections.remove(0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }, triggerContext -> {
|
|
|
+ LocalDateTime startTime = LocalDateTime.now();
|
|
|
+ LocalDateTime endTime = startTime.plusMinutes(10);
|
|
|
+ List<Collection> collectionList = collectionRepo.selectStartTimeAfter(startTime, endTime);
|
|
|
+ if (Collections.isEmpty(collectionList)) {
|
|
|
+// return new CronTrigger("0 0/10 * * * ?").nextExecutionTime(triggerContext);
|
|
|
+ return Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ }
|
|
|
+ if (Collections.isEmpty(collections)){
|
|
|
+ collections = collectionList.stream().sorted(Comparator.comparing(Collection::getStartTime)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return Date.from(collections.get(0).getStartTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|