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

    <!--根据学生试卷ID清空作答及批阅信息-->
    <update id="updateStudentBlockQuestionByMpsid">
        update ms_paper_student_block b join ms_paper_student_question q
        on b.mpsid=q.mpsid and b.mblockid=q.mblockid
        set b.stuanswer=null,b.converted=0,b.checked=0,b.hasgood=0,b.hasbad=0,b.firstcid=null,b.firstcime=null,b.firstccore=0,
            b.secondcid=null,b.secondctime=null,b.secondcscore=0,b.arbitrateid=null,b.arbitratetime=null,b.arbitratescore=0,
            b.hasproblem=0,b.problemtype=0,b.problemcomm=null,b.hasfinal=0,b.finalreason=0,
            q.answered=0,q.answertype=0,q.stuanswer=null,q.converted=0,q.answertime=0,q.stuscore=0,
            q.checked=0,q.firstcid=null,q.firstcime=null,q.firstccore=0,q.secondcid=null,q.secondctime=null,q.secondcscore=0,
            q.arbitrateid=null,q.arbitratetime=null,q.arbitratescore=0,q.hasgood=0,
            q.hasbad=0,q.hasproblem=0,q.problemtype=0,q.problemcomm=null
        where b.mpsid = #{mpsid}
    </update>

    <!--获取学生题块试题-->
    <resultMap id="stuBlockQuestions" type="com.xhkjedu.smarking.model.stupaper.MsPaperStudentBlock">
        <result property="mpsbid" column="mpsbid"/>
        <result property="mpsid" column="mpsid"/>
        <result property="mpid" column="mpid"/>
        <result property="mblockid" column="mblockid"/>
        <result property="stuanswer" column="stuanswer"/>
        <result property="blocktype" column="blocktype"/>
        <collection property="questions" ofType="com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion" javaType="java.util.List">
            <result property="mpsqid" column="mpsqmid"/>
            <result property="mptqid" column="mptqid"/>
            <result property="mblockid" column="mblockid"/>
            <result property="qn" column="qn"/>
            <result property="qorder" column="qorder"/>
            <result property="qscore" column="qscore"/>
            <result property="ctype" column="ctype"/>
            <result property="qanswer" column="qanswer"/>
            <result property="loseoption" column="loseoption"/>
            <result property="scoreset" column="scoreset"/>
            <result property="hasanswer" column="answered"/>

        </collection>
    </resultMap>
    <select id="listPaperStuBlockQuestions" resultMap="stuBlockQuestions">
        select distinct b.mpsbid,b.mpsid,b.mpid,b.mblockid,b.stuanswer,pb.blocktype,q.mpsqid,q.mptqid,q.qn,q.qorder,
                        q.qscore,q.ctype,tq.qanswer,tq.loseoption,tq.scoreset,p.answered
        from ms_paper_student_question q left join ms_paper_student_block b
        on b.mpsid=q.mpsid and b.mblockid=q.mblockid
        left join ms_paper_block pb on pb.mblockid=b.mblockid
        left join ms_paper_block_question pbq on q.mptqid=pbq.mptqid
        left join ms_paper_qtype_question tq on pbq.mptqid=tq.mptqid
        left join ms_paper p on p.mpid=b.mpid
        where b.mpsid = #{mpsid}
    </select>

    <!--批量更改学生题块-->
    <update id="updateBatchStuBlock">
        <foreach collection="list" separator=";" item="b">
            update ms_paper_student_block set stuanswer=#{b.stuanswer},converted=#{b.converted},checked=#{b.checked} where mpsbid=#{b.mpsbid}
        </foreach>
    </update>
    <!--批量更新批改人ID-->
    <update id="batchUpdateCheckId">
        update ms_paper_student_block
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="firstcid = case mpsbid" suffix="end,">
                <foreach collection="list" item="m" separator=" ">
                    when #{m.mpsbid} then ifnull(#{m.firstcid},firstcid)
                </foreach>
            </trim>
            <trim prefix="secondcid = case mpsbid" suffix="end,">
                <foreach collection="list" item="m" separator=" ">
                    when #{m.mpsbid} then ifnull(#{m.secondcid},secondcid)
                </foreach>
            </trim>
        </trim>
        where mpsbid in
        <foreach collection="list" item="m" open="(" separator="," close=")">
            #{m.mpsbid}
        </foreach>
    </update>
    <!--清空教师未阅卷任务-->
    <update id="updateByMpsbidsAndTeacherid">
        update ms_paper_student_block
        set firstcid = if(firstcid=#{firstcid},null,firstcid)
          ,secondcid = (if(secondcid=#{secondcid},null,secondcid))
        where mpsbid in (${mpsbids})
    </update>

    <!--批量保存学生题块-->
    <insert id="insertBatchStudentBlock">
        insert into ms_paper_student_block(examid,mpid,subjectid,mblockid,studentid,mpsid,createtime) values
        <foreach collection="list" item="b" separator=",">
            (#{b.examid},#{b.mpid},#{b.subjectid},#{b.mblockid},#{b.studentid},#{b.mpsid},#{b.createtime})
        </foreach>
    </insert>

</mapper>
