package cn.com.capcloud.core.quartz.task;

import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseDao;
import cn.com.capcloud.core.cache.BaseConfigStaticCache;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;

@Component
@Order(value = 1)
public class TaskRunner implements ApplicationRunner {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TaskRunner.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private TaskService scheduledTaskService;
    @Autowired
    private KHCRedisCache khcRedisCache;
    @Value("${rzth.isRunTask}")
    private String isRunTask;

    /**
     * 程序启动完毕后,需要自启的任务
     */
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        if(isRunTask.equals("Y")){
            logger.info(" >>>>>> 项目启动完毕, 开启 => 需要自启的任务 开始! ");
            String whereSql=" where status=0 and task_start_run='Y' and task_status='Y'";
            List<Map<String, Object>> scheduledTaskBeanList = BaseDao.queryNotPage("tbl_data_task",  whereSql,jdbcTemplate);
            BaseConfigStaticCache.init(khcRedisCache);
            scheduledTaskService.initAllTask(scheduledTaskBeanList);
            logger.info(" >>>>>> 项目启动完毕, 开启 => 需要自启的任务 结束！");
        }else{
            logger.info(" >>>>>> 项目启动完毕！(本应用不参与计划任务执行)");
        }
    }
}

