package com.srs.web.controller.comprehensive;

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.domain.entity.SysUser;
import com.srs.common.core.page.TableDataInfo;
import com.srs.comprehensive.service.*;
import liquibase.pro.packaged.A;
import liquibase.pro.packaged.l;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sqlserverdata")
public class CphSqlServerDataController extends BaseController {
    @Autowired
    CphSqlServerDataService sqlServerDataService;

    //学生
    @Autowired
    ISrsStudentService iSrsStudentService;

    //班级
    @Autowired
    ISrsClassService iSrsClassService;

    //专业
    @Autowired
    ISrsMajorsService iSrsMajorsService;

    //年级
    @Autowired
    ISrsGradeService iSrsGradeService;

    //辅导员
    @Autowired
    ICphTeacherService iCphTeacherService;


    @RequestMapping("/getUserInfo")
    public AjaxResult getStudentInfoList(@RequestParam("pageNum")int page,
                                         @RequestParam("pageSize") int pageSize,
                                         @RequestParam(value = "stuNo", required = false) String stuNo){
        //startPage();
        int studentInfoNumber = sqlServerDataService.getStudentInfoNumber();
        List<Map> list=sqlServerDataService.getStudentInfoList(page,pageSize,stuNo);
        return success(list).put("total", studentInfoNumber);
    }

    //同步学生
    @RequestMapping("/synchronousStudent")
    public AjaxResult synchronousStudent(){
        int pageNum=1;
        int pageSize=500;
        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();//同步到用户表
        return success();
    }

    //班级
    @RequestMapping("/classInfoList")
    public TableDataInfo classInfoList(){
        startPage();
        List<Map> list=sqlServerDataService.getClassInfoList();
        return getDataTable(list);
    }

    //班级代码查询
    @GetMapping(value = "/getClassInfodm/{bjdm}")
    public AjaxResult getClassInfodm(@PathVariable("bjdm") String bjdm){
        return success(sqlServerDataService.getClassInfodm(bjdm));
    }

    //同步班级
    @RequestMapping("/synchronousClass")
    public AjaxResult synchronousClass(){
        //startPage();
        List<Map> list=sqlServerDataService.getClassInfoList();
//        String gets = sqlServerDataService.gets(40L);
        iSrsClassService.sqlserverSynchronousMYSQL(list);//同步班级
        return success(list);
    }

    //专业
    @RequestMapping("/getSpecialtyInfoList")
    public TableDataInfo getSpecialtyInfoList(){
        startPage();
        List<Map> list=sqlServerDataService.getSpecialtyInfoList();
        return getDataTable(list);
    }
    //通过专业代码查询
    @GetMapping(value = "/getSpecialtyzydm/{zydm}")
    public AjaxResult getSpecialtyzydm(@PathVariable("zydm") String zydm){
        return success(sqlServerDataService.getSpecialtyzydm(zydm));
    }

    //同步专业
    @RequestMapping("/synchronousSpecialtyInfoList")
    public AjaxResult synchronousSpecialtyInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getSpecialtyInfoList();
        iSrsMajorsService.sqlserverSynchronousMYSQL(list);//同步专业
        return success(list);
    }
    //年级
    @RequestMapping("/getGradeInfoList")
    public AjaxResult getGradeInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getGradeInfoList();
        return success(list);
    }
    //同步年级
    @RequestMapping("/synchronousGradeInfoList")
    public AjaxResult synchronousGradeInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getGradeInfoList();
        iSrsGradeService.sqlserverSynchronousMYSQL(list);//同步年级
        return success(list);
    }

    //班级邦辅导员
    @RequestMapping("/getBZRClassInfoList")
    public AjaxResult getBZRClassInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getBZRClassInfoList();
        return success(list);
    }
    //教职工
    @RequestMapping("/getEmployEEInfoList")
    public AjaxResult getEmployEEInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getEmployEEInfoList();
        return success(list);
    }

    //同步辅导员
    @RequestMapping("/synchronousEmployEEInfoList")
    public AjaxResult synchronousEmployEEInfoList(){
        //startPage();
        List<Map> list=sqlServerDataService.getEmployEEInfoList();
        iCphTeacherService.sqlserverSynchronousMYSQL(list);//同步辅导员
        iCphTeacherService.Usertb();
        return success();
    }
}
