<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xhkjedu.sexam.mapper.exam.ESubjectInvigilateMapper">
    <!--删除考试科目监考教师-->
    <delete id="delByEsId">
        delete from e_subject_invigilate where esid=#{esid}
    </delete>
    <!--监考设置列表-->
    <select id="listSubject" resultType="java.util.Map">
        select s.esid,s.subjectid,s.sdate,s.begintime,s.endtime,s.esstate,s.pstate,s.subjectname
        ,ifnull(ep.pnum,0) as pnum,ifnull(ep.pscore,0) as pscore,ep.correcttype
        from e_subject s left join e_paper ep on s.esid = ep.esid
        where s.examid=#{examid} group by s.esid
    </select>
    <!--考试监考人-->
    <select id="listByEsId" resultType="java.util.Map">
        select ei.classid,ei.majored,ei.teacherid,u.username
        from e_subject_invigilate ei left join t_user u on ei.teacherid = u.userid
        where ei.esid=#{esid}
    </select>
    <!--教师监考-监控列表-->
    <select id="listJkSubject" resultType="java.util.Map">
        select es.*,count(eps.epsid)examnum
        ,count(if(eps.sstate in(0,3), eps.epsid, null)) wjcnum
        ,count(if(eps.sstate in(1,2), eps.epsid, null))jcnum
        ,truncate(count(if(eps.sstate in(1,2), eps.epsid, null))*100/count(eps.epsid),2) jcrate
        ,count(if(eps.sstate=2, eps.epsid, null))tjnum
        ,count(if(eps.sstate=3, eps.epsid, null))qknum
        from(select es.esid,es.sdate,es.begintime,es.endtime,es.esstate,es.subjectname,es.pstate
        ,eb.examname,eb.gradeid,eb.examtype,eb.examstate,eb.hashb,eb.schoolid,max(ei.majored)majored
        ,(if(max(ei.majored)=0, group_concat(distinct ei.classid), group_concat(distinct ec.classid)))classids
        ,concat('[',group_concat(distinct json_object('classid',ec.classid,'classname',ec.classname
        ,'schoolid',ec.schoolid,'schoolname',s.schoolname)),']')classnames
        from e_subject_invigilate ei left join e_subject es on ei.esid=es.esid
        left join e_base eb on eb.examid = es.examid
        left join e_class ec on eb.examid = ec.examid
        left join t_school s on ec.schoolid = s.schoolid
        where eb.deleted=1 and eb.examstate!=0 and eb.exammode=1 and eb.monitored=1
        <if test="teacherid!=null and teacherid!=0">
            and ei.teacherid=#{teacherid}
        </if>
        <if test="examid!=null and examid!=0">
            and es.examid=#{examid}
        </if>
        <if test="esstate==1">
            and es.pstate!=2
        </if>
        <if test="esstate==2">
            and es.pstate=2
        </if>
        group by es.esid having classids is not null
        )es left join e_paper_student eps on es.esid = eps.esid and find_in_set(eps.classid,es.classids)
        group by es.esid
        <if test="esstate==1">
            order by abs(timediff(concat(es.sdate,' ',es.begintime),#{strtime}))
        </if>
        <if test="esstate==2">
            order by es.sdate desc,es.endtime desc
        </if>
    </select>
    <!--缺考学生列表-->
    <select id="listMissStu" resultType="java.util.Map">
        select eps.studentid,u.username,eps.sstate
        from e_paper_student eps  left join t_user u on eps.studentid = u.userid
        where eps.esid=#{vo.esid} and eps.sstate in(0,3) and eps.classid in(${vo.classids})
        order by convert(u.username using gbk),u.userid
    </select>
    <!--考试监考人-->
    <select id="listUserByEsIds" resultType="com.xhkjedu.sexam.vo.exam.EITeacherVo">
        select ei.esid,ei.classid,ei.majored,ei.teacherid,u.username,u.loginname
        from e_subject_invigilate ei left join t_user u on ei.teacherid = u.userid
        where ei.esid in(${esids}) order by convert(u.username using gbk),u.userid
    </select>
</mapper>
