package com.ann.demo.job;

import com.ann.demo.primaryEntity.ScanningFile;
import com.ann.demo.primaryRepository.ScanningFileRepository;
import com.ann.demo.service.AnalysisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2021/12/22 0:07
 * @UpdateUser: 曾文和
 * @UpdateDate: 2021/12/22 0:07
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Component
public class DataRunner implements ApplicationRunner {
    static final Logger logger = LoggerFactory.getLogger(DataRunner.class);
    private static int flag = 0;
    private static int count = 0;
    @Autowired
    private ScanningFileRepository scanningFileRepository;

    @Autowired
    private AnalysisService analysisService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        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();
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
            t6.start();
            t7.start();
            t8.start();
            t9.start();
        }
    }

    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();
                }
            }
        }
    }
}
