package com.lq.activity.process;
import com.lq.activity.application.ActivityService;
import com.lq.activity.domain.enums.ActivityType;
import com.lq.activity.domain.model.Activity;
import com.lq.activity.infrastructure.repository.ActivityRepository;
import com.lq.activity.utils.CronUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
@Slf4j
@Component
@EnableScheduling
public class LootActivityProcess {
private ThreadPoolTaskScheduler schedulerPool;
@Autowired
public void setSchedulerPool(ThreadPoolTaskScheduler schedulerPool) {
this.schedulerPool = schedulerPool;
}
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
@Autowired
private ActivityService activityService;
@Autowired
private ActivityRepository activityRepo;
private Map> futureMap = new HashMap<>();
@PostConstruct
private void initFutureMap() {
List activityList = activityRepo.findAllByActivityType(ActivityType.loot);
for (Activity activity : activityList) {
addFutureActivity(activity);
}
}
public void addFutureActivity(Activity activity) {
// 根据不同的时候添加不同的任务
Date now = new Date();
Date startTime = activity.getStartTime();
Date endTime = activity.getEndTime();
Date drawPrizeTime = activity.getDrawPrizeTime();
if (now.before(activity.getStartTime())) {
addFuture(activity, drawPrizeTime, LootProcessType.drawLoot);
addFuture(activity, startTime, LootProcessType.startLoot);
addFuture(activity, endTime, LootProcessType.endLoot);
} else if (now.after(startTime) && now.before(endTime)) {
addFuture(activity, drawPrizeTime, LootProcessType.drawLoot);
addFuture(activity, endTime, LootProcessType.endLoot);
} else if (now.after(endTime) && now.before(drawPrizeTime)) {
addFuture(activity, drawPrizeTime, LootProcessType.drawLoot);
}
}
public void addFuture(Activity activity, Date date, String key) {
key = activity.getId() + "-" + key;
stopTask(futureMap.get(key));
futureMap.put(key, getFuture(activity, date, key));
log.info("添加完成,现有:{}个任务", futureMap.size());
}
public ScheduledFuture> getFuture(Activity activity, Date date, String key) {
String cron = CronUtils.generateCron(date);
if (!StringUtils.hasLength(cron)) {
return null;
}
String[] keys = key.split("-");
CronTrigger trigger = new CronTrigger(cron);
if (LootProcessType.drawLoot.equals(keys[1])) {
return schedulerPool.schedule(executeDraw(activity), trigger);
}
return schedulerPool.schedule(executeUpdateLootStatus(activity), trigger});
}
private void stopTask(ScheduledFuture> future) {
if (future != null) {
future.cancel(true);
}
}
private Runnable executeDraw(Activity activity) {
return () -> activityService.generateLootProductWin(activity);
}
private Runnable executeUpdateLootStatus(Activity activity) {
return () -> activityService.updateLootStatusForActivityAndLootProduct(activity);
}
}