package com.moka.gdtauto.task;

import java.util.List;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.moka.gdtauto.entity.AdAutoConf;
import com.moka.gdtauto.service.AdAutoConfService;
import com.moka.gdtauto.service.AdPlanAutoExecutor;
import com.moka.gdtauto.service.AdPlanCreativeAutoExecutor;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;

@Component
@AllArgsConstructor
@Slf4j
public class AdAutoTask {

    private final AdAutoConfService adAutoConfService;
    private final AdPlanAutoExecutor adPlanAutoExecutor;
    private final AdPlanCreativeAutoExecutor adPlanCreativeAutoExecutor;
    // private final ExecutorService executorService = Executors.newFixedThreadPool(10);

    @Scheduled(cron = "0 0/10 * * * ?")
    @SchedulerLock(name = "ad-plan-auto-task", lockAtMostFor = "9m", lockAtLeastFor = "2m")
    public void adPlanAutoTask() {
        log.info("AdPlanAutoTask execute");
        List<AdAutoConf> confList = adAutoConfService.lambdaQuery().list();
        confList.forEach(conf -> adPlanAutoExecutor.asyncExecute(conf));
    }

    @Scheduled(cron = "30 0/10 * * * ?")
    @SchedulerLock(name = "ad-creative-auto-task", lockAtMostFor = "9m", lockAtLeastFor = "2m")
    public void adCreativeAutoTask() {
        log.info("AdCreativeAutoTask execute");
        List<AdAutoConf> confList = adAutoConfService.lambdaQuery().list();
        confList.forEach(conf -> adPlanCreativeAutoExecutor.asyncExecute(conf));
    }
}
