package com.diagbot.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.entity.SysTaskCron;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.facade.ConsoleFacade;
import com.diagbot.facade.SysTaskCronFacade;
import com.diagbot.facade.data.ADoctorAdviceFacade;
import com.diagbot.util.StringUtil;
import com.diagbot.vo.FilterVO;
import com.diagbot.vo.IndexTimeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class MedIndexTask implements SchedulingConfigurer{

	@Autowired
    private SysTaskCronFacade sysTaskCronFacade;

    private SysTaskCron task001 = new SysTaskCron();

    @Autowired
    private ConsoleFacade consoleFacade;
    
    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                //1.添加任务内容(Runnable)
                if (null != task001
                        && task001.getIsDeleted().equals(IsDeleteEnum.N.getKey())
                        && task001.getIsUsed().equals(1)) {
                    log.info("执行动态定时任务: " + LocalDateTime.now().toLocalTime());
                    IndexTimeVO IndexTimeVO = new IndexTimeVO();
                    String param = task001.getParam();
                    String[] split = {param};
                    if(param.contains(",")){
                        split = param.split(",");
                    }
                    for (String str : split) {
                        IndexTimeVO.setHospitalId(str);
                        consoleFacade.saveMedicaIndicator(IndexTimeVO);
                    }
                }
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                //2.1 从数据库获取执行周期
                task001 = sysTaskCronFacade.getOne(new QueryWrapper<SysTaskCron>()
                        .eq("cron_code", "TASK_INDEX"), false);
                String cron = "0 0 3 * * ?";
                //2.2 合法性校验.
                if (null != task001 && StringUtil.isNotBlank(task001.getCron())) {
                    cron = task001.getCron();
                }
                CronTrigger trigger = new CronTrigger(cron);
                return trigger.nextExecutionTime(triggerContext);
            }
        });
    }
}
