package com.malk.junengtai.schedule;

import com.fasterxml.jackson.core.JacksonException;
import com.malk.junengtai.service.PurchaseService;
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.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @EnableScheduling 开启定时任务 [配置参考McScheduleTask]
 */
@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnProperty(name = {"enable.scheduling"})
public class Schedule {
    @Autowired
    private PurchaseService purchaseService;

    /*组件信息定时器*/
    @Scheduled(cron = "0 0 22 * * ?")
    public void purchaseList(){
        log.info("开始同步组件信息");
        try {
            LocalDate today = LocalDate.now(); // 获取当前日期（基于系统默认时区）
            String dateString = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            System.out.println("当前时间：" + dateString);
            purchaseService.ComponentInformation(dateString);
            System.out.println("同步组件信息完成");
        } catch (JacksonException e) {
            throw new RuntimeException(e);
        }
    }

    /*供应商信息定时器*/
    @Scheduled(cron = "0 0 23 * * ?")
    public void supplierInformation(){
        log.info("开始同步供应商信息");
        try {
            LocalDate today = LocalDate.now(); // 获取当前日期（基于系统默认时区）
            String dateString1 = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            System.out.println("当前时间：" + dateString1);
            purchaseService.supplierInformation(dateString1);
            System.out.println("同步供应商信息完成");
        } catch (JacksonException e) {
            throw new RuntimeException(e);
        }
    }




}
