package shop.alien.job.store;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shop.alien.entity.result.R;
import shop.alien.job.feign.AlienStoreFeign;

/**
 * 推送任务厂商统计同步。
 * <p>
 * 在 XXL-JOB 管理台配置任务：JobHandler = commonPushTaskStatsSync，Cron 建议 0 0/30 * * * ?（每 30 分钟）。
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CommonPushTaskStatsJob {

    private final AlienStoreFeign alienStoreFeign;

    @XxlJob("commonPushTaskStatsSync")
    public void commonPushTaskStatsSync() {
        log.info("【定时任务】推送统计同步：开始执行");
        XxlJobHelper.log("【定时任务】推送统计同步：开始执行");
        try {
            R<Integer> result = alienStoreFeign.syncPushTaskStatistics();
            int count = (result != null && result.getData() != null) ? result.getData() : 0;
            log.info("【定时任务】推送统计同步：执行完成，成功更新任务数={}", count);
            XxlJobHelper.log("【定时任务】推送统计同步：执行完成，成功更新任务数=" + count);
        } catch (Exception e) {
            log.error("【定时任务】推送统计同步：执行异常", e);
            XxlJobHelper.log("【定时任务】推送统计同步：执行异常 " + e.getMessage());
            throw e;
        }
    }
}
