package com.younike.admin.task;

import com.younike.common.quartz.job.BaseJob;
import com.younike.service.pojo.TaskLog;
import com.younike.service.service.ITaskLogService;
import lombok.extern.log4j.Log4j2;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * 关闭保证金不足100的店铺
 * 2021-7-23 Another
 */
@Component
@Log4j2
@DisallowConcurrentExecution
public class TestTask implements BaseJob {

    @Autowired
    private ITaskLogService taskLogService;

    @Value("${job.start}")
    private boolean startJob;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("--------- 测试1分钟一次 -----------------");

        if(!startJob){
            return;
        }

        TaskLog taskLog = new TaskLog();
        taskLog.setTaskFileName("TestTask");
        taskLog.setTaskName("测试1分钟一次");
        taskLog.setExecutionTime(new Date());
        try {
            // 查询 保证金不足100，审核状态是审核成功，并且营业状态是 营业中 或 休息中 的商家id
           /* List<String> shopIdList = shopService.findShopIdByBondMoney();
            if (CollectionUtils.isNotEmpty(shopIdList)) {
                shopService.updateBusinessState(shopIdList);
            }*/

            taskLog.setIsSuccess(1);
        } catch (Exception e){
            taskLog.setIsSuccess(0);
        } finally {
            int insert = taskLogService.insert(taskLog);
            if (insert != 1) {
                log.error("保存定时任务日志失败");
            }
        }

    }

    /*public static void main(String[] args) throws SchedulerException, InterruptedException {
        // 1. 创建调度器工厂
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        // 2. 由调度器工厂创建调度器
        Scheduler scheduler = schedulerFactory.getScheduler();

        // 3. 创建 TaskDemo 实例（定时任务执行内容）
        JobDetail jobDetail = JobBuilder.newJob(CloseShopTask.class)
                .withIdentity("job1", "group1").build();

        // 4. 创建触发器对象（Trigger实例），每隔1s执行一次
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "triggerGroup1")
                .startNow()    // 立即生效
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 1 * * ?"))    // 每天凌晨1点执行一次
                .build();    // 一直执行

        // 5. 执行
        scheduler.scheduleJob(jobDetail, trigger);
        System.out.println("--------scheduler start ! ------------");
        scheduler.start();

        // 6. 结束
        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();
        System.out.println("--------scheduler shutdown ! ------------");

    }*/

}
