package com.xhkjedu.sexam.service.paper;

import com.xhkjedu.sexam.mapper.exam.EClassMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperCorrectclassMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperCorrectquestionMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperMapper;
import com.xhkjedu.sexam.model.paper.EPaperCorrectclass;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class EPaperCorrectclassService {
    @Resource
    private EPaperCorrectclassMapper ePaperCorrectclassMapper;
    @Resource
    private EClassMapper eClassMapper;
    @Resource
    private EPaperMapper ePaperMapper;
    @Resource
    private EPaperCorrectquestionMapper ePaperCorrectquestionMapper;

    //设置批阅人
    @Transactional(rollbackFor = Exception.class)
    public void setTeacher(Integer epid, Integer createid, List<EPaperCorrectclass> teachers) {
        //Integer examstate = ePaperMapper.getExamStateByEpId(epid);
        //if (2 <= examstate) throw new MissingParametersException("考试已结束禁止操作");
        ePaperCorrectclassMapper.delByEpId(epid);//删除批阅人
        ePaperCorrectquestionMapper.delByEpId(epid);//删除批阅人
        int timestamp = N_Utils.getSecondTimestamp();
        teachers.forEach(t -> {
            t.setEpid(epid);
            t.setCreateid(createid);
            t.setCreatetime(timestamp);
        });
        ePaperCorrectclassMapper.insertList(teachers);//添加批阅人
        ePaperMapper.updateCorrectType(epid, 1);//修改批阅设置
    }

    //查看批阅人
    public List<Map> listByEpId(Integer epid) {
        Integer examId = ePaperMapper.getExamIdByEpId(epid);
        List<Map> list = eClassMapper.listByExamId(examId);//考试班级
        List<Map> users = ePaperCorrectclassMapper.listByEpId(epid);//试卷批阅人
        for (Map map : list) {
            List<Map> teachers = users.stream().filter(u -> u.get("classid").equals(map.get("classid"))).collect(Collectors.toList());
            map.put("teachers", teachers);
        }
        return list;
    }

    //查看批阅人--学校班级
    public List<Map> listClassCorrectByEpId(Integer epid) {
        Map examinfo = ePaperMapper.getExamSchoolidByEpId(epid);
        Integer examId = Integer.parseInt(examinfo.get("examid").toString());
        Integer schoolId = Integer.parseInt(examinfo.get("schoolid").toString());

        List<Map> list = eClassMapper.listSchoolClassByExamid(schoolId,examId);//考试班级
        List<Map> users = ePaperCorrectclassMapper.listByEpId(epid);//试卷批阅人
        for (Map map : list) {
            List<Map> classes = (List<Map>) map.get("classes");
            for(Map classMap : classes){
                List<Map> teachers = users.stream().filter(u -> u.get("classid").equals(classMap.get("classid"))).collect(Collectors.toList());
                classMap.put("teachers", teachers);
            }
        }
        return list;
    }

    //根据试卷id获取考试科目基本信息
    public Map getESubjectByEpId(Integer epid) {
        return ePaperMapper.getESubjectByEpId(epid);
    }

    //删除批阅人
    @Transactional(rollbackFor = Exception.class)
    public void deleteByEpId(Integer epid) {
        //Integer examstate = ePaperMapper.getExamStateByEpId(epid);
        //if (2 <= examstate) throw new MissingParametersException("考试已结束禁止操作");
        ePaperCorrectclassMapper.delByEpId(epid);//删除批阅人
        ePaperCorrectquestionMapper.delByEpId(epid);//删除批阅人
        ePaperMapper.updateCorrectType(epid, 0);
    }
}
