<?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.paper.EPaperCorrectTeacherMapper">
    <!--待阅卷列表-->
    <select id="listPending" resultType="java.util.Map">
        select a.*,eb.examname,eb.gradeid,eb.examtype,eb.examstate,eb.hashb,eb.schoolid,es.sdate,es.begintime,es.endtime,es.subjectname
        ,truncate(a.pynum*100/a.tjnum,2)pgrate
        from(select p.esid,p.epid,group_concat(distinct concat(psq.qn,'_',psq.eptqid) order by psq.eptqid)qns
        ,group_concat(distinct pc.classid)classids
        ,count(case when psq.answered=1 then psq.epsqid else null end)tjnum
        ,count(case when psq.answered=1 and psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper p left join e_paper_correctclass pc on p.epid = pc.epid
        left join e_paper_student_question psq on psq.classid=pc.classid and psq.epid=p.epid
        where pc.teacherid=#{teacherid} and psq.ctype in(3,13,14,15) group by p.epid
        union
        select p.esid,p.epid,group_concat(distinct concat(pq.qn,'_',pq.eptqid) order by pq.eptqid)qns
        ,'' classids
        ,count(case when psq.answered=1 then psq.epsqid else null end)tjnum
        ,count(case when psq.answered=1 and psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper p left join e_paper_correctquestion pq on p.epid = pq.epid
        left join e_paper_student_question psq on psq.eptqid=pq.eptqid
        where pq.teacherid=#{teacherid} group by p.epid)a left join e_subject es on a.esid = es.esid
        left join e_base eb on es.examid = eb.examid
        where a.tjnum!=a.pynum and eb.deleted=1
    </select>
    <!--已阅卷列表-->
    <select id="listMarked" resultType="java.util.Map">
        select a.*,eb.examname,eb.gradeid,eb.examtype,eb.examstate,eb.hashb,eb.schoolid,es.sdate,es.begintime,es.endtime,es.subjectname
        ,100 pgrate
        from(select p.esid,p.epid,group_concat(distinct concat(psq.qn,'_',psq.eptqid) order by psq.eptqid)qns
        ,group_concat(distinct pc.classid)classids
        ,count(case when psq.answered=1 then psq.epsqid else null end)tjnum
        ,count(case when psq.answered=1 and psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper p left join e_paper_correctclass pc on p.epid = pc.epid
        left join e_paper_student_question psq on psq.classid=pc.classid and psq.epid=p.epid
        where pc.teacherid=#{teacherid} and psq.ctype in(3,13,14,15) group by p.epid
        union
        select p.esid,p.epid,group_concat(distinct concat(pq.qn,'_',pq.eptqid) order by pq.eptqid)qns
        ,'' classids
        ,count(case when psq.answered=1 then psq.epsqid else null end)tjnum
        ,count(case when psq.answered=1 and psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper p left join e_paper_correctquestion pq on p.epid = pq.epid
        left join e_paper_student_question psq on psq.eptqid=pq.eptqid
        where pq.teacherid=#{teacherid} group by p.epid)a left join e_subject es on a.esid = es.esid
        left join e_base eb on es.examid = eb.examid
        where a.tjnum>0 and a.tjnum=a.pynum and eb.deleted=1
        order by a.epid desc
    </select>
    <!--随机获取一条待批阅试题-->
    <select id="getCorrectQuestion" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        <if test="epsqid!=null and epsqid!=0 and checked==2">
        select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
        ,psq.answertype,psq.stuanswer,psq.stuanswertxt,psq.checked
        from e_paper_student_question psq left join e_paper_student_question psq2 on psq.eptqid=psq2.eptqid and psq.checkid=psq2.checkid
        where psq.checked=2 and psq.checkid=#{teacherid} and psq2.epsqid=#{epsqid}
        and psq.checktime >= psq2.checktime and psq.epsqid!=#{epsqid}
        order by psq.checktime,psq.epsqid limit 1
        </if>
        <if test="(epsqid==null or epsqid==0) and checked==2">
            select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
            ,psq.answertype,psq.stuanswer,psq.stuanswertxt,psq.checked
            from e_paper_student_question psq
            where psq.checked=2 and psq.checkid=#{teacherid} and psq.eptqid=#{eptqid}
            order by psq.checktime,psq.epsqid limit 1
        </if>
        <if test="(epsqid==null or epsqid==0) and checked==1">
        select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
        ,psq.answertype,psq.stuanswer,psq.stuanswertxt,psq.checked
        from e_paper_student_question psq where psq.eptqid=#{eptqid}
        and psq.answered=1 and (psq.checked=0 or (psq.checked=1 and psq.checkid=#{teacherid}))
        <if test="classids!=null and classids!=''">
            and psq.classid in(${classids})
        </if>
        order by psq.eptqid,psq.checked desc,rand() limit 1
        </if>
    </select>
    <!--试题批阅情况-->
    <select id="getCheckQuestion" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        select count(psq.epsqid)tjnum,count(case when psq.checked=2 then psq.epsqid else null end)pynum
        ,count(case when psq.checked=2 and psq.checkid=#{teacherid} then psq.epsqid else null end)wpynum
        from e_paper_student_question psq where psq.eptqid=#{eptqid} and psq.answered=1
        <if test="classids!=null and classids!=''">
            and psq.classid in(${classids})
        </if>
    </select>
    <!--修改试卷学生试题为批阅中-->
    <update id="updateCheckQuestionById">
        update e_paper_student_question set checked=1,checkid=#{teacherid},checktime=#{timestamp}
        where epsqid=#{epsqid}
    </update>
    <!--获取上一份批阅试题-->
    <select id="getPreQuestion" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
        ,psq.answertype,psq.stuanswer,psq.stuanswertxt,psq.checktime,psq.checked
        from e_paper_student_question psq where psq.eptqid=#{eptqid} and psq.checked=2 and psq.checkid=#{teacherid}
        order by psq.checktime desc,psq.epsqid desc limit 1
    </select>
    <!--阅卷记录-->
    <select id="listRecord" resultType="java.util.Map">
        select psq.epsqid,psq.stuanswer,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.checktime,psq.checked
        ,psq.qn,psq.qorder,psq.answertype,psq.stuanswertxt,psq.epsid
        from e_paper_student_question psq where psq.eptqid=#{eptqid}
        and psq.checked=2 and psq.checkid=#{teacherid}
        order by psq.checktime desc,psq.epsqid desc
    </select>
    <!--批阅试题-->
    <update id="correctQuestion">
        update e_paper_student_question
        set stuscore=#{psq.stuscore},checked=2
        ,checkid=#{psq.checkid},checktime=#{psq.checktime}
        ,good=#{psq.good},bad=#{psq.bad}
        <if test="psq.stuanswer!=null and psq.stuanswer!=''">
            ,stuanswer=#{psq.stuanswer}
        </if>
        where epsqid=#{psq.epsqid}
    </update>
    <!--学生未批改试题数量-->
    <select id="getNotCorrect" resultType="java.lang.Integer">
        select count(*) from e_paper_student_question where epsid=#{epsid} and checked!=2
    </select>
    <!--修改学生试卷批阅状态-->
    <update id="updateCheckStuById">
        update e_paper_student ps set ps.checked=2,ps.checkid=#{checkid}
        ,ps.stuscore=(select sum(psq.stuscore) from e_paper_student_question psq where psq.epsid=ps.epsid)
        where ps.epsid=#{epsid}
    </update>
    <!--根据考试科目id获取考试名称-->
    <select id="getExamNameByEsId" resultType="java.util.Map">
        select es.subjectname,eb.examname,eb.exammode
        from e_subject es left join e_base eb on eb.examid = es.examid where es.esid=#{esid}
    </select>
    <!--获取考试试卷-->
    <select id="listPaperByExamId" resultType="com.xhkjedu.sexam.vo.paper.EPaperVo">
        select ep.epid,ep.pnum,es.subjectid,es.subjectname,ep.correcttype
        ,(case when ep.correcttype=1
        then (select concat('[',group_concat(json_object('classid',epc.classid,'teacherid',epc.teacherid)),']')
        from e_paper_correctclass epc where epc.epid=ep.epid)
        else (select concat('[',group_concat(json_object('eptqid',epc.eptqid,'teacherid',epc.teacherid)),']')
        from e_paper_correctquestion epc where epc.epid=ep.epid) end)correctJson
        from e_paper ep left join e_subject es on ep.esid = es.esid
        where ep.examid=#{examid}
    </select>
    <!--获取考试学生试题-->
    <select id="listStuQuestionByEpids" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select studentid,epid,eptqid,qn,answered,checked,checkid,ctype,qorder
        from e_paper_student_question where epid in(${epids})
    </select>
    <!--获取试卷试题题号-->
    <select id="listQnByEpId" resultType="java.util.Map">
        select q.qn,q.eptqid
        from e_paper_qtype_question q where q.epid=#{epid}
    </select>
    <!--获取已批阅试题-->
    <select id="getCheckedQuestion" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
        ,psq.answertype,psq.stuanswer,psq.stuanswertxt,psq.checked,psq.checktime
        from e_paper_student_question psq left join
        e_paper_student_question psq2 on psq.eptqid=psq2.eptqid and psq.checkid=psq2.checkid
        where psq.checked=2 and psq.checkid=#{teacherid} and psq2.epsqid=#{epsqid}
        and psq.checktime &lt;= psq2.checktime and psq.epsqid!=#{epsqid}
        order by psq.checktime desc,psq.epsqid limit 1
    </select>
    <!--随机获取一条待批阅试题-->
    <select id="getCorrectQuestion2" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
        ,psq.answertype,psq.stuanswer,psq.stuanswertxt,psq.checked
        from e_paper_student_question psq where psq.eptqid in
        <foreach collection="eptqids" item="eptqid" open="(" separator="," close=")">
            ${eptqid}
        </foreach>
        and psq.answered=1 and (psq.checked=0 or (psq.checked=1 and psq.checkid=#{teacherid}))
        <if test="classids!=null and classids!=''">
            and psq.classid in(${classids})
        </if>
        order by psq.eptqid,psq.checked desc,rand() limit 1
    </select>
    <!--获取第一条批改试题-->
    <select id="getCheckedQuestion2" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        select psq.epsqid,psq.qn,psq.eptqid,psq.qscore,psq.stuscore,psq.good,psq.bad,psq.epsid
        ,psq.answertype,psq.stuanswer,psq.stuanswertxt,checked,psq.checktime
        from e_paper_student_question psq where psq.eptqid in
        <foreach collection="eptqids" item="eptqid" open="(" separator="," close=")">
            ${eptqid}
        </foreach>
        and psq.checked=2 and psq.checkid=#{teacherid}
        order by psq.checktime asc,psq.epsqid limit 1
    </select>
    <!--取消试题锁定-->
    <update id="unLockAllQuestion">
        update e_paper_student_question set checked=0,checkid=null,checktime=null
        where checked=1 and checkid=#{teacherid}
    </update>
    <!--试题锁定状态超过30分钟后自动取消-->
    <update id="unLockQuestion">
        update e_paper_student_question set checked=0,checkid=null,checktime=null
        where checked=1 and checktime!=0 and #{timestamp}-checktime>1800
    </update>
    <!--试卷批阅进度-->
    <select id="getPaperCheck" resultType="com.xhkjedu.sexam.vo.paper.EPaperSQuestionVo">
        select count(psq.epsqid)tjnum,count(case when psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper_student_question psq where psq.eptqid in
        <foreach collection="psq.eptqids" item="eptqid" open="(" separator="," close=")">
            ${eptqid}
        </foreach>
        and psq.answered=1
        <if test="psq.classids!=null and psq.classids!=''">
            and psq.classid in(${psq.classids})
        </if>
    </select>
</mapper>