<?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.EPaperStudentQuestionMapper">

    <!--根据学生试题id获取试题正确答案-->
    <select id="getPaperQuestionAnswer" resultType="com.xhkjedu.sexam.vo.paperstudent.PsqAnswerVo">
        select psq.eptqid,psq.costtime,ptq.ctype,ptq.score,ptq.answer,p.answered,psq.epsid
        from e_paper_student_question psq left join e_paper_qtype_question ptq on psq.eptqid=ptq.eptqid
        left join e_paper p on psq.epid=p.epid where psq.epsqid=#{epsqid}
    </select>

    <!--提交单题-->
    <update id="updateStuAnswer">
        update e_paper_student_question set answered=#{q.answered},answertype=#{q.answertype},
        stuanswer=#{q.stuanswer},stuanswertxt=#{q.stuanswertxt},converted=#{q.converted},
        answertime=#{q.answertime},costtime=#{q.costtime},stuscore=#{q.stuscore},
        checked=#{q.checked},checkid=#{q.checkid},checktime=#{q.checktime} where epsqid=#{q.epsqid}
    </update>

    <!--处理学生试卷中未提交试题-->
    <update id="updateStuQuesitonNoCommit">
        UPDATE e_paper_student_question SET costtime=0,checked=2,stuscore=0,checktime=#{timestamp}
        WHERE epsid=#{epsid} AND answered=0
    </update>

    <!--学生作业未批改试题数量-->
    <select id="getStuQuestionNoCheckNum" resultType="java.lang.Integer">
        SELECT count(*) FROM e_paper_student_question WHERE epsid=#{epsid} AND checked!=2
    </select>

    <!--获取试卷中未合并图片-->
    <select id="listStuQuesitonStuAnswerPic" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select epsqid,stuanswer from e_paper_student_question
        where epsid=#{epsid} and answertype=1 and answered=1 and converted=0 order by qorder
    </select>
    <!--获取试卷中未合并图片-->
    <select id="listNoMergeUserAnswer" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select epsqid,stuanswer,examid from e_paper_student_question
        where answertype=1 and answered=1 and converted=0 order by qorder
    </select>
    <!--获取未合并图片考试模式列表-->
    <select id="listExamByIds" resultType="java.util.Map">
        select examid,exammode from e_base where examid in
        <foreach collection="examids" item="examid" open="(" separator="," close=")"> ${examid} </foreach>
    </select>

    <!--获取试卷中所有试题正确答案及分值-->
    <select id="listPaperQuestionsAnswer" resultType="com.xhkjedu.sexam.vo.paperstudent.PsqAnswerVo">
        select distinct psq.epsqid,psq.eptqid,psq.costtime,psq.qn,psq.qorder,ptq.ctype,ptq.score,ptq.answer,p.answered
        from e_paper_student_question psq left join e_paper_qtype_question ptq on psq.eptqid=ptq.eptqid
        left join e_paper p on psq.epid=p.epid where psq.epsid=#{epsid} order by ptq.qorder
    </select>

    <!--批量提交试题-->
    <update id="updateBatchStuQuestion">
        <foreach collection="list" item="q" separator=";">
            update e_paper_student_question set answered=#{q.answered},answertype=#{q.answertype},stuanswer=#{q.stuanswer}
            ,stuanswertxt=#{q.stuanswertxt},converted=#{q.converted},answertime=#{q.answertime},stuscore=#{q.stuscore}
            ,checked=#{q.checked},checktime=#{q.checktime} where epsqid=#{q.epsqid}
        </foreach>
    </update>

    <!--获取试卷中指定试题作答情况-->
    <select id="listPaperStudentQuestionsByeptqid" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select epsqid,eptqid,stuanswer,stuscore,epsid,ctype from e_paper_student_question
        where epid=#{epid} and answered=1 and answertype=1 and eptqid=#{eptqid} and needdeal=1
        order by epsqid limit #{limitnum}
    </select>

    <!--批量更新学生提交指定试题是否需要重新批改-->
    <update id="updateBatchStuQuesNeeddeal">
        update e_paper_student_question set needdeal=1 where epid=#{epid}
        and answered=1 and eptqid in
        <foreach collection="eptqids" item="eptqid" separator="," open="(" close=")">#{eptqid}</foreach>
    </update>

    <!--批量更新试卷中试题分值-->
    <update id="updateBatchStuQuestionScore">
        <foreach collection="list" item="q" separator=";">
            update e_paper_student_question set stuscore=#{q.stuscore},needdeal=0 where epsqid=#{q.epsqid}
        </foreach>
    </update>

    <!--报告生成-学生试卷-->
    <select id="listExamStudentsAnswer" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select q.epsqid,q.classid,q.studentid,q.epid,q.eptqid,q.ctype,q.qn,q.answertype,q.qscore,
        q.stuscore,q.good,q.bad,u.username stuname,pq.answer,s.stuscore as stupaperscore,
        (case q.ctype when q.ctype in (1,2,4,5,6) THEN q.stuanswer ELSE null end) as stuanswer
        from e_paper_student_question q left join e_paper_student s on s.epsid=q.epsid
        left join e_paper_qtype_question pq on q.eptqid=pq.eptqid
        left join t_user u on q.studentid=u.userid where s.examid=#{examid}
        group by q.epsqid order by pq.qorder,pq.eptqid
    </select>

    <!--学生作答试卷题库-->
    <resultMap id="qtypesQuestion" type="java.util.Map">
        <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="score" property="score"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="answer" property="answer"></result>
            <result column="stuanswer" property="stuanswer"></result>
            <result column="stuscore" property="stuscore"></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="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="qctype" property="ctype"></result>
            <result column="hashear" property="hashear"></result>
            <result column="hearfile" property="hearfile"></result>
            <result column="qoption" property="qoption"></result>
            <result column="qstem" property="qstem"></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="mhashear" property="mhashear"></result>
            <result column="mhearfile" property="mhearfile"></result>
        </collection>
    </resultMap>
    <select id="listQStuQuestionScore" resultMap="qtypesQuestion">
        select pt.ctype,pt.eptname,pt.eptnum,pt.eptorder,pt.eptscore,
               psq.epsqid,psq.questionid,psq.qorder,psq.qn,ptq.score,ptq.answer,psq.answered,psq.answertype,psq.stuanswer,
               psq.stuanswertxt,psq.eptqid,psq.stuscore,q.qstem,q.qoption,q.ctype qctype,q.qtypeid,q.hashear,q.hearfile,
               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 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>


    <!--学生作答得分试卷附件-->
    <resultMap id="fjtypeQuestions" type="java.util.Map">
        <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="score" property="score"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="eptqid" property="eptqid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="qctype" property="ctype"></result>
            <result column="optionnum" property="optionnum"></result>
            <result column="answer" property="answer"></result>
            <result column="stuscore" property="stuscore"></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>
    <select id="listFjStuQuestionScore" resultMap="fjtypeQuestions">
        select 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,psq.stuscore,pq.answer,pq.optionnum,pq.ctype qctype,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>

    <!--根据学生试卷id获取学生作答内容-->
    <select id="listStuQuestionsByEpsid" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select * from e_paper_student_question where epsid=#{epsid} order by epsqid
    </select>
    <!--/获取学生试题列表-->
    <select id="listQuestionByStu" resultType="com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion">
        select epsqid,eptqid,qn,stuscore,epsid
        from e_paper_student_question where studentid=#{studentid} and eptqid in
        <foreach collection="eptqids" item="eptqid" open="(" separator="," close=")">${eptqid}</foreach>
        order by epsqid
    </select>
    <!--批量更新学生试题得分-->
    <update id="updateBatchStuQuestionSore">
        update e_paper_student_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="stuscore = case" suffix="end,">
                <foreach collection="list" item="q">
                    when epsqid=#{q.epsqid} then #{q.stuscore}
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                epsqid=#{q.epsqid}
            </foreach>
        </where>
    </update>
</mapper>
