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.ESubjectInvigilateMapper;
import com.xhkjedu.sexam.mapper.exam.ESubjectMapper;
import com.xhkjedu.sexam.model.exam.ESubjectInvigilate;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ESubjectInvigilateService {
    @Resource
    private ESubjectInvigilateMapper eSubjectInvigilateMapper;
    @Resource
    private EBaseMapper eBaseMapper;
    @Resource
    private ESubjectMapper eSubjectMapper;
    @Resource
    private EClassMapper eClassMapper;

    //设置监考人--设置
    @Transactional(rollbackFor = Exception.class)
    public void setTeacher(Integer esid, String subjectid, Integer createid, List<ESubjectInvigilate> teachers) {
        eSubjectInvigilateMapper.delByEsId(esid);//删除考试科目监考教师
        int timestamp = N_Utils.getSecondTimestamp();
        teachers.forEach(t -> {
            t.setEsid(esid);
            t.setSubjectid(subjectid);
            t.setCreateid(createid);
            t.setCreatetime(timestamp);
        });
        eSubjectInvigilateMapper.insertList(teachers);
    }

    //监考设置列表
    public Map listSubject(Integer examid) {
        Map map = eBaseMapper.findById(examid);
        List<Map> subjects = eSubjectInvigilateMapper.listSubject(examid);//监考设置列表
        map.put("subjects", subjects);
        return map;
    }

    //监考人查看
    public Map listByEsId(Integer esid) {
        Map map = new HashMap();
        Integer examId = eSubjectMapper.getExamIdByEsId(esid);//根据考试科目id获取考试id
        List<Map> list = eClassMapper.listByExamId(examId);//考试班级
        List<Map> users = eSubjectInvigilateMapper.listByEsId(esid);//考试监考人
        map.put("classes", list);
        map.put("teachers", users);
        return map;
    }

    //监考人删除
    public void delByEsId(Integer esid) {
        eSubjectInvigilateMapper.delByEsId(esid);//删除考试科目监考教师
    }

    //教师监考-监控列表
    public List<Map> listJkSubject(Integer examid, Integer teacherid, Integer esstate) {
        List<Map> list = eSubjectInvigilateMapper.listJkSubject(examid, teacherid, esstate);
        return list;
    }

    //根据考试科目id获取考试科目基本信息
    public Map getESubjectByEsId(Integer esid) {
        return eSubjectMapper.getESubjectByEsId(esid);
    }

}
