package com.srs.web.controller.comprehensive;

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.Dto.SelectClass;
import com.srs.comprehensive.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/syncdata")
public class SyncDataController extends BaseController {
    @Autowired
    SyncDataService syncDataService;
    @Autowired
    ICphTeacherService iCphTeacherService;
    @Autowired
    ISrsMajorsService iSrsMajorsService;
    @Autowired
    ISrsClassService iSrsClassService;
    @Autowired
    ISrsStudentService iSrsStudentService;
    //中间表教职工信息
    @RequestMapping("/getEmployEEInfoList")
    public TableDataInfo getEmployEEInfoList(String zgh){
        startPage();
        List<Map> list=syncDataService.getEmployEEInfoList(zgh);
        return getDataTable(list);
    }
    //同步教职工
    @RequestMapping("/synchronousEmployEEInfoList")
    public AjaxResult synchronousEmployEEInfoList(){
        List<Map> list=syncDataService.getEmployEEInfoList(null);
        iCphTeacherService.synchronousMYSQL(list);//同步教职工
        iCphTeacherService.UsertbNew();
        return success();
    }
    //中间表专业信息
    @RequestMapping("/getSpecialtyInfoList")
    public TableDataInfo getSpecialtyInfoList(String zydm){
        startPage();
        List<Map> list=syncDataService.getSpecialtyInfoList(zydm);
        return getDataTable(list);
    }
    //同步专业
    @RequestMapping("/synchronousSpecialtyInfoList")
    public AjaxResult synchronousSpecialtyInfoList(){
        List<Map> list=syncDataService.getSpecialtyALLInfo();
        iSrsMajorsService.synchronousMYSQL(list);//同步专业
        return success();
    }
    //中间表班级
    @RequestMapping("/classInfoList")
    public TableDataInfo classInfoList(SelectClass selectClass){
        startPage();
        List<Map> list=syncDataService.getClassInfoList(selectClass);
        return getDataTable(list);
    }

    //同步班级
    @RequestMapping("/synchronousClass")
    public AjaxResult synchronousClass(SelectClass selectClass){
        List<Map> list=syncDataService.getClassInfoList(selectClass);
        iSrsClassService.synchronousMYSQL(list);//同步班级
        return success();
    }
    @RequestMapping("/getStudentInfo")
    public AjaxResult getStudentInfoList(@RequestParam("pageNum")Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestParam(value = "stuNo", required = false)String stuNo,
                                         @RequestParam(value = "gradeCode", required = false)String gradeCode){
        int studentInfoNumber = syncDataService.getStudentInfoNumber(gradeCode);
        List<Map> list=syncDataService.getStudentInfoList(pageNum,pageSize,stuNo,gradeCode);
        return success(list).put("total", studentInfoNumber);
    }
    //同步学生
    @RequestMapping("/synchronousStudent")
    public AjaxResult synchronousStudent(String gradeCode){
        int pageNum=1;
        int pageSize=500;
        int studentInfoNumber = syncDataService.getStudentInfoNumber(gradeCode);//总数
        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=syncDataService.getStudentInfoList(pageNum,pageSize,null,gradeCode);
            List<Map> maps = iSrsStudentService.synchronousMYSQL(list);//同步到mysql
            stuNoList.addAll(maps);
            list.clear();
        }
        iSrsStudentService.addStudentData(stuNoList);//添加到学生表
        iSrsStudentService.Usertb();//同步到用户表
        return success();
    }

}
