<?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.smarking.mapper.stupaper.MsPaperStudentQuestionMapper">
    <!--学生考试-单题正确答案及分值-->
    <select id="getPaperQuestionAnswer" resultType="com.xhkjedu.smarking.vo.stupaper.QuestionAnswerVo">
        select psq.mptqid,psq.costtime,ptq.ctype,ptq.qscore,ptq.qanswer,psq.mpsid,p.answered as hasanswer
        from ms_paper_student_question psq left join ms_paper_qtype_question ptq on psq.mptqid=ptq.mptqid
        left join ms_paper p on psq.mpid=p.mpid where psq.mpsqid=#{mpsqid}
    </select>
    <!--学生考试-提交单题-->
    <update id="updateStudentAnswer">
        update ms_paper_student_question set answered=#{q.answered},stuanswer=#{q.stuanswer},answertime=#{q.answertime},checked=#{q.checked}
        <if test="q.answertype!=null">
            ,answertype=#{q.answertype}
        </if>
        <if test="q.converted!=null and q.converted!=0">
            ,converted=#{q.converted}
        </if>
        <if test="q.costtime!=null">
            ,costtime=#{q.costtime}
        </if>
        <if test="q.firstcime!=null and q.firstcime!=0">
            ,firstcime=#{q.firstcime}
        </if>
        <if test="q.firstccore!=null">
            ,firstccore=#{q.firstccore}
        </if>
        where mpsqid=#{q.mpsqid}
    </update>
    <!--学生考试-提交单题组-试题组正确答案及分值-->
    <select id="listPaperQuestionAnswers" resultType="com.xhkjedu.smarking.vo.stupaper.QuestionAnswerVo">
        select psq.mptqid,psq.mpsqid,psq.costtime,ptq.ctype,ptq.qscore,ptq.qanswer,psq.mpsid,p.answered as hasanswer
        from ms_paper_student_question psq left join ms_paper_qtype_question ptq on psq.mptqid=ptq.mptqid
        left join ms_paper p on psq.mpid=p.mpid where psq.mpsid=#{mpsid}
    </select>
    <!--学生考试-提交单题组-->
    <update id="updateBatchStudentAnswer">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_student_question set answered=#{q.answered},stuanswer=#{q.stuanswer},answertime=#{q.answertime},checked=#{q.checked}
            <if test="q.answertype!=null">
                ,answertype=#{q.answertype}
            </if>
            <if test="q.converted!=null and q.converted!=0">
                ,converted=#{q.converted}
            </if>
            <if test="q.costtime!=null">
                ,costtime=#{q.costtime}
            </if>
            <if test="q.stuscore!=null and q.stuscore!=0">
                ,stuscore=#{q.stuscore}
            </if>
            where mpsqid=#{q.mpsqid}
        </foreach>
    </update>
    <!--学生考试-提交试卷-试卷中未合并图片-->
    <select id="listStuQuestionStuAnswerPic" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion">
        select mpsqid,stuanswer from ms_paper_student_question
        where mpsid=#{mpsid} and answertype=2 and answered=1 and converted=1 order by qorder
    </select>

    <!--学生考试-提交试卷-生成学生题块-试卷中所有试题-->
    <select id="listStuQuestionsByMpsid" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion">
        select * from ms_paper_student_question where mpsid=#{mpsid} order by qorder
    </select>
    <!--学生考试-提交试卷-修改未提交试题为已批阅-->
    <update id="updateStuQuestionsNoCommit">
        UPDATE ms_paper_student_question SET costtime=0,checked=3,stuscore=0,firstccore=0,firstcime=#{timestamp}
        WHERE mpsid=#{mpsid} AND answered=0 and checked=0
    </update>

    <!--试卷答案-客观题-批量更新学生提交指定试题是否需要重新批改-->
    <update id="updateBatchStuQuesNeeddeal">
        update ms_paper_student_question set needdeal=1 where mpid=#{mpid}
        and answered=1 and mptqid in
        <foreach collection="list" item="q" separator="," open="(" close=")">#{q.mptqid}</foreach>
    </update>

    <!--试卷答案-客观题-获取试卷中指定试题作答情况-->
    <select id="listPaperStudentQuestionsByMptqid" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion">
        select mpsqid,mptqid,stuanswer,stuscore,mpsid,ctype from ms_paper_student_question
        where mpid=#{mpid} and answered=1 and answertype=1 and mptqid=#{mptqid} and needdeal=1
        order by mpsqid limit #{limitnum}
    </select>
    <!--试卷答案-客观题-批量更新试卷中试题分值-->
    <update id="updateBatchStuQuestionScore">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_student_question set stuscore=#{q.stuscore},needdeal=0,checked=3,
            firstccore=#{q.stuscore},firstcime=#{q.firstcime} where mpsqid=#{q.mpsqid}
        </foreach>
    </update>
    <!--批量更新批改人ID-->
    <update id="batchUpdateCheckId">
        update ms_paper_student_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="firstcid = case mpsqid" suffix="end,">
                <foreach collection="list" item="m" separator=" ">
                    when #{m.mpsbid} then ifnull(#{m.firstcid},firstcid)
                </foreach>
            </trim>
            <trim prefix="secondcid = case mpsqid" suffix="end,">
                <foreach collection="list" item="m" separator=" ">
                    when #{m.mpsbid} then ifnull(#{m.secondcid},secondcid)
                </foreach>
            </trim>
        </trim>
        where mpsqid in
        <foreach collection="list" item="m" open="(" separator="," close=")">
            #{m.mpsbid}
        </foreach>
    </update>
    <!--扫描-保存识别学生答案-->
    <update id="updateBatchStuAnswerForScan">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_student_question set answered=#{q.answered},stuanswer=#{q.stuanswer},checked=#{q.checked}
            <if test="q.converted!=null">
                ,converted=#{q.converted}
            </if>
            <if test="q.answertime!=null and q.answertime!=0">
                ,answertime=#{q.answertime}
            </if>
            <if test="q.answertype!=null and q.answertype!=0">
                ,answertype=#{q.answertype}
            </if>
            <if test="q.costtime!=null">
                ,costtime=#{q.costtime}
            </if>
            <if test="q.stuscore!=null">
                ,stuscore=#{q.stuscore}
            </if>
            <if test="q.firstcime!=null and q.firstcime!=0">
                ,firstcime=#{q.firstcime}
            </if>
            <if test="q.firstccore!=null">
                ,firstccore=#{q.firstccore}
            </if>
            where mpsid=#{q.mpsid} and mblockid=#{q.mblockid}
            <if test="q.mptqid!=null and q.mptqid!=0">and mptqid=#{q.mptqid}</if>
        </foreach>
    </update>
    <!--更新学生单题批阅信息-->
    <update id="checkByMptqidAndStudentId">
        update ms_paper_student_question set
        <trim prefix="stuscore = case mptqid" suffix="end,">
            <foreach collection="list" item="q" separator=" ">
                when #{q.mptqid} then ifnull(#{q.stuscore},stuscore)
            </foreach>
        </trim>
        <if test="block.firstcime!=null and block.firstcime!=0">
            <trim prefix="firstccore = case mptqid" suffix="end,">
               <foreach collection="list" item="q" separator=" ">
                   when #{q.mptqid} then #{q.firstccore}
               </foreach>
            </trim>
            firstcid=#{block.firstcid},firstcime=#{block.firstcime}
        </if>
        <if test="block.secondctime!=null and block.secondctime!=0">
            <trim prefix="secondcscore = case mptqid" suffix="end,">
                <foreach collection="list" item="q" separator=" ">
                    when #{q.mptqid} then #{q.secondcscore}
                </foreach>
            </trim>
            secondcid=#{block.secondcid},secondctime=#{block.secondctime}
        </if>
        <if test="block.arbitrateid!=null and block.arbitrateid!=0">
            ,arbitrateid=#{block.arbitrateid}
        </if>
        <if test="block.arbitratetime!=null and block.arbitratetime!=0">
            ,arbitratetime=#{block.arbitratetime}
        </if>
        <if test="block.arbitratescore!=null and block.arbitratescore!=0">
            ,arbitratescore=#{block.arbitratescore}
        </if>
        <if test="block.arbitratestate!=null and block.arbitratestate!=0">
            ,arbitratestate=#{block.arbitratestate}
        </if>
        <if test="block.hasgood!=null and block.hasgood!=0">
           ,hasgood=#{block.hasgood}
        </if>
        <if test="block.hasbad!=null and block.hasbad!=0">
           ,hasbad=#{block.hasbad}
        </if>
        <if test="block.hasproblem!=null and block.hasproblem!=0">
           ,hasproblem=#{block.hasproblem}
        </if>
        <if test="block.problemtype!=null and block.problemtype!=0">
           ,problemtype=#{block.problemtype}
        </if>
        <if test="block.problemcomm!=null and block.problemcomm!=''">
           ,problemcomm=#{block.problemcomm}
        </if>
        <if test="block.probleid!=null and block.probleid!=0">
            ,probleid=#{block.probleid}
        </if>
        <if test="block.hasfinal!=null and block.hasfinal!=0">
           ,hasfinal=#{block.hasfinal}
        </if>
        <if test="block.finalreason!=null and block.finalreason!=0">
           ,finalreason=#{block.finalreason}
        </if>
        <if test="block.checked!=null and block.checked!=0">
            ,checked=#{block.checked}
        </if>
        <if test="block.stumerge!=null and block.stumerge!=''">
            ,stumerge=#{block.stumerge}
        </if>
        where mblockid=#{block.mblockid} and mpsid=#{block.mpsid} and mptqid in
        <foreach collection="list" item="q" separator="," open="(" close=")">#{q.mptqid}</foreach>
    </update>

    <!--扫描记录-查看原卷-客观题保存-获取学生答案-->
    <select id="listStuQuestionsByMspidAnbBlockid" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion">
        select q.mpsqid,q.mpsid,q.mblockid,q.answered,q.stuanswer,q.checked,q.qscore,q.stuscore,
        tq.loseoption,tq.scoreset,tq.qanswer,p.answered as hasanswer
        from ms_paper_student_question q left join ms_paper_student ps on q.mpsid=ps.mpsid
        left join ms_paper_qtype_question tq on q.mpid=tq.mpid and q.mptqid=tq.mptqid
        left join ms_paper p on q.mpid=p.mpid
        where ps.mspid=1 and q.mblockid=1 order by q.mptqid
    </select>

    <!--修改分数-批量修改分值-->
    <update id="updateBatchStuscore">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_student_question set stuscore=#{q.stuscore} where mpsqid=#{q.mpsqid}
        </foreach>
    </update>

    <!--学生试卷-题型试题、作答、得分情况（题库）-->
    <resultMap id="qtypesQuestion" type="java.util.Map">
        <result column="mptid" property="mptid"></result>
        <result column="mptname" property="mptname"></result>
        <result column="mptnum" property="mptnum"></result>
        <result column="mptorder" property="mptorder"></result>
        <result column="mptscore" property="mptscore"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.List">
            <result column="mpsqid" property="mpsqid"></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="qanswer" property="qanswer"></result>
            <result column="answered" property="answered"></result>
            <result column="answertype" property="answertype"></result>
            <result column="stuanswer" property="stuanswer"></result>
            <result column="mptqid" property="mptqid"></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>
    <!--学生试卷-题型试题、作答、得分情况（题库）-->
    <select id="listStuPaperQtypeQueAnswerForWebTk" resultMap="qtypesQuestion">
        select pt.mptid,pt.mptname,pt.mptnum,pt.mptorder,pt.mptscore,psq.mpsqid,psq.questionid,
        psq.qorder,psq.qn,psq.qscore,ptq.qanswer,psq.answered,psq.answertype,psq.stuanswer,psq.stuscore,
        psq.mptqid,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 ms_paper_student_question psq left join ms_paper_qtype_question ptq on psq.mptqid=ptq.mptqid
        left join ms_paper_qtype pt on pt.mptid=ptq.mptid
        left join t_question q on psq.questionid=q.questionid
        left join t_question mq on q.questionpid=mq.questionid
        where psq.mpsid=#{mpsid} order by pt.mptorder,psq.qorder,q.sorder
    </select>
    <!--学生试卷-题型试题、作答、得分情况（附件）-->
    <resultMap id="fjtypeQuestions" type="java.util.Map">
        <result column="mptid" property="mptid"></result>
        <result column="mptname" property="mptname"></result>
        <result column="mptnum" property="mptnum"></result>
        <result column="mptorder" property="mptorder"></result>
        <result column="mptscore" property="mptscore"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.Map">
            <result column="stuscore" property="stuscore"></result>
            <result column="mpsqid" property="mpsqid"></result>
            <result column="qscore" property="qscore"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="mptqid" property="mptqid"></result>
            <result column="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="ctype" 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>
        </collection>
    </resultMap>
    <!--学生试卷-题型试题、作答、得分情况（附件）-->
    <select id="listStuPaperQtypeQueAnswerForWebFj" resultMap="fjtypeQuestions">
        select pt.mptid,pt.mptname,pt.mptnum,pt.mptorder,pt.mptscore,
        psq.mpsqid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,psq.stuscore,
        psq.mptqid,psq.qscore,pq.optionnum,pq.ctype,pq.qtypeid,pq.qtypename,pq.qanswer
        from ms_paper_student_question psq left join ms_paper_qtype_question pq on psq.mptqid=pq.mptqid
        left join ms_paper_qtype pt on pq.mptid=pt.mptid
        where psq.mpsid=#{mpsid} order by pt.mptorder, psq.qorder
    </select>

    <!--定时任务-获取学生答案待合并试题-->
    <select id="listNoMergeStupic" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion">
        select mpsqid,stuanswer,examid from ms_paper_student_question
        where answered=1 and answertype=2 and converted=1 order by mpsqid
    </select>

    <!--批量保存学生试题-->
    <insert id="saveBatchStuPaperQuestion">
        insert into ms_paper_student_question(studentid,examid,subjectid,mpid,mpsid,mptqid,mblockid,ctype,questionid,qorder,qn,qscore) values
        <foreach collection="list" item="q" separator=",">
            (#{q.studentid},#{q.examid},#{q.subjectid},#{q.mpid},#{q.mpsid},#{q.mptqid},#{q.mblockid},#{q.ctype},#{q.questionid},#{q.qorder},#{q.qn},#{q.qscore})
        </foreach>
    </insert>

    <!--获取考试所有参考学生作答情况-->
    <select id="listAllStuQuesByExamid" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion">
        select sq.mpsqid,sq.studentid,sq.subjectid,sq.mpid,sq.mpsid,sq.mptqid,sq.ctype,sq.questionid,sq.qn,sq.qorder,
        sq.qscore,sq.answered,sq.answertype,sq.stuanswer,sq.stuscore,sq.stumerge,sq.hasgood,sq.hasbad
        from ms_paper_student_question sq
        left join ms_paper_student ps on sq.mpsid=ps.mpsid
        where sq.examid=#{examid} and ps.sstate=2
        <if test="subjectid!=null and subjectid!=''"> and sq.subjectid=#{subjectid} </if>
        order by sq.mpid,sq.studentid
    </select>
    <!--获取参与统计科目单题得分情况-->
    <resultMap id="subjectScore" type="com.xhkjedu.smarking.vo.paper.SQScoreVo">
        <result column="subjectid" property="subjectid"/>
        <collection property="questions" ofType="com.xhkjedu.smarking.vo.paper.SQuestionVo" javaType="java.util.List">
            <result column="mptqid" property="mptqid"/>
            <collection property="stuscores" ofType="java.lang.Double" javaType="java.util.List">
                <result column="stuscore" property="stuscore"/>
            </collection>
        </collection>
    </resultMap>
    <select id="listByMpIds" resultMap="subjectScore">
        select subjectid,mptqid,stuscore from ms_paper_student_question where mpid in
        <foreach collection="mpids" item="mpid" separator="," open="(" close=")">#{mpid}</foreach>
    </select>
</mapper>
