package com.xhkjedu.sexam.service.paper;

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.EPaperCorrectquestion;
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 EPaperCorrectquestionService {
    @Resource
    private EPaperCorrectquestionMapper ePaperCorrectquestionMapper;
    @Resource
    private EPaperMapper ePaperMapper;
    @Resource
    private EPaperCorrectclassMapper ePaperCorrectclassMapper;

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

    //查看批阅人
    public List<Map> listByEpId(Integer epid) {
        List<Map> 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)).collect(Collectors.toList());
            q.put("teachers", teachers);
        }
        return list;
    }

    //获取主观题题号
    public List<Map> listQnByEpId(Integer epid) {
        return ePaperCorrectquestionMapper.listQnByEpId(epid);
    }

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