package com.xhkjedu.sexam.service.exam;

import com.xhkjedu.sexam.mapper.exam.EClassMapper;
import com.xhkjedu.sexam.mapper.exam.ESubjectBadstudentMapper;
import com.xhkjedu.sexam.mapper.exam.ESubjectMapper;
import com.xhkjedu.sexam.mapper.system.UserMapper;
import com.xhkjedu.sexam.model.exam.ESubjectBadstudent;
import com.xhkjedu.sexam.model.exam.ESubjectInvigilate;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

@Service
public class ESubjectBadstudentService {
    @Resource
    private ESubjectBadstudentMapper eSubjectBadstudentMapper;
    @Resource
    private ESubjectMapper eSubjectMapper;
    @Resource
    private EClassMapper eClassMapper;
    @Resource
    private UserMapper userMapper;

    //添加
    public void save(ESubjectBadstudent model) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        eSubjectBadstudentMapper.insertSelective(model);
    }

    //试卷学生
    public List<Map> listStu(ESubjectInvigilate ei) {
        List<Map> students = eSubjectBadstudentMapper.listStu(ei);
        return students;
    }

    //监控详情
    public Map getJkDetail(ESubjectInvigilate ei) {
        Map map = getJkClass(ei);//监考班级
        if (N_Utils.isEmptyInteger(ei.getClassid())) {
            ei.setClassids(map.get("classids").toString());
        }
        Map rate = eSubjectBadstudentMapper.getPaperRate(ei);//学生进场概况
        map.putAll(rate);
        return map;
    }

    //监考班级
    public Map getJkClass(ESubjectInvigilate ei) {
        Integer esid = ei.getEsid();
        Integer teacherid = ei.getTeacherid();
        Map map = eSubjectMapper.getESubjectByEsId(esid);//考试科目基本信息
        String classids = "";
        List<Map> classes = eClassMapper.listByExamId((Integer) map.get("examid"));//考试班级
        Integer createid= (Integer) map.get("createid");
        if (!N_Utils.isEmptyInteger(teacherid) && !createid.equals(teacherid)
                && userMapper.getUsertypeByUserid(teacherid) == 1) {
            Map claMap = eSubjectBadstudentMapper.getClass(esid, teacherid);//教师监考班级
            if (claMap == null) {
                classes = new ArrayList<>();
            } else if (claMap.get("majored").equals(0)) {//教师不是主监考
                List<Map> classes2 = new ArrayList<>();
                classids = claMap.get("classids").toString();
                String[] classIds = classids.split(",");
                for (String classid : classIds) {
                    Map cla = classes.stream().filter(c -> c.get("classid").toString().equals(classid)).findFirst().orElse(null);
                    if (cla != null) classes2.add(cla);
                }
                classes = classes2;
            }
        } else {
            classids = classes.stream().map(c -> c.get("classid").toString()).collect(Collectors.joining(","));
        }
        map.put("classids", classids);
        map.put("classes", classes);
        return map;
    }

    //试卷学生异常记录
    public List<Map> listBadStu(ESubjectInvigilate ei) {
        return eSubjectBadstudentMapper.listBadStu(ei);
    }

    //教师监考班级ids
    public String getClassIds(Integer esid, Integer teacherid) {
        String classids = "";
        if (teacherid != 0) {
            Map claMap = eSubjectBadstudentMapper.getClass(esid, teacherid);//教师监考班级
            if (claMap.get("majored").equals(0)) {//教师不是主监考
                classids = claMap.get("classids").toString();
            }
        }
        return classids;
    }

    //学生进场概况
    public Map getPaperRate(ESubjectInvigilate ei) {
        return eSubjectBadstudentMapper.getPaperRate(ei);
    }

    //监考学生
    public List<Map> listStu2(ESubjectInvigilate ei) {
        return eSubjectBadstudentMapper.listStu2(ei);
    }

    //监控详情
    public Map getJkGk(ESubjectInvigilate ei) {
        return eSubjectBadstudentMapper.getPaperRate(ei);//学生进场概况
    }

    //视频监控-学生列表（带违规数量）
    public List<Map> listStu3(ESubjectInvigilate ei) {
        return eSubjectBadstudentMapper.listStu3(ei);
    }

    //视频监控-学生照片详情
    public List<Map> listXsZp(ESubjectBadstudent bs) {
        return eSubjectBadstudentMapper.listXsZp(bs);
    }

    /*
     * @Description 监控详情
     * @Date 2023/8/30 11:17:52
     * @Author WN
     * @Param [ei]
     * @Return java.util.Map
     **/
    public Map getJkSchoolClassDetail(ESubjectInvigilate ei) {
        Map map = listJkSchoolClass(ei);//监考班级
        if (N_Utils.isEmptyInteger(ei.getClassid())) {
            ei.setClassids(map.get("classids").toString());
        }
        Map rate = eSubjectBadstudentMapper.getPaperRate(ei);//学生进场概况
        map.putAll(rate);
        return map;
    }

    /*
     * @Description 监控班级（学校班级）
     * @Date 2023/8/30 10:20:37
     * @Author WN
     * @Param [invigilate]
     * @Return java.util.Map
     **/
    public Map listJkSchoolClass(ESubjectInvigilate ei) {
        Integer esid = ei.getEsid();
        Integer teacherid = ei.getTeacherid();
        Map map = eSubjectMapper.getESubjectByEsId(esid);//考试科目基本信息
        String classids = "";
        Integer examid = (Integer) map.get("examid");
        List<Map> schoolClasses = eClassMapper.listSchoolClassByExamid((Integer) map.get("schoolid"),examid);//考试学校班级学生

        Integer createid= (Integer) map.get("createid");
        if (!N_Utils.isEmptyInteger(teacherid) && !createid.equals(teacherid)
                && userMapper.getUsertypeByUserid(teacherid) == 1) {
            Map claMap = eSubjectBadstudentMapper.getClass(esid, teacherid);//教师监考班级
            if (claMap == null) {
                schoolClasses = new ArrayList<>();
            } else if (claMap.get("majored").equals(0)) {//教师不是主监考
                classids = claMap.get("classids").toString();
                String[] classIds = classids.split(",");

                List<Map> schoolClasses2 = new ArrayList<>();
                for(Map scMap : schoolClasses){
                    List<Map> classes2 = new ArrayList<>();
                    List<Map> classMap = (List<Map>) scMap.get("classes");//获取学校下班级
                    for (String classid : classIds) {
                        for (Map cla : classMap) {
                            if(cla.get("classid").toString().equals(classid)){
                                classes2.add(cla);
                                break;
                            }
                        }
                    }
                    if(N_Utils.isListEmpty(classes2)) continue;
                    scMap.put("classes",classes2);
                    schoolClasses2.add(scMap);
                }

                schoolClasses = schoolClasses2;

            }
        } else {
            List<String> classidList = new ArrayList<>();
            for(Map scMap : schoolClasses){
                List<Map> classMap = (List<Map>) scMap.get("classes");//获取学校下班级
                for(Map cMap : classMap){
                    classidList.add(cMap.get("classid").toString());
                }
            }

            classids = String.join(",",classidList);
        }
        map.put("classids", classids);
        map.put("schools", schoolClasses);
        return map;
    }
}
