package com.usky.vpp.job;

import com.usky.vpp.service.VppSettlementSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 每月 1 号 00:35 同步上月电量核算与电费账单（电费金额为 0）
 */
@Component
public class VppSettlementMonthlyScheduler {

    private static final Logger log = LoggerFactory.getLogger(VppSettlementMonthlyScheduler.class);

    @Value("${vpp.settlement.monthly-sync-enabled:true}")
    private boolean enabled;

    @Autowired
    private VppSettlementSyncService settlementSyncService;

    @Scheduled(cron = "${vpp.settlement.monthly-sync-cron:0 35 0 1 * ?}")
    public void syncLastMonthSettlement() {
        if (!enabled) {
            return;
        }
        try {
            int success = settlementSyncService.syncLastMonth();
            log.info("月度电量/电费同步任务完成, successSites={}", success);
        } catch (Exception ex) {
            log.error("月度电量/电费同步任务失败: {}", ex.getMessage(), ex);
        }
    }
}
