package com.xhkjedu.sexam.controller.exam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sexam.model.exam.ESubjectBadstudent;
import com.xhkjedu.sexam.model.exam.ESubjectInvigilate;
import com.xhkjedu.sexam.service.exam.ESubjectBadstudentService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ebs")
public class ESubjectBadstudentController {
    @Resource
    private ESubjectBadstudentService eSubjectBadstudentService;

    /**
     * 保存违规学生记录
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [badstudent]
     * @Author ywx
     * @Date 2022/8/17 10:50
     **/
    @PostMapping("/add")
    public ResultVo add(@RequestBody ESubjectBadstudent badstudent) {
        N_Utils.validation(new Object[]{badstudent.getStudentid(), "学生id", 1, badstudent.getEsid(), "考试科目id", 1
                , badstudent.getClassid(), "班级id", 1, badstudent.getExamid(), "考试id", 1
                , badstudent.getBadtype(), "违规类型", 1
        });
        eSubjectBadstudentService.save(badstudent);
        return new ResultVo(0, "保存成功");
    }

    /**
     * 实时监控--教师
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/17 11:44
     **/
    @PostMapping("/list_sjk")
    public ResultVo listJk(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        Map map = eSubjectBadstudentService.getJkDetail(invigilate);
        if (N_Utils.isEmptyInteger(classid)) {
            String classids = map.get("classids").toString();
            invigilate.setClassids(classids);
        }
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listStu(invigilate);
        List<Map> classes = (List<Map>) map.get("classes");
        Map<Object, Object> classMap = classes.stream().collect(Collectors.toMap(m -> m.get("classid"), m -> m.get("classname")));
        list.forEach(s -> s.put("classname", classMap.get(s.get("classid"))));
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        map.put("pageResult", pageResult);
        return new ResultVo(0, "获取成功", map);
    }

    @PostMapping("/list_jktea")
    public ResultVo listTeacherJk(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        Map map = eSubjectBadstudentService.getJkSchoolClassDetail(invigilate);
        if (N_Utils.isEmptyInteger(classid)) {
            String classids = map.get("classids").toString();
            invigilate.setClassids(classids);
        }
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listStu(invigilate);
        List<Map> schoolClasses = (List<Map>) map.get("schools");

        Map<Object, Object> classMap = classListToMap(schoolClasses);
        list.forEach(s -> s.put("classname", classMap.get(s.get("classid"))));
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        map.put("pageResult", pageResult);
        return new ResultVo(0, "获取成功", map);
    }

    private Map<Object, Object> classListToMap(List<Map> schools){
        Map<Object, Object> classMap = new HashMap<>();
        for(Map school : schools){
            List<Map> classes = (List<Map>) school.get("classes");
            for(Map cla : classes){
                classMap.put(cla.get("classid"),cla.get("classname"));
            }
        }
        return classMap;
    }

    /**
     * 实时监控--管理员
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/17 17:00
     **/
    @PostMapping("/list_gsjk")
    public ResultVo listGJk(@RequestBody ESubjectInvigilate invigilate) {
        Integer esid = invigilate.getEsid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        invigilate.setTeacherid(0);
        Map map = eSubjectBadstudentService.getJkDetail(invigilate);
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listStu(invigilate);
        List<Map> classes = (List<Map>) map.get("classes");
        Map<Object, Object> classMap = classes.stream().collect(Collectors.toMap(m -> m.get("classid"), m -> m.get("classname")));
        list.forEach(s -> s.put("classname", classMap.get(s.get("classid"))));
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        map.put("pageResult", pageResult);
        return new ResultVo(0, "获取成功", map);
    }

    @PostMapping("/list_jkgly")
    public ResultVo listGlyJk(@RequestBody ESubjectInvigilate invigilate) {
        Integer esid = invigilate.getEsid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        invigilate.setTeacherid(0);
        Map map = eSubjectBadstudentService.getJkSchoolClassDetail(invigilate);
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listStu(invigilate);
        List<Map> schoolClasses = (List<Map>) map.get("schools");
        Map<Object, Object> classMap = classListToMap(schoolClasses);
        list.forEach(s -> s.put("classname", classMap.get(s.get("classid"))));
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        map.put("pageResult", pageResult);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 试卷学生异常记录--教师
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/18 9:16
     **/
    @PostMapping("/list_bs")
    public ResultVo listBadStu(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        if (N_Utils.isEmptyInteger(classid)) {
            String classids = eSubjectBadstudentService.getClassIds(esid, teacherid);//教师监考班级ids
            invigilate.setClassids(classids);
        }
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listBadStu(invigilate);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * 试卷学生异常记录--管理员
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/18 9:16
     **/
    @PostMapping("/list_gbs")
    public ResultVo listGBadStu(@RequestBody ESubjectInvigilate invigilate) {
        Integer esid = invigilate.getEsid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listBadStu(invigilate);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * 实时监控--全部班级
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/22 15:28
     **/
    @PostMapping("/jk_bj")
    public ResultVo listJkClass(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1});
        Map map = eSubjectBadstudentService.getJkClass(invigilate);
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * @Description 实时监控--学校班级
     * @Date 2023/8/30 10:50:01
     * @Author WN
     * @Param [invigilate]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/jk_sclass")
    public ResultVo listJkSchoolClass(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1});
        Map map = eSubjectBadstudentService.listJkSchoolClass(invigilate);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 实时监控--全部学生
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/22 15:46
     **/
    @PostMapping("/jk_xs")
    public ResultVo listJkStu(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        Integer page = invigilate.getPage();
        Integer pageSize = invigilate.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        if (N_Utils.isEmptyInteger(classid)) {
            String classids = eSubjectBadstudentService.getClassIds(esid, teacherid);//教师监考班级ids
            invigilate.setClassids(classids);
        }
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listStu(invigilate);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * 实时监控--统计
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/8/22 15:58
     **/
    @PostMapping("/jk_tj")
    public ResultVo getJkRate(@RequestBody ESubjectInvigilate invigilate) {
        Integer teacherid = invigilate.getTeacherid();
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1});
        if (N_Utils.isEmptyInteger(classid)) {
            String classids = eSubjectBadstudentService.getClassIds(esid, teacherid);//教师监考班级ids
            invigilate.setClassids(classids);
        }
        Map map = eSubjectBadstudentService.getPaperRate(invigilate);
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * 监控学生
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/11/7 9:18
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/jk_xs2")
    public ResultVo listJkStu2(@RequestBody ESubjectInvigilate invigilate) {
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, classid, "班级id", 1});
        List<Map> list = eSubjectBadstudentService.listStu2(invigilate);
        return new ResultVo(0, "获取成功", list);
    }

    /*
     * 监控概况
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/11/7 9:33
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/get_jkgk")
    public ResultVo getJkGk(@RequestBody ESubjectInvigilate invigilate) {
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, classid, "班级id", 1});
        Map map = eSubjectBadstudentService.getJkGk(invigilate);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 视频监控-学生列表（带违规数量）
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [invigilate]
     * @Author ywx
     * @Date 2022/12/30 12:18
     **/
    @PostMapping("/jk_xs3")
    public ResultVo listJkStu3(@RequestBody ESubjectInvigilate invigilate) {
        Integer esid = invigilate.getEsid();
        Integer classid = invigilate.getClassid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, classid, "班级id", 1});
        List<Map> list = eSubjectBadstudentService.listStu3(invigilate);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * 视频监控-学生照片详情
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [bs]
     * @Author ywx
     * @Date 2022/12/30 12:29
     **/
    @PostMapping("/list_xszp")
    public ResultVo listXsZp(@RequestBody ESubjectBadstudent bs) {
        Integer esid = bs.getEsid();
        Integer studentid = bs.getStudentid();
        Integer page = bs.getPage();
        Integer pageSize = bs.getPageSize();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, studentid, "学生id", 1
                , page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectBadstudentService.listXsZp(bs);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }
}
