package com.datacenter.job;

import com.datacenter.service.StatPlatformDailyPnlService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 平台每日盈亏统计定时任务。
 * 每天凌晨 1:05 计算昨日数据（等待当天最后的结算任务完成后再跑）。
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformDailyPnlJob {

    private final StatPlatformDailyPnlService statPlatformDailyPnlService;

    @Scheduled(cron = "0 5 1 * * *")
    public void run() {
        LocalDate yesterday = LocalDate.now().minusDays(1);
        log.info("[PlatformDailyPnlJob] 开始统计昨日平台盈亏 date={}", yesterday);
        try {
            statPlatformDailyPnlService.computeAndSaveBoth(yesterday);
            log.info("[PlatformDailyPnlJob] 统计完成 date={}", yesterday);
        } catch (Exception e) {
            log.error("[PlatformDailyPnlJob] 统计失败 date={}", yesterday, e);
        }
    }
}
