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.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;

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

    //添加
    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"));//考试班级
        if (teacherid != 0) {
            Map claMap = eSubjectBadstudentMapper.getClass(esid, teacherid);//教师监考班级
            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;
            }
        }
        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);
    }
}
