<?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.ESubjectMapper">
    <!--考试设置-->
    <update id="setExam">
        <foreach collection="subjects" item="s" separator=";">
            update e_subject set sdate=#{s.sdate},begintime=#{s.begintime},endtime=#{s.endtime},teacherid=#{s.teacherid}
            ,handleid=#{handleid},handletime=unix_timestamp()
            where examid=#{s.examid} and subjectid=#{s.subjectid}
        </foreach>
    </update>

    <!--根据考试科目id更改考试科目状态-->
    <update id="updateExamSubjectState">
        update e_subject set esstate=#{esstate} where esid = #{esid};
    </update>

    <!--根据试卷id更新考试科目状态-->
    <update id="updateEsubjectStateByEpid">
        update e_subject es,e_paper ep set es.esstate=#{esstate} where es.esid=ep.esid and ep.epid=#{epid}
    </update>
    <!--考试科目列表-->
    <select id="listSubject" resultType="java.util.Map">
        select es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.teacherid,u.username
        from e_subject es left join t_user u on es.teacherid=u.userid
        where examid=#{examid} order by es.esid
    </select>
    <!--根据考试科目id获取考试id-->
    <select id="getExamIdByEsId" resultType="java.lang.Integer">
        select examid from e_subject where esid=#{esid} limit 1
    </select>
    <!--考试科目设置列表-->
    <select id="listSubjectSet" resultType="java.util.Map">
        select es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.esstate
        ,ep.correcttype,es.esid,ep.epid,ep.ptype
        ,(select count(*) from e_subject_invigilate ei where ei.esid=es.esid)jkjsnum
        ,count(distinct case when eps.sstate=3 then eps.epsid else null end)qknum
        ,count(distinct case when eps.sstate=2 then eps.epsid else null end)tjnum
        ,ifnull(truncate(count(case when psq.answered=1 AND psq.checked=2 then psq.epsqid else null end)*100/
                         count(case when psq.answered=1 then psq.epsqid else null end),2),0)pyrate
        from e_subject es left join e_paper ep on es.esid = ep.esid
        left join e_paper_student eps on ep.epid = eps.epid
        left join e_paper_student_question psq on psq.epid=ep.epid
        where es.examid=#{examid}
        group by es.esid
    </select>
    <!--考试科目阅卷设置列表-->
    <select id="listSubjectCSet" resultType="java.util.Map">
        select es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.esstate
        ,ep.ptype,ep.epid,ep.pnum,ep.pscore,ep.correcttype,ep.ptype
        from e_subject es left join e_paper ep on es.esid = ep.esid
        where es.examid=#{examid}
    </select>
    <!--根据考试科目id获取考试科目基本信息-->
    <select id="getESubjectByEsId" resultType="java.util.Map">
        select es.sdate,es.begintime,es.endtime,es.subjectname
        ,eb.examname,eb.schoolid,eb.examid,eb.gradeid,eb.examtype
        from e_subject es left join e_base eb on es.examid = eb.examid
        where es.esid=#{esid}
    </select>
    <!--获取未完成的考试试卷数量-->
    <select id="countNoCompletePaper" resultType="java.lang.Integer">
        select count(*) from e_subject where examid=#{examid} and esstate!=1
    </select>

    <!--获取考试关联科目及科目下试题数量-->
    <select id="listSubjectAndPnum" resultType="java.util.Map">
        select es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.teacherid,p.pnum,p.ptype,p.pscore
        from e_subject es left join e_paper p on es.esid=p.esid
        where es.examid=#{examid} order by es.esid
    </select>
    <!--考试科目选择列表-->
    <select id="listSubjectForSel" resultType="java.util.Map">
        select es.subjectid,es.subjectname from e_subject es where es.examid=#{examid}
    </select>
    <!--试卷开始考试-->
    <update id="startPaper">
        update e_base eb left join e_subject es on eb.examid = es.examid
        set es.pstate=1
        where eb.examstate=1 and eb.deleted=1 and eb.exammode=1
        and es.pstate=0 and concat(es.sdate,' ',es.begintime)&lt;=now()
    </update>
    <!--试卷结束考试-->
    <update id="endPaper">
        update e_base eb left join e_subject es on eb.examid = es.examid
        set es.pstate=2
        where eb.examstate=1 and eb.deleted=1 and eb.exammode=1
        and es.pstate=1 and concat(es.sdate,' ',es.endtime)&lt;=now()
    </update>
</mapper>
