package com.xhkjedu.smarking.service.exam;

import com.xhkjedu.smarking.mapper.exam.MsClassMapper;
import com.xhkjedu.smarking.mapper.exam.MsClassStudentMapper;
import com.xhkjedu.smarking.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 MsSubjectSearchService {
    @Resource
    private MsClassMapper classMapper;
    @Resource
    private MsClassStudentMapper classStudentMapper;

    // 查找设置
    public List<Map> listSearchTeacher(Integer schoolid) {
        List<CTeacherVo> users = classMapper.listTeacherBySchoolId(schoolid);// 获取学校所有教师
        String teacherids = users.stream().map(u -> u.getTeacherid().toString()).collect(Collectors.joining(","));
        List<CTeacherVo> subjects = classMapper.listSubjectByTeacherIds(teacherids);// 获取教师科目
        String subjectids = subjects.stream().map(CTeacherVo::getSubjectid).distinct().collect(Collectors.joining("','"));
        List<CTeacherVo> subjects2 = classMapper.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 = classMapper.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<String,Object>> listClassTeacher(Integer examid, String subjectid) {
        List<Map<String,Object>> list = classMapper.listByExamId(examid, subjectid);
        String classids = list.stream().map(c -> c.get("classid").toString()).collect(Collectors.joining(","));
        List<Map> teacherList = classMapper.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 List<Map> listStudentByExamno(Integer examid, String examno, String subjectid) {
        return classStudentMapper.listStudentByExamIdAnExamno(examid, examno, subjectid);
    }
}
