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.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 static com.srs.common.core.domain.AjaxResult.success;

/**
 * mssql查询添加
 */

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

    @Autowired
    public ICphStuScoreMiddlesService cphStuScoreMiddlesService;

    //同步学生成绩
    @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")//16：03
    public AjaxResult selectCphStuScoreMiddleEmptyAndAdd(){
        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();
        return success();
    }

    //查询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);
    }
}
