package com.xiang.xservice.application.script.fwd.schedule;

import com.xiang.xservice.application.script.fwd.service.IPerformServiceHttp;
import com.xiang.xservice.application.script.fwd.utils.TimeSyncUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author: xiang
 * @Date: 2025-09-30 14:56
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class FwdTimeSyncJob {

    private final IPerformServiceHttp performServiceHttp;
    @Scheduled(fixedRate = 60_000) // 每分钟执行一次
    public void syncTime() {
        try {
            // 调用对方接口
            Long timeStamp = performServiceHttp.getServerTimeStamp();
            if (Objects.equals(timeStamp,-1L)) {
                log.info("请求服务器时间失败!");
                return;
            }
            TimeSyncUtils.syncWithServer(timeStamp);
        } catch (Exception e) {
            log.error("同步时间失败", e);
        }
    }
}
