package com.srs.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.comprehensive.domain.Dto.CphStuScoreMiddleDto;
import com.srs.comprehensive.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component("CphTask")
public class CphTask {
    @Autowired
    ICphTestForService cphTestForService;
    @Autowired
    ICphStuScoreMiddlesService cphStuScoreMiddlesService;

    @Autowired
    CphSqlServerDataService sqlServerDataService;

    //学生
    @Autowired
    ISrsStudentService iSrsStudentService;

    //班级
    @Autowired
    ISrsClassService iSrsClassService;

    //专业
    @Autowired
    ISrsMajorsService iSrsMajorsService;

    //年级
    @Autowired
    ISrsGradeService iSrsGradeService;

    //辅导员
    @Autowired
    ICphTeacherService iCphTeacherService;


    /**
     * 更新成绩
     * @param stuYearId
     */
    public void totalScoreWrite(int stuYearId) {
        cphTestForService.testMethod(stuYearId);
    }

    /**
     * 同步学生成绩
     */
    public void stuScoreMiddleSync(){
        cphStuScoreMiddlesService.emptyTableDate();
        int pageNum=1;
        int pageSize=3000;
        int studentInfoNumber = cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(null);//总数
        int sum=studentInfoNumber/ pageSize;//需要循环的次数
        int sumS=studentInfoNumber%pageSize>0? sum + 1: sum;
        QueryWrapper<CphStuScoreMiddleDto> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByDesc("stu_no", "kcdm");
        ExecutorService executor = Executors.newFixedThreadPool(15);
        for (pageNum=1; pageNum <= sumS; pageNum++){
            final int currentPage = pageNum;
            executor.execute(() -> {
                List<Map<String, Object>> cphStuScoreMiddles = cphStuScoreMiddlesService
                        .serectCphStuScoreMiddlesXh(currentPage, pageSize, null);
                cphStuScoreMiddlesService.selectCphStuScoreMiddleEmptyAndAdd(cphStuScoreMiddles);
                cphStuScoreMiddles.clear();
            });
        }
        executor.shutdown();
        //cphStuScoreMiddlesService.syncCphStuScoreMiddles();
    }

    /**
     * 同步年级
     */
    public void synchronousGradeInfoList(){
        System.out.println("启动 同步年级");
        List<Map> list=sqlServerDataService.getGradeInfoList();
        iSrsGradeService.sqlserverSynchronousMYSQL(list);//同步年级
    }
    /**
     * 同步学生
     */
    public void synchronousStudent(){
        int pageNum=1;
        int pageSize=3000;
        int studentInfoNumber = sqlServerDataService.getStudentInfoNumber();//总数
        int sum=studentInfoNumber/ pageSize;//需要循环的次数
        int sumS=studentInfoNumber%pageSize>0? sum + 1: sum;
        List<Map> stuNoList = new ArrayList<>();
        for (pageNum=1; pageNum <= sumS; pageNum++){
            List<Map> list=sqlServerDataService.getStudentInfoList(pageNum,pageSize,null);
            List<Map> maps = iSrsStudentService.sqlserverSynchronousMYSQL(list);//同步到mysql
            stuNoList.addAll(maps);
            list.clear();
        }
        iSrsStudentService.addStudentData(stuNoList);//添加到学生表
        iSrsStudentService.Usertb();//同步到用户表
    }

    /**
     *
     * 同步专业
     */
    public void synchronousSpecialtyInfoList(){
        List<Map> list=sqlServerDataService.getSpecialtyInfoList();
        iSrsMajorsService.sqlserverSynchronousMYSQL(list);//同步专业
    }

    /**
     * 同步辅导员
     */
    public void synchronousEmployEEInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getEmployEEInfoList();
        iCphTeacherService.sqlserverSynchronousMYSQL(list);//同步辅导员
        iCphTeacherService.Usertb();
    }

    /**
     * 同步班级
     */
    public void synchronousClass(){
        List<Map> list=sqlServerDataService.getClassInfoList();
        iSrsClassService.sqlserverSynchronousMYSQL(list);//同步班级
    }
}
