<?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=#{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
        ,(case when es.esstate!=0 then ep.handleid else es.teacherid end)teacherid
        ,(select u.username from t_user u where u.userid=
        (case when es.esstate!=0 then ep.handleid else ifnull(es.teacherid,0) end))username
        from e_subject es left join e_paper ep on es.esid = ep.esid
        where es.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>
    <!--考试科目设置列表-->
    <resultMap id="subjectResult" type="java.util.Map">
        <collection property="yjteachers" ofType="java.util.Map" javaType="java.util.List"
                    select="listYjTeacher" column="epid=epid,correcttype=correcttype">
        </collection>
        <collection property="jkteachers" ofType="java.util.Map" javaType="java.util.List"
                    select="listJkTeacher" column="esid=esid">
        </collection>
    </resultMap>
    <select id="listYjTeacher" resultType="java.util.Map">
        <if test="correcttype==2">
            select u.username,u.loginname
            from t_user u where u.userid in(select distinct epc.teacherid
            from e_paper_correctquestion epc where epc.epid=#{epid})
            order by convert(u.username using gbk),u.userid
        </if>
        <if test="correcttype!=2">
            select u.username,u.loginname
            from t_user u where u.userid in(select distinct epc.teacherid
            from e_paper_correctclass epc where epc.epid=#{epid})
            order by convert(u.username using gbk),u.userid
        </if>
    </select>
    <select id="listJkTeacher" resultType="java.util.Map">
        select u.username,u.loginname
        from t_user u where u.userid in(select distinct ei.teacherid
        from e_subject_invigilate ei where ei.esid=#{esid})
        order by convert(u.username using gbk),u.userid
    </select>
    <select id="listSubjectSet" resultMap="subjectResult">
        select es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.esstate
        ,ep.correcttype,es.esid,ep.epid,ep.ptype,ep.pnum,ep.pscore
        ,(select u.username from t_user u where u.userid=
        (case when es.esstate!=0 then ep.handleid else ifnull(es.teacherid,0) end))cjteacher
        ,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.epsid=eps.epsid
        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,eb.createid,eb.schoolid,eb.hashb
        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} and es.pstate=2 order by es.begintime,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;=#{strtime}
    </update>
    <!--试卷结束考试-->
    <update id="endPaper">
        update e_base eb left join e_subject es on eb.examid = es.examid
        left join e_paper_student eps on es.esid=eps.esid and eps.sstate in(0,1)
        set es.pstate=2,eps.sstate=(case when eps.sstate=0 then 3 else 2 end)
        ,eps.endtime=(case when eps.sstate=1 then #{timestamp} else eps.endtime end)
        where eb.examstate>=1 and eb.deleted=1 and eb.exammode=1
        and es.pstate=1 and adddate(concat(es.sdate,' ',es.endtime),interval 5 minute) &lt;#{strtime}
    </update>
    <!--获取考试科目状态-->
    <select id="getPstateByExamIdSid" resultType="java.lang.Integer">
        select pstate from e_subject where examid=#{examid} and subjectid=#{subjectid}
    </select>
    <!--修改考试科目试卷状态-->
    <update id="updatePstate">
        update e_subject set pstate=2 where examid=#{examid}
    </update>
</mapper>
