package com.moka.gdtauto.task;

import java.util.List;

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

import com.moka.gdtauto.common.Constant;
import com.moka.gdtauto.entity.AssetDramaSyncConf;
import com.moka.gdtauto.service.AssetDramaSyncConfService;
import com.moka.gdtauto.service.TencentAssetFictionService;

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

@Slf4j
@Component
@RequiredArgsConstructor
public class SyncDramaFinctionTask {
    private final AssetDramaSyncConfService assetDramaSyncConfService;
    private final TencentAssetFictionService tencentAssetFictionService;
    

    // 每小时
    @Scheduled(cron = "0 0 * * * ?")
    @SchedulerLock(name = "syncDramaAsset", lockAtMostFor = "4m", lockAtLeastFor = "1m")
    public void syncDramaAsset() {
        log.info("开始执行短剧资产同步任务");
        List<AssetDramaSyncConf> confList = assetDramaSyncConfService.list();
        confList.forEach(conf -> {
            String jobNumber = conf.getJobNumber();
            Long accountId = conf.getAccountId();
            try {
                log.info("开始同步短剧资产，jobNumber：{} accountId：{}", jobNumber, accountId);
                int count = tencentAssetFictionService.syncMarketingAssets(jobNumber, Constant.MAIN_ORG_ACCOUNT_ID, accountId);
                log.info("同步短剧资产完成，jobNumber：{} accountId：{} count：{}", jobNumber, accountId, count);
            } catch (Exception e) {
                log.error("同步短剧资产失败", e);
            }
        });
    }
}
