package com.srs.web.controller.comprehensive;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.annotation.Anonymous;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.page.TableDataInfo;
import com.srs.comprehensive.domain.CphStuScoreMiddle;
import com.srs.comprehensive.domain.Dto.CphStuScoreMiddleDto;
import com.srs.comprehensive.domain.Vo.CphStuScoreMiddleDtoVo;
import com.srs.comprehensive.service.ICphStuScoreMiddlesService;
import com.srs.comprehensive.config.SyncConfig;
import com.sun.net.httpserver.Authenticator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static com.srs.common.core.domain.AjaxResult.success;

/**
 * mssql查询添加
 */

@RestController
@RequestMapping("/sqlserver/cphStuScoreMiddle")
public class CphStuScoreMiddleController extends BaseController {

    @Autowired
    public ICphStuScoreMiddlesService cphStuScoreMiddlesService;
    
    @Autowired
    private SyncConfig syncConfig;

    //同步学生成绩
    @GetMapping("/selectAllw")
    public AjaxResult selectCphStuScoreMiddle(){
        int pageNum=1;
        int pageSize=10;
        int studentInfoNumber = cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(null);//总数
        int sum=studentInfoNumber/ pageSize;//需要循环的次数
        int sumS=studentInfoNumber%pageSize>0? sum + 1: sum;
        List<Map<String,Object>> stuNoList = new ArrayList<>();
        for (pageNum=1; pageNum <= sumS; pageNum++){
            List<Map<String,Object>> cphStuScoreMiddles = cphStuScoreMiddlesService
                    .serectCphStuScoreMiddlesXh(pageNum,pageSize,null);
            //cphStuScoreMiddlesService.typeConversion(cphStuScoreMiddles);
            List<Map<String, Object>> maps = cphStuScoreMiddlesService.insertCphStuScoreMiddlesDel(cphStuScoreMiddles);
            stuNoList.addAll(maps);
            cphStuScoreMiddles.clear();
        }
        cphStuScoreMiddlesService.selectCphStuScoreMiddleEmptyAndAdd(stuNoList);
        return success();
    }

    //同步学生成绩，清空在添加
    @GetMapping("/selectAll")
    public AjaxResult selectCphStuScoreMiddleEmptyAndAdd(){
        try {
            logger.info("开始同步学生成绩数据...");
            long startTime = System.currentTimeMillis();
            
            // 清空现有数据
            cphStuScoreMiddlesService.emptyTableDate();
            logger.info("已清空现有数据");
            
            // 配置参数
            int pageSize = syncConfig.getPageSize();
            int threadPoolSize = syncConfig.getWebThreadPoolSize();
            
            // 获取总数
            int studentInfoNumber = cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(null);
            int totalPages = (int) Math.ceil((double) studentInfoNumber / pageSize);
            
            logger.info("总记录数: {}, 总页数: {}, 每页大小: {}", studentInfoNumber, totalPages, pageSize);
            
            // 创建线程池
            ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
            CountDownLatch latch = new CountDownLatch(totalPages);
            AtomicInteger successCount = new AtomicInteger(0);
            AtomicInteger errorCount = new AtomicInteger(0);
            
            // 提交任务
            for (int pageNum = 1; pageNum <= totalPages; pageNum++) {
                final int currentPage = pageNum;
                executor.submit(() -> {
                    try {
                        // 分页查询数据
                        List<Map<String, Object>> cphStuScoreMiddles = cphStuScoreMiddlesService
                                .serectCphStuScoreMiddlesXh(currentPage, pageSize, null);
                        
                        if (cphStuScoreMiddles != null && !cphStuScoreMiddles.isEmpty()) {
                            // 处理数据
                            cphStuScoreMiddlesService.selectCphStuScoreMiddleEmptyAndAdd(cphStuScoreMiddles);
                            successCount.incrementAndGet();
                            logger.debug("第{}页处理完成，记录数: {}", currentPage, cphStuScoreMiddles.size());
                        }
                        
                    } catch (Exception e) {
                        errorCount.incrementAndGet();
                        logger.error("处理第{}页时发生错误: {}", currentPage, e.getMessage(), e);
                    } finally {
                        latch.countDown();
                    }
                });
            }
            
            // 等待所有任务完成
            boolean completed = latch.await(syncConfig.getTimeoutMinutes(), TimeUnit.MINUTES);
            
            if (!completed) {
                logger.warn("同步任务未在指定时间内完成");
                return AjaxResult.error("同步任务超时");
            }
            
            // 关闭线程池
            executor.shutdown();
            
            long endTime = System.currentTimeMillis();
            long duration = endTime - startTime;
            
            logger.info("学生成绩同步完成 - 总耗时: {}ms, 成功页数: {}, 失败页数: {}", 
                    duration, successCount.get(), errorCount.get());
            
            if (errorCount.get() > 0) {
                return AjaxResult.warn("同步完成，但有部分数据失败。成功: " + successCount.get() + 
                        "，失败: " + errorCount.get());
            }
            
            return AjaxResult.success("同步完成，共处理 " + totalPages + " 页数据，耗时 " + duration + "ms");
            
        } catch (Exception e) {
            logger.error("同步学生成绩数据时发生严重错误", e);
            return AjaxResult.error("同步失败: " + e.getMessage());
        }
    }

    //查询sqlserver学生成绩
    @GetMapping("/getAll")
    public AjaxResult getCphStuScoreMiddle(@RequestParam("pageNum")int page,
                                           @RequestParam("pageSize") int pageSize,
                                           @RequestParam(value = "stuNo", required = false) String stuNo){
        List<Map<String,Object>> cphStuScoreMiddles = cphStuScoreMiddlesService.serectCphStuScoreMiddlesXh(page,pageSize,stuNo);
        return success(cphStuScoreMiddles).put("total",cphStuScoreMiddlesService.serectCphStuScoreMiddleCount(stuNo));
    }

    //查询MySQL学生成绩
    @GetMapping("/getMysqlStuScoreCphStuScoreMiddle")
    public TableDataInfo getCphStuScoreMiddleS(@RequestParam(value = "stuNo", required = false) String stuNo){
        startPage();
        List<CphStuScoreMiddleDtoVo> cphStuScoreMiddles = cphStuScoreMiddlesService.insertCphStuScoreMiddlesDelS(stuNo);
        return getDataTable(cphStuScoreMiddles);
    }
}
