package com.malk.ruisi.controller;

import cn.hutool.core.io.FileUtil;
import com.malk.ruisi.service.dingshiqi;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnProperty(name = {"enable.scheduling"})
public class ScheduleTask {
    @Autowired
    private dingshiqi ds;
    @Value("${qiyuesuo.pdfbdurl}")
    private String PDFBDURL;
    /**
     * 每天查询业务ID
     */
//    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncRecruitSource() {
        try {
           System.out.println("开始上传业务ID");
            System.out.println(LocalDateTime.now());
            ds.getYWFLid( );
            System.out.println("结束业务ID");
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 每天查询物理印章ID
     */
//    @Scheduled(cron = "0/1 * * * * ?")
//    @Scheduled(cron = "0 09 22 * * ?")
//    @Scheduled(cron = "0 0/10 * * * ?")
    public void syncRecruitSource1() {
        try {
            System.out.println("开始上传物理印章ID");
            System.out.println(LocalDateTime.now());
            ds.getYWFLid1( );
            System.out.println("结束物理印章ID");
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 每天查询电子印章ID
//     */
//    @Scheduled(cron = "0/1 * * * * ?")
//    @Scheduled(cron = "0 01 23 * * ?")
    public void syncRecruitSource2() {
        try {
            System.out.println("开始上传电子印章ID");
            System.out.println(LocalDateTime.now());
            ds.getYWFLid2( );
            System.out.println("结束电子印章ID");
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

//    @Scheduled(cron = "0 1 0 * * ?")
//    @Scheduled(cron = "0 0 * * 0")
    public void delTempFile() {
        log.info("开始删除文件");
        File directory = new File(PDFBDURL);
        try {
            FileUtils.cleanDirectory(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//        @Scheduled(cron = "0/1 * * * * ?")
    public void syncDelete() {

        log.info("开始删除数据");

        try {
             ds.syncDelete();
            System.out.println(LocalDateTime.now());
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }
}
