<?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.paperstudent.EPaperStudentMapper">

    <!--学生进行中考试列表   abs(UNIX_TIMESTAMP(concat(es.sdate,' ',es.begintime))-UNIX_TIMESTAMP())-->
    <select id="listExamPapersForStudent" resultType="java.util.Map">
        select eps.epsid, es.esid,es.examid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.pstate,
        e.examname,e.gradeid,e.examtype,ep.epid,ep.ptype,eps.sstate
        from e_paper_student eps left join e_subject es on eps.esid=es.esid
         left join e_paper ep on eps.epid=ep.epid left join e_base e on eps.examid=e.examid
        where e.deleted=1 and e.exammode=1 and eps.studentid=#{studentid}
          and (e.examstate=1 or (e.examstate>1 and es.pstate!=2))
         GROUP BY eps.epsid
        order by FIELD(es.pstate,1,0,2),
                 abs(timestampdiff(MINUTE,concat(es.sdate,' ',es.begintime),#{nowtime})),es.esid desc
    </select>

    <resultMap id="examsEndForStu" type="java.util.Map">
        <result property="examid" column="examid"></result>
        <result property="examname" column="examname"></result>
        <result property="examdate" column="examdate"></result>
        <result property="gradeid" column="gradeid"></result>
        <result property="examtype" column="examtype"></result>
        <result property="exammode" column="exammode"></result>
        <result property="examstate" column="examstate"></result>
        <result property="examcomm" column="examcomm"></result>
        <collection property="subjects" ofType="java.util.Map" javaType="java.util.List"
                    select="listExamSubjects" column="{examid=examid,studentid=studentid}">
        </collection>
    </resultMap>
    <!--学生已结束考试列表-->
    <select id="listExamsEndForStudent" resultMap="examsEndForStu">
        select e.examid,e.examname,e.examdate,e.gradeid,e.examtype,e.exammode,e.examstate,e.examcomm,#{studentid} as studentid
        from e_base e left join e_paper_student eps on e.examid=eps.examid
        where e.deleted=1 and eps.studentid=#{studentid} and e.examstate>=2
        group by e.examid order by e.examdate desc
    </select>
    <select id="listExamSubjects" resultType="java.util.Map">
        select es.esid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,eps.epsid
             ,(select ep.ptype from e_paper ep where ep.esid=es.esid)ptype
        from e_subject es left join e_paper_student eps on es.esid=eps.esid
        where eps.studentid=#{studentid} and es.examid=#{examid} and es.pstate=2 order by es.begintime,es.esid
    </select>

    <!--获取考试科目时间信息-->
    <select id="getExamSubjectPaperByEpsid" resultType="java.util.Map">
        select ps.epsid,ps.classid,ps.examid,ps.epid,e.examname,e.examcomm,e.monitored,e.gradeid,es.esid,es.subjectid,es.subjectname,es.sdate,
               es.begintime,es.endtime,p.ptype,p.pnum,p.pscore,p.hearfile,p.hearnum,c.classname
        from e_paper_student ps left join e_base e on ps.examid=e.examid
        left join e_subject es on ps.esid=es.esid and ps.examid=es.examid
        left join e_paper p on ps.epid=p.epid
        left join e_class c on ps.classid=c.classid and ps.examid=c.examid
        where ps.epsid=#{epsid}
    </select>

    <!--考试试卷基本信息-->
    <select id="getExamSubjectByEpsid" resultType="com.xhkjedu.sexam.vo.paperstudent.EPaperStudentVo">
        select ps.epsid,ps.examid,ps.esid,ps.classid,ps.studentid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,
               e.examname,e.gradeid,ps.epid,p.ptype,p.pnum,p.pscore,p.hearfile,p.hearnum,s.classname
        from e_paper_student ps left join e_subject es on ps.esid=es.esid
        left join e_base e on ps.examid=e.examid left join e_paper p on ps.epid=p.epid
        left join t_class s on ps.classid=s.classid
        where ps.epsid=#{epsid}
    </select>
    <!--学生试卷题库（android）-->
    <select id="listStuPaperQuestionsForAndroid" resultType="java.util.Map">
        select distinct psq.epsqid,psq.questionid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,
        psq.stuanswertxt,psq.eptqid,pq.score,q.qstem,q.qoption,q.ctype,q.hashear,q.hearfile,q.qtypeid,
        q.qtypename,q.qlevel,q.snum,q.sorder,q.questionpid,mq.questionid mquestionid,mq.qstem mqstem,
        mq.qoption mqoption,mq.ctype mctype,mq.qtypeid mqtypeid,mq.qtypename mqtypename,mq.hashear mhashear,mq.hearfile mhearfile
        from e_paper_student_question psq left join e_paper_qtype_question pq on psq.eptqid=pq.eptqid
        left join t_question q on psq.questionid=q.questionid
        left join t_question mq on q.questionpid=mq.questionid
        where psq.epsid=#{epsid} order by psq.qorder,q.sorder
    </select>
    <!--学生试卷附件（android）-->
    <select id="listStuPaperFjQuestionsForAndroid" resultType="java.util.Map">
        select distinct psq.epsqid,psq.questionid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,
        psq.stuanswertxt,psq.eptqid,pq.score,pq.optionnum,pq.ctype,pq.qtypeid,pq.qtypename
        from e_paper_student_question psq left join e_paper_qtype_question pq on psq.eptqid=pq.eptqid
        where psq.epsid=#{epsid} order by psq.qorder
    </select>

    <resultMap id="qtypesQuestion" type="java.util.Map">
        <result column="eptid" property="eptid"></result>
        <result column="ctype" property="ctype"></result>
        <result column="eptname" property="eptname"></result>
        <result column="eptnum" property="eptnum"></result>
        <result column="eptorder" property="eptorder"></result>
        <result column="eptscore" property="eptscore"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.List">
            <result column="epsqid" property="epsqid"></result>
            <result column="questionid" property="questionid"></result>
            <result column="qscore" property="qscore"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="answered" property="answered"></result>
            <result column="answertype" property="answertype"></result>
            <result column="stuanswer" property="stuanswer"></result>
            <result column="stuanswertxt" property="stuanswertxt"></result>
            <result column="eptqid" property="eptqid"></result>
            <result column="good" property="good"></result>
            <result column="bad" property="bad"></result>
            <result column="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="qctype" property="ctype"></result>
            <result column="qstem" property="qstem"></result>
            <result column="hearfile" property="hearfile"></result>
            <result column="qoption" property="qoption"></result>
            <result column="answer" property="answer"></result>
            <result column="stuscore" property="stuscore"></result>
            <result column="qanalyze" property="qanalyze"></result>
            <result column="qlevel" property="qlevel"></result>
            <result column="sorder" property="sorder"></result>
            <result column="snum" property="snum"></result>
            <result column="questionpid" property="questionpid"></result>
            <result column="mquestionid" property="mquestionid"></result>
            <result column="mqstem" property="mqstem"></result>
            <result column="mqtypeid" property="mqtypeid"></result>
            <result column="mqtypename" property="mqtypename"></result>
            <result column="mctype" property="mctype"></result>
            <result column="mhearfile" property="mhearfile"></result>
        </collection>
    </resultMap>
    <!--试题详情-->
    <!--学生试卷题库（web）-->
    <select id="listStuPaperQuestionsForWeb" resultMap="qtypesQuestion">
        select pt.eptid,pt.ctype,pt.eptname,pt.eptnum,pt.eptorder,pt.eptscore,
               psq.epsqid,psq.questionid,psq.qorder,psq.qn,psq.qscore,psq.answered,psq.answertype,psq.stuanswer,
               psq.stuanswertxt,psq.eptqid,q.qstem,q.qoption,q.ctype qctype,q.hearfile,q.qtypeid,
               q.qtypename,q.qlevel,q.snum,q.sorder,q.questionpid,mq.questionid mquestionid,mq.qstem mqstem,
               mq.qoption mqoption,mq.ctype mctype,mq.qtypeid mqtypeid,mq.qtypename mqtypename,mq.hearfile mhearfile
        from e_paper_student_question psq left join e_paper_qtype_question ptq on psq.eptqid=ptq.eptqid
        left join e_paper_qtype pt on pt.eptid=ptq.eptid
        left join t_question q on psq.questionid=q.questionid
        left join t_question mq on q.questionpid=mq.questionid
        where psq.epsid=#{epsid} order by pt.eptorder,psq.qorder,q.sorder
    </select>
    <!--学生试卷题库（web）-->
    <select id="listStuPaperQuestionsForBg" resultMap="qtypesQuestion">
        select pt.eptid,pt.ctype,pt.eptname,pt.eptnum,pt.eptorder,pt.eptscore,ptq.answer,psq.stuscore,
               psq.epsqid,psq.questionid,psq.qorder,psq.qn,psq.qscore,psq.answered,psq.answertype,psq.stuanswer,
               psq.stuanswertxt,psq.eptqid,psq.good,psq.bad,q.qstem,q.qoption,q.ctype qctype,q.hearfile,q.qtypeid,
               q.qtypename,q.qlevel,q.snum,q.sorder,q.questionpid,mq.questionid mquestionid,mq.qstem mqstem,
               mq.qoption mqoption,mq.ctype mctype,mq.qtypeid mqtypeid,mq.qtypename mqtypename,mq.hearfile mhearfile
        from e_paper_student_question psq left join e_paper_qtype_question ptq on psq.eptqid=ptq.eptqid
        left join e_paper_qtype pt on pt.eptid=ptq.eptid
        left join t_question q on psq.questionid=q.questionid
        left join t_question mq on q.questionpid=mq.questionid
        where psq.epsid=#{epsid} order by pt.eptorder,psq.qorder,q.sorder
    </select>
    <select id="listStuPaperFjQuestionsForBg" resultMap="fjtypeQuestions">
        select pt.eptid,pt.ctype,pt.eptname,pt.eptnum,pt.eptorder,pt.eptscore,pq.answer,psq.stuscore,
               psq.epsqid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,
               psq.stuanswertxt,psq.eptqid,psq.good,psq.bad,psq.qscore score,pq.optionnum,pq.ctype qctype,pq.qtypeid,pq.qtypename
        from e_paper_student_question psq left join e_paper_qtype_question pq on psq.eptqid=pq.eptqid
        left join e_paper_qtype pt on pq.eptid=pt.eptid
        where psq.epsid=#{epsid} order by pt.eptorder, psq.qorder
    </select>

    <resultMap id="fjtypeQuestions" type="com.xhkjedu.sexam.vo.paper.EPaperQtypeVo">
        <result column="eptid" property="eptid"></result>
        <result column="ctype" property="ctype"></result>
        <result column="eptname" property="eptname"></result>
        <result column="eptnum" property="eptnum"></result>
        <result column="eptorder" property="eptorder"></result>
        <result column="eptscore" property="eptscore"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.Map">
            <result column="answer" property="answer"></result>
            <result column="stuscore" property="stuscore"></result>
            <result column="epsqid" property="epsqid"></result>
            <result column="score" property="score"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="eptqid" property="eptqid"></result>
            <result column="good" property="good"></result>
            <result column="bad" property="bad"></result>
            <result column="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="qctype" property="ctype"></result>
            <result column="optionnum" property="optionnum"></result>
            <result column="answered" property="answered"></result>
            <result column="answertype" property="answertype"></result>
            <result column="stuanswer" property="stuanswer"></result>
            <result column="stuanswertxt" property="stuanswertxt"></result>
        </collection>
    </resultMap>
    <!--学生试卷附件（web）-->
    <select id="listStuPaperFjQuestionsForWeb" resultMap="fjtypeQuestions">
        select pt.eptid,pt.ctype,pt.eptname,pt.eptnum,pt.eptorder,pt.eptscore,
               psq.epsqid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,
               psq.stuanswertxt,psq.eptqid,psq.qscore score,pq.optionnum,pq.ctype qctype,pq.qtypeid,pq.qtypename
        from e_paper_student_question psq left join e_paper_qtype_question pq on psq.eptqid=pq.eptqid
        left join e_paper_qtype pt on pq.eptid=pt.eptid
        where psq.epsid=#{epsid} order by pt.eptorder, psq.qorder
    </select>

    <!--学生提交试卷-->
    <update id="updateStudentPaper">
        update e_paper_student set endtime=#{ps.endtime},costtime=#{ps.costtime}
         ,sstate=#{ps.sstate},checked=#{ps.checked},stupic=#{ps.stupic}
         ,stuscore=(SELECT SUM(IFNULL(q.stuscore,0)) FROM e_paper_student_question q WHERE q.epsid=#{ps.epsid})
         <if test="ps.costtime==null and ps.costtime==0">
             ,costtime=(SELECT SUM(IFNULL(q.costtime,0)) FROM e_paper_student_question q WHERE q.epsid=#{ps.epsid})
         </if>
        <if test="ps.costtime!=null">
            ,costtime=#{ps.costtime}
        </if>
        <if test="ps.submittype!=null and ps.submittype!='0'">
            ,submittype = #{ps.submittype}
        </if>
        <if test="ps.device!=null and ps.device!=''">
            ,device = #{ps.device}
        </if>
        where epsid=#{ps.epsid}
    </update>
    <!--更新缺考状态-->
    <update id="updateStudentPaperState">
        update e_paper_student set sstate=#{ps.sstate},checked=#{ps.checked},stupic=#{ps.stupic}
        <if test="ps.ps.hasbad!=null and ps.hasbad!=0">
            ,hasbad=#{ps.hasbad}
        </if>
        where epsid=#{ps.epsid}
    </update>

    <!--批量更新学生试卷分值-->
    <update id="updateBathStudentPaperScoreByEpsid">
        <foreach collection="epsids" item="epsid" separator=";">
            update e_paper_student ps set ps.stuscore=(SELECT SUM(IFNULL(q.stuscore,0)) FROM e_paper_student_question q
            WHERE q.epid=#{epid} and q.epsid=#{epsid}) where ps.epid=#{epid} and ps.epsid=#{epsid}
        </foreach>
    </update>

    <!--根据试卷id和学生id获取学生试卷信息-->
    <select id="getPaperStudentByStuidAndEpid" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudent">
        select * from e_paper_student where epid=#{epid} and studentid=#{studentid} order by epsid desc limit 1
    </select>

    <select id="listByExamId" resultType="com.xhkjedu.sexam.model.reportstu.ERstudent">
        select classid,examid,subjectid,studentid,stuscore score,sstate
        from e_paper_student where examid=#{examid}
        <foreach collection="epids" item="epid" open="and epid in(" separator="," close=")">
            ${epid}
        </foreach>
    </select>
    <!--考试试卷学生-->
    <select id="listByExamAndSubjectId" resultType="com.xhkjedu.sexam.model.reportstu.ERstudent">
        select classid,examid,subjectid,studentid,stuscore score,sstate
        from e_paper_student where examid=#{examid}
        <if test="subjectid!='zf'">
            and subjectid=#{subjectid}
        </if>
    </select>

    <!--更改单个学生试卷分值-->
    <update id="updateStuPaperscoreEpsid">
        update e_paper_student ps set ps.stuscore=(SELECT SUM(IFNULL(q.stuscore,0)) FROM e_paper_student_question q
          WHERE q.epsid=#{epsid} ) where ps.epsid=#{epsid}
    </update>

    <!--获取学生试卷基础信息和得分-->
    <select id="getStudentPaperScore" resultType="java.util.Map">
        SELECT ps.epsid,ps.epid,ps.studentid,u.username stuname,u.loginname,ps.stuscore,s.subjectid,s.subjectname,
        e.examname,e.exammode,e.gradeid,c.classname,p.ptype,p.hasfile,p.hearfile,p.hearnum
        from e_paper_student ps left join e_subject s on ps.esid=s.esid
        left join e_paper p on ps.epid = p.epid
        left join e_base e on ps.examid=e.examid
        left join t_user u on ps.studentid=u.userid
        left join e_class_student cs on cs.studentid=ps.studentid
        left join e_class c on cs.examid=c.examid and cs.classid=c.classid
        where ps.epsid=#{epsid} group by ps.studentid
    </select>

    <!--搜索考试学生获取学生试卷id-->
    <select id="getStuSubjectPaperByname" resultType="java.util.Map" parameterType="com.xhkjedu.sexam.model.paperstudent.EPaperStudent">
        select ps.epsid, ps.studentid,u.username stuname,u.examno from e_paper_student ps
        left join t_user u on ps.studentid=u.userid
        where ps.examid=#{ps.examid} and ps.subjectid=#{ps.subjectid}
        <if test="ps.classid!=null and ps.classid!='0'">
            and ps.classid=#{ps.classid}
        </if>
        and u.username like concat('%',#{ps.stuname,jdbcType=VARCHAR},'%')
        group by ps.studentid
    </select>
    <!--修改学生试卷状态-->
    <update id="updateSstate">
        update e_paper_student set sstate=#{sstate} where epsid=#{epsid}
    </update>
    <!--学生试卷（扫描）-->
    <select id="listStuPaperQuestionsForSca" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select epsqid,answered,eptqid,stuanswer from e_paper_student_question where epsid=#{epsid}
    </select>
    <!--更新学生试卷扫描信息-->
    <update id="updateScaStudentPaper">
        update e_paper_student
        set stupic=#{ps.stupic}
          , sstate=#{ps.sstate}
          , stuscore=#{ps.stuscore}
          , batchid=#{ps.batchid}
          , hasbad=#{ps.hasbad}
          , checked=#{ps.checked}
        ,pagenum=#{ps.pagenum}
        where epsid = #{ps.epsid}
    </update>
    <!--批量更新试卷学生状态-->
    <update id="batchUpdateSstate">
        update e_paper_student set sstate=#{sstate} where epsid in
        <foreach collection="epsids" item="epsid" open="(" separator="," close=")">
            ${epsid}
        </foreach>
    </update>
    <!--批量更新试卷学生试卷状态-->
    <update id="batchUpdatePStuState">
        update e_paper_student
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="sstate = case" suffix="end,">
                <foreach collection="list" item="q">
                    when epsid=#{q.epsid} then #{q.sstate}
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                epsid=#{q.epsid}
            </foreach>
        </where>
    </update>
    <!--修改开始考试时间-->
    <update id="updateStuStartime">
        update e_paper_student set starttime=#{starttime},sstate=1 where epsid=#{epsid}
    </update>
    <!--根据试卷学生id获取考试状态-->
    <select id="getExamStateByEpsId" resultType="java.lang.Integer">
        select examstate from e_paper_student eps left join e_base b on b.examid = eps.examid where eps.epsid=#{epsid}
    </select>
    <!--更新未已提交未批阅完成学生得分-->
    <update id="updateStuscoreByExamId">
        update e_paper_student eps
        set eps.stuscore=(select sum(epsq.stuscore) from e_paper_student_question epsq where epsq.epsid=eps.epsid)
        where eps.examid=#{examid} and eps.sstate!=0 and eps.checked=0
    </update>

    <!--根据试卷学生id获取考试状态和考试科目状态和时间-->
    <select id="getExamStateSubjectStateByEpsid" resultType="java.util.Map">
        select b.examstate,s.pstate,s.begintime,s.endtime,s.sdate from e_paper_student eps
        left join e_base b on b.examid = eps.examid left join e_subject s on eps.esid=s.esid
        where eps.epsid=#{epsid}
    </select>
    <!--根据试卷学生id获取考试科目状态-->
    <select id="getSubjectStateByEpsid" resultType="java.lang.Integer">
        select s.pstate from e_paper_student eps left join e_subject s on eps.esid=s.esid where eps.epsid=#{epsid}
    </select>
    <!--根据试卷学生id获取考试科目状态和学生试卷状态-->
    <select id="getSubjectStateAndSstageByEpsid" resultType="java.util.Map">
        select eps.sstate,s.pstate,s.esid,eps.studentid,s.sdate,s.begintime from e_paper_student eps left join e_subject s on eps.esid=s.esid where eps.epsid=#{epsid}
    </select>
    <!--缺考详情-->
    <select id="listQkDetail" resultType="java.util.Map">
        select ec.classid,ec.classname,ec.classnum
        ,count(distinct case when eps.sstate=3 then eps.epsid else null end)qknum
        ,ifnull(group_concat(case when eps.sstate=3 then eps.studentid else null end),'')userids
        ,truncate(count(distinct case when eps.sstate=3 then eps.epsid else null end)*100/count(*),2)qkrate
        from e_paper_student eps left join e_class ec on eps.examid=ec.examid and eps.classid=ec.classid
        left join t_class c on ec.classid=c.classid
        where eps.epid=#{epid} and ec.schoolid=#{schoolid} group by eps.classid order by c.classtype,c.classorder,ec.ecid
    </select>
    <!--当天进行中考试列表-->
    <select id="listExam" resultType="java.util.Map">
        SELECT b.examname,s.* FROM e_subject s LEFT JOIN e_base b ON s.examid=b.examid
        WHERE s.sdate=#{strtime} AND b.examstate>=1 and s.pstate=1
    </select>
    <!--更新当天进行中考试已提交的学生状态为已进场-->
    <update id="updateSstateByEsIds">
        UPDATE e_paper_student SET sstate=1 WHERE esid in(${esids}) AND sstate=2
    </update>
    <!--获取学生考试结束时间戳-->
    <select id="getEndTime" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudent">
        select concat(es.sdate,' ',es.endtime,':00')device,(right(#{studentid},2)+0)/100 score
        from e_subject es where es.esid=#{esid}
    </select>
    <!--更改单个学生试卷分值和状态-->
    <update id="updateStuScoreAndStateEpsid">
        update e_paper_student ps set ps.sstate=2
        ,ps.stuscore=(select sum(ifnull(q.stuscore,0)) from e_paper_student_question q where q.epsid=#{epsid} )
        where ps.epsid=#{epsid}
    </update>
</mapper>
