package com.xhkjedu.sexam.service.exam;

import com.xhkjedu.sexam.mapper.exam.EBaseMapper;
import com.xhkjedu.sexam.mapper.exam.EClassMapper;
import com.xhkjedu.sexam.mapper.exam.EClassStudentMapper;
import com.xhkjedu.sexam.mapper.exam.ESubjectMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperCorrectclassMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperCorrectquestionMapper;
import com.xhkjedu.sexam.mapper.system.UserMapper;
import com.xhkjedu.sexam.vo.exam.CTeacherVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ESubjectSearchService {
    @Resource
    private EClassMapper eClassMapper;
    @Resource
    private EBaseMapper eBaseMapper;
    @Resource
    private ESubjectMapper eSubjectMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private EClassStudentMapper eClassStudentMapper;
    @Resource
    private EPaperCorrectclassMapper ePaperCorrectclassMapper;
    @Resource
    private EPaperCorrectquestionMapper ePaperCorrectquestionMapper;

    // 查找设置
    public List<Map> listSearchTeacher(Integer schoolid) {
        List<CTeacherVo> users = eClassMapper.listTeacherBySchoolId(schoolid);// 获取学校所有教师
        String teacherids = users.stream().map(u -> u.getTeacherid().toString()).collect(Collectors.joining(","));
        List<CTeacherVo> subjects = eClassMapper.listSubjectByTeacherIds(teacherids);// 获取教师科目
        String subjectids = subjects.stream().map(CTeacherVo::getSubjectid).distinct().collect(Collectors.joining("','"));
        List<CTeacherVo> subjects2 = eClassMapper.listSubjectBySubjectIds(subjectids);
        Map<Integer, String> subjectMap = subjects.stream().collect(Collectors.toMap(CTeacherVo::getTeacherid,
                                                                                     CTeacherVo::getSubjectid));
        for (CTeacherVo teacher : users) {
            teacher.setSubjectid(subjectMap.get(teacher.getTeacherid()));
        }
        List<Map> subjectTeachers = new ArrayList<>();
        List<CTeacherVo> teachers;
        for (CTeacherVo s : subjects2) {
            String subjectid = s.getSubjectid();
            teachers = users.stream().filter(u -> subjectid.equals(u.getSubjectid())).collect(Collectors.toList());
            Map st = new HashMap();
            st.put("subjectid", subjectid);
            st.put("subjectname", s.getSubjectname());
            st.put("teachers", teachers);
            subjectTeachers.add(st);
        }
        return subjectTeachers;
    }

    // 快捷设置
    public Map listQuickTeacher(String username, Integer examid) {
        Map map = new HashMap();
        String[] usernames = username.split("，");
        List<Map> teachers = eClassMapper.listSearchTeacher(usernames, examid);
        Map<String, Object> nameMap = teachers.stream().map(t -> t.get("username").toString()).distinct().collect(Collectors.toMap(t -> t, t -> t));
        StringBuilder sb = new StringBuilder();
        for (String name : usernames) {
            if (nameMap.get(name) == null) {
                sb.append(name).append("，");
            }
        }
        map.put("teachers", teachers);// 匹配的教师
        String notExist = sb.toString();// 未匹配的教师
        if (N_Utils.isNotEmpty(notExist)) {
            notExist = notExist.substring(0, notExist.length() - 1);
        }
        map.put("notExist", notExist);
        return map;
    }

    // 按班设置
    public List<Map> listClassTeacher(Integer examid, String subjectid) {
        List<Map> list = eClassMapper.listByExamId(examid);
        String classids = list.stream().map(c -> c.get("classid").toString()).collect(Collectors.joining(","));
        List<Map> teacherList = eClassMapper.listClassTeacher(classids, subjectid);
        for (Map map : list) {
            List<Map> teachers = teacherList.stream().filter(t -> t.get("classid").equals(map.get("classid"))).collect(Collectors.toList());
            map.put("teachers", teachers);
        }
        return list;
    }

    public Map listSubject(Integer examid) {
        Map map = eBaseMapper.findById(examid);
        List<Map> subjects = eSubjectMapper.listSubjectCSet(examid);// 考试科目阅卷设置列表
        Integer schoolid = Integer.parseInt(map.get("schoolid").toString());// 发起考试的学校
        for (Map s : subjects) {
            Integer correcttype = (Integer) s.get("correcttype");
            Integer epid = (Integer) s.get("epid");
            List<Map> list = new ArrayList<>();
            if (correcttype == 1) {
                list = eClassMapper.listSchoolClassByExamid(schoolid, examid);// 考试班级
                List<Map> users = ePaperCorrectclassMapper.listByEpId(epid);// 试卷批阅人
                for (Map m : list) {
                    List<Map> cmapList = (List<Map>) m.get("classes");
                    for (Map c : cmapList) {
                        List<Map> teachers = users.stream().filter(u -> u.get("classid").equals(c.get("classid")))
                                                  .sorted(Comparator.comparing(t -> t.get("username").toString())).collect(Collectors.toList());
                        c.put("teachers", teachers);
                    }
                }
            } else if (correcttype == 2) {
                list = ePaperCorrectquestionMapper.listQnByEpId(epid);
                List<Map> users = ePaperCorrectquestionMapper.listByEpId(epid);// 试卷批阅人
                for (Map q : list) {
                    Object qorder = q.get("qorder");
                    List<Map> teachers = users.stream().filter(u -> u.get("qorder").equals(qorder))
                                              .sorted(Comparator.comparing(t -> t.get("username").toString())).collect(Collectors.toList());
                    q.put("teachers", teachers);
                }
            }
            s.put("correctList", list);
        }
        map.put("subjects", subjects);
        return map;
    }

    // 主监考人查找
    public List<Map> listMasterTeacher(Integer schoolid) {
        return userMapper.listMasterTeacher(schoolid);
    }

    // 主监考人快捷查找
    public Map listQuickMasterTeacher(Integer schoolid, String username) {
        Map map = new HashMap();
        String[] usernames = username.split("，");
        List<Map> teachers = userMapper.listQuickMasterTeacher(usernames, schoolid);
        Map<String, Object> nameMap = teachers.stream().map(t -> t.get("username").toString()).distinct().collect(Collectors.toMap(t -> t, t -> t));
        StringBuilder sb = new StringBuilder();
        for (String name : usernames) {
            if (nameMap.get(name) == null) {
                sb.append(name).append("，");
            }
        }
        map.put("teachers", teachers);// 匹配的教师
        String notExist = sb.toString();// 未匹配的教师
        if (N_Utils.isNotEmpty(notExist)) {
            notExist = notExist.substring(0, notExist.length() - 1);
        }
        map.put("notExist", notExist);
        return map;
    }

    // 据考号搜索参加考试学生
    public List<Map> listStudentByExamno(Integer examid, String examno) {
        return eClassStudentMapper.listStudentByExamidAnExamno(examid, examno);
    }
}
