package com.malk.aipocloud.schedule;

import com.malk.aipocloud.service.ABClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

/**
 * @EnableScheduling 开启定时任务 [配置参考McScheduleTask]
 */
@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnProperty(name = {"spel.scheduling"})
public class ABScheduleTask {

    @Autowired
    private ABClient abClient;

    /**
     * 同步审批单: 查询5min, 避免拒绝\撤销重复推送问题
     */
    @Scheduled(cron = "0 1/5 * * * ?")
    public void timer_1() {
        try {
            // todo utc时间和gmt+8时间，是utc加上8小时就是gmt时间
            LocalDateTime now = LocalDateTime.now().minusHours(8);
            abClient.syncProcess(now.minusMinutes(5), now);
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 同步通讯录: 每小时3分执行, 错开审批单同步
     */
    @Scheduled(cron = "0 3 * * * ?")
    public void timer_2() {
        try {
            abClient.syncContact();
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }
}