package com.malk.kuaikeli.schedule;

import com.malk.kuaikeli.service.KKLService;
import com.malk.kuaikeli.service.SupplierMaterialService;
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;

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

//    @Autowired
//    private KKLService kklService;

    @Autowired
    private SupplierMaterialService supplierMaterialService;

    /**
     * 周一凌晨1点同步 (即周日后生效)
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void syncDingTalkFailedList() {
        log.info("开始同步原材料下周单价到本周");
        try {
//            kklService.syncPrice();
            supplierMaterialService.syncPrice();
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 每10分钟同步
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void sync() {
        log.info("开始同步供应商原材料");
        try {
            supplierMaterialService.sync();
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }
}