package com.moka.gdtauto.task;

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

import com.moka.gdtauto.service.AdPlanCreativeService;
import com.moka.gdtauto.service.AdPlanService;

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

/**
 * 同步广告计划 system_status 定时任务
 * <p>
 * 流程：
 * 1. 分页查询 ad_plan 表（status=2 执行中，ID 降序）
 * 2. 按 accountId 分组，每组最多 100 条（腾讯广告 IN 过滤上限）
 * 3. 调用 adgroups/get 接口批量获取 system_status
 * 4. 批量更新回 ad_plan 表
 * </p>
 *
 * @author moka
 * @since 2026-03-05
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SyncAdgroupSystemStatusTask {

    private final AdPlanService adPlanService;
    private final AdPlanCreativeService adPlanCreativeService;

    /**
     * 每小时同步一次广告状态
     */
    @Scheduled(cron = "0 0 * * * *")
    @SchedulerLock(name = "syncAdgroupSystemStatus", lockAtMostFor = "9m", lockAtLeastFor = "1m")
    public void syncAdgroupSystemStatus() {
        adPlanService.syncAdPlanStatus(null);
    }

    /**
     * 同步广告计划创意 approval_status 定时任务
     */
    @Scheduled(cron = "0 0 * * * *")
    @SchedulerLock(name = "syncAdgroupCreativeStatus", lockAtMostFor = "9m", lockAtLeastFor = "1m")
    public void syncAdgroupCreativeStatus() {
        adPlanCreativeService.syncAdPlanCreativeStatus(null);
    }

}
