package com.ann.demo.job;

import com.ann.demo.primaryRepository.ScanningFileRepository;
import com.ann.demo.primaryEntity.ScanningFile;
import com.ann.demo.service.AnalysisService;
import com.ann.demo.utils.QuartzUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

/**
 * 取从属表未解析的数据
 *
 * @Author: LeiJiaXin
 * @Date: 2019/8/16 10:11
 */
@DisallowConcurrentExecution
public class ScheduledJob implements Job {

    static final Logger logger = LoggerFactory.getLogger(ScheduledJob.class);
    private static int flag = 0;
    private static int count = 0;
    @Autowired
    private ScanningFileRepository scanningFileRepository;

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private Scheduler scheduler;

    @Value("${start}")
    private int start;

    @Value("${over}")
    private int over;

    @Override
    public void execute(JobExecutionContext context) {
        QuartzUtils.pauseScheduleJob(scheduler, "group1", "job1");
        try {
            if(flag == 0) {
                List<ScanningFile> scanningFiles = scanningFileRepository.findScanningFileList();
                count = scanningFiles.size();
                logger.error("查询出待处理记录数:"+scanningFiles.size()+"个");
                analysisService.handleScanningFile(scanningFiles);
                flag = 1;
            }
            if(count != 0) {
                Thread t1 = new Thread1();
                Thread t2 = new Thread2();
                Thread t3 = new Thread3();
                Thread t4 = new Thread4();
                Thread t5 = new Thread5();
                Thread t6 = new Thread6();
                Thread t7 = new Thread7();
                Thread t8 = new Thread8();
                Thread t9 = new Thread9();
                Thread t10 = new Thread10();
                t1.start();
                t2.start();
                t3.start();
                t4.start();
                t5.start();
               /* t6.start();
                t7.start();
                t8.start();
                t9.start();
                t10.start();*/
               /* Thread t11 = new Thread10();
                t11.start();
                Thread t12 = new Thread10();
                t12.start();
                Thread t13 = new Thread10();
                t13.start();
                Thread t14 = new Thread10();
                t14.start();
                Thread t15 = new Thread10();
                t15.start();*/
            }
        } catch (Exception e) {
            // 如果报错--  捕捉异常 继续执行
            logger.error("执行定时任务出错：{}" ,e.getMessage());
        }
        QuartzUtils.resumeScheduleJob(scheduler, "group1", "job1");
    }
    class Thread1 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread2 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread3 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread4 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread5 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread6 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread7 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread8 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread9 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread10 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread11 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread12 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread13 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread14 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread15 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                     count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
/*
    @Override
    public void execute(JobExecutionContext context) {
        QuartzUtils.pauseScheduleJob(scheduler, "group1", "job1");
        List<ScanningFile> scanningFiles =new ArrayList<ScanningFile>();
        try {
            if(start==0){
                scanningFiles = scanningFileRepository.findScanningFiles(over);
            }else{
                scanningFiles = scanningFileRepository.findScanningFilesMore(over-start,start);
            }
            analysisService.handleScanningFile(scanningFiles);
        } catch (Exception e) {
            // 如果报错--  捕捉异常 继续执行
            logger.error("执行定时任务出错：{}" ,e.getMessage());
        }
        QuartzUtils.resumeScheduleJob(scheduler, "group1", "job1");
    }
*/

}

