/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  javax.annotation.Resource
 *  org.apache.commons.lang.StringUtils
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.context.annotation.Bean
 *  org.springframework.scheduling.Trigger
 *  org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
 *  org.springframework.scheduling.support.CronTrigger
 *  org.springframework.stereotype.Service
 */
package com.hst.task.scuduler;

import com.hst.framework.constants.WebConstants;
import com.hst.task.domain.TaskData;
import com.hst.task.runable.JhsjTsTaskRunable;
import com.hst.task.service.JhsjTsService;
import com.hst.task.support.ScheduledMethodRunnable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

@Service
public class JhsjTsScuduler {
    @Autowired
    private ThreadPoolTaskScheduler jhsjTsTaskScheduler;
    public static Log _logger = LogFactory.getLog(JhsjTsScuduler.class);
    private ScheduledFuture<?> future;
    private JhsjTsService service;

    @Bean
    public ThreadPoolTaskScheduler jhsjTsTaskScheduler() throws InterruptedException {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(20);
        scheduler.setThreadNamePrefix("task-");
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setRejectedExecutionHandler((RejectedExecutionHandler)new ThreadPoolExecutor.CallerRunsPolicy());
        return scheduler;
    }

    public JhsjTsService getService() {
        return this.service;
    }

    @Resource(name="jhsjTsService")
    public void setService(JhsjTsService service) {
        this.service = service;
    }

    public void queryUploadTask() throws Exception {
        this.jhsjTsTaskScheduler.destroy();
        this.jhsjTsTaskScheduler.initialize();
        this.jhsjTsTaskScheduler.getScheduledExecutor().awaitTermination(10L, TimeUnit.SECONDS);
        List<TaskData> dataList = this.getService().queryTaskList("2");
        Iterator<TaskData> it = dataList.iterator();
        while (it.hasNext()) {
            TaskData task = it.next();
            JhsjTsTaskRunable taskRunable = (JhsjTsTaskRunable)WebConstants.WEB_APP_CONTEXT.getBean("jhsjTsTaskRunable");
            ScheduledMethodRunnable runnable = new ScheduledMethodRunnable(taskRunable, "doUploadJhsj", task);
            if ("1".equals(task.getYxzq())) {
                long currentTime = System.currentTimeMillis();
                Date nextTime = new Date(currentTime + 5000L);
                this.future = this.jhsjTsTaskScheduler.schedule((Runnable)runnable, nextTime);
                it.remove();
                continue;
            }
            if (!"3".equals(task.getYxzq())) continue;
            String time = WebConstants.JHXT_XTCS.get("2001");
            if (StringUtils.isBlank((String)time)) {
                time = "30";
            }
            if (StringUtils.isNotBlank((String)task.getJgsj())) {
                time = task.getJgsj();
            }
            String corn = "0/" + time + " * 6-20 * * ?";
            this.future = this.jhsjTsTaskScheduler.schedule((Runnable)runnable, (Trigger)new CronTrigger(corn));
        }
        List<TaskData> downloadTasks = this.getService().queryTaskList("1");
        for (TaskData task : downloadTasks) {
            JhsjTsTaskRunable taskRunable = (JhsjTsTaskRunable)WebConstants.WEB_APP_CONTEXT.getBean("jhsjTsTaskRunable");
            ScheduledMethodRunnable runnable = new ScheduledMethodRunnable(taskRunable, "doDownLoad", task);
            long currentTime = System.currentTimeMillis();
            Date nextTime = new Date(currentTime + 15000L);
            this.future = this.jhsjTsTaskScheduler.schedule((Runnable)runnable, nextTime);
        }
    }

    public void doExecuteSqlScript() throws Exception {
        this.getService().doExecuteSqlScript();
    }

    public void doShutDown() {
        this.jhsjTsTaskScheduler.getScheduledExecutor().shutdownNow();
    }
}
