package com.weather.config;

import com.weather.common.job.PhoneCacheMeiHaoJob;
import com.weather.common.job.PhoneCacheXMGJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : fengyuhao
 * @desc : 定时任务配置类
 * @update : 修改说明
 * @date : 2020/12/28-上午11:31
 */
@Configuration
public class QuartzConfig {

    private static final String JOB_GROUP_NAME = "PJB_JOBGROUP_NAME";
    private static final String TRIGGER_GROUP_NAME = "PJB_TRIGGERGROUP_NAME";



    // =================================================================================================================
    /**
     * 定时任务2：
     * 同步用户信息Job（任务详情）
     */
    @Bean
    public JobDetail phoneCacheXMGJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(PhoneCacheXMGJob.class)
                .withIdentity("syncUserJobDetailxmg", JOB_GROUP_NAME)
                //设置参数（键值对）
                .usingJobData("userName", "参数值")
                //即使没有Trigger关联时，也不需要删除该JobDetail
                .storeDurably()
                .build();
        return jobDetail;
    }
    /**
     * 定时任务2：
     * 同步用户信息Job（触发器）
     */
    @Bean
    public Trigger phoneCacheXMFJobTrigger() {
        //每隔5秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                //关联上述的JobDetail
                .forJob(phoneCacheXMGJobDetail())
                //给Trigger起个名字
                .withIdentity("syncUserxmgJobTrigger", TRIGGER_GROUP_NAME)
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }
    // =================================================================================================================
    /**
     * 定时任务1：
     * 同步用户信息Job（任务详情）
     */
    @Bean
    public JobDetail phoneCacheMeiHaoJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(PhoneCacheMeiHaoJob.class)
                .withIdentity("syncUserJobDetailmeihao", JOB_GROUP_NAME)
                //设置参数（键值对）
                .usingJobData("userName", "参数值")
                //即使没有Trigger关联时，也不需要删除该JobDetail
                .storeDurably()
                .build();
        return jobDetail;
    }
    /**
     * 定时任务1：
     * 同步用户信息Job（触发器）
     */
    @Bean
    public Trigger phoneCacheMeiHaoJobTrigger() {
        //每隔5秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                //关联上述的JobDetail
                .forJob(phoneCacheMeiHaoJobDetail())
                //给Trigger起个名字
                .withIdentity("syncUsermeihaoJobTrigger", TRIGGER_GROUP_NAME)
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }
}
