package com.ruoyi.benyun.config;


import com.ruoyi.benyun.job.AnalyseJob;
import com.ruoyi.benyun.job.BrandStatsiticsJob;
import com.ruoyi.benyun.job.WriteRedisJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.ZoneId;
import java.util.TimeZone;

/*
 * 定时器：分析网点数据
 * */
@Slf4j
@Configuration
public class QuartzConfig {

    /**
     * 给网点打标签
     * @return
     */
    @Bean
    public JobDetail AnalyseJobDetail() {
        log.info("AnalyseJobDetail");

        return JobBuilder.newJob(AnalyseJob.class) //newJob方法就是在绑定要运行的Job接口实现。
            .withIdentity("analyseJobDetail") //给当前JobDetail对象取名字
            .storeDurably() //及时没有触发器绑定当前JobDetail对象，也不会被删除
            .build();
    }


    @Bean
    public Trigger AnalyseTrigger() {
        //定义Cron表达式
        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 23 26 * ? ");
        cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        return TriggerBuilder
            .newTrigger()
            .forJob(AnalyseJobDetail()) //绑定要运行的JobDetail对象,这是得到的是Spring容器里的JobDetail
            .withIdentity("analyseTrigger") //给当前Trigger对象起名字，名字任意
            .withSchedule(cron) //绑定cron表达式
            .build();
    }

    /**
     * 将网点载入redis中
     * @return
     */
    @Bean
    public JobDetail WriteRedisJobDetail() {
        log.info("WriteRedisJobDetail");

        return JobBuilder.newJob(WriteRedisJob.class) //newJob方法就是在绑定要运行的Job接口实现。
            .withIdentity("writeRedisJobDetail") //给当前JobDetail对象取名字
            .storeDurably() //及时没有触发器绑定当前JobDetail对象，也不会被删除
            .build();
    }



    @Bean
    public Trigger WriteRedisTrigger(){
        //定义Cron表达式
        CronScheduleBuilder cron  = CronScheduleBuilder.cronSchedule("0 0 1 * * ? ");

        cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        return TriggerBuilder
            .newTrigger()
            .forJob(WriteRedisJobDetail()) //绑定要运行的JobDetail对象,这是得到的是Spring容器里的JobDetail
            .withIdentity("writeRedisTrigger") //给当前Trigger对象起名字，名字任意
            .withSchedule(cron) //绑定cron表达式
            .build();
    }

    /**
     * 品牌分析
     * */
    @Bean
    public JobDetail BrandStatsiticsJobDetail() {
        log.info("BrandStatsiticsJobDetail");

        return JobBuilder.newJob(BrandStatsiticsJob.class) //newJob方法就是在绑定要运行的Job接口实现。
            .withIdentity("BrandStatsiticsJobDetail") //给当前JobDetail对象取名字
            .storeDurably() //及时没有触发器绑定当前JobDetail对象，也不会被删除
            .build();
    }

    //触发器声明，设置job的运行时机，并放入Spring容器中
    @Bean
    public Trigger BrandStatsiticsTrigger() {
        //定义Cron表达式
        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 23 30 * ? ");
        //CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 30 11 16 11 ? ");
        cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        return TriggerBuilder
            .newTrigger()
            .forJob(BrandStatsiticsJobDetail()) //绑定要运行的JobDetail对象,这是得到的是Spring容器里的JobDetail
            .withIdentity("BrandStatsiticsTrigger") //给当前Trigger对象起名字，名字任意
            .withSchedule(cron) //绑定cron表达式
            .build();
    }

}
