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.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.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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;

    //查找设置
    public List<Map> listSearchTeacher(Integer examid, Integer schoolid) {
        List<Map> classes = eClassMapper.listByExamId(examid);
        String classids = classes.stream().map(c -> c.get("classid").toString()).collect(Collectors.joining(","));
        List<CTeacherVo> teachers = eClassMapper.listTeacherByClassIds(classids);//获取班级教师
        String teacherids =
                teachers.stream().map((CTeacherVo::getTeacherid)).map(String::valueOf).distinct().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<String, String> subjectNameMap = subjects2.stream().collect(Collectors.toMap(CTeacherVo::getSubjectid,
                CTeacherVo::getSubjectname));
        Map<Integer, String> subjectMap = subjects.stream().collect(Collectors.toMap(CTeacherVo::getTeacherid,
                CTeacherVo::getSubjectid));
        List<CTeacherVo> users = eClassMapper.listTeacherBySchoolId(schoolid);//获取学校所有教师
        for (CTeacherVo teacher : teachers) {
            CTeacherVo vo = users.stream().filter(u -> u.getTeacherid().equals(teacher.getTeacherid())).findFirst().orElse(null);
            if (vo != null) {
                teacher.setUsername(vo.getUsername());
                teacher.setLoginname(vo.getLoginname());
                teacher.setUsersex(vo.getUsersex());
            }
            teacher.setSubjectid(subjectMap.get(teacher.getTeacherid()));
        }
        for (Map cmap : classes) {
            Integer classid = (Integer) cmap.get("classid");
            LinkedHashMap<String, List<CTeacherVo>> scollect = teachers.stream()
                    .filter(t -> t.getClassid().equals(classid) && t.getUsername() != null && t.getSubjectid() != null)
                    .collect(Collectors.groupingBy(t -> t.getSubjectid(), LinkedHashMap::new, Collectors.toList()));
            List<Map> subjectTeachers = new ArrayList<>();
            for (Map.Entry<String, List<CTeacherVo>> entry : scollect.entrySet()) {
                String subjectid = entry.getKey();
                Map st = new HashMap();
                st.put("subjectid", subjectid);
                st.put("subjectname", subjectNameMap.get(subjectid));
                st.put("teachers", entry.getValue());
                subjectTeachers.add(st);
            }
            cmap.put("subjectTeachers", subjectTeachers);
        }
        return classes;
    }

    //快捷设置
    public Map listQuickTeacher(String username, Integer classid) {
        Map map = new HashMap();
        String[] usernames = username.split("，");
        List<Map> teachers = eClassMapper.listSearchTeacher(usernames, classid);
        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);//考试科目阅卷设置列表
        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);
    }
}
