<?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.paper.MsPaperQtypeQuestionMapper">

    <!--保存主观题选项数量-->
    <update id="updateBatchQuestionOptionnum">
        update ms_paper_qtype_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="optionnum = case" suffix="end,">
                <foreach collection="list" item="q">
                    when mptqid=#{q.mptqid} then #{q.optionnum}
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                mptqid=#{q.mptqid}
            </foreach>
        </where>
    </update>

    <!--保存客观题答案-->
    <update id="updateBatchQuestionAnswer">
        update ms_paper_qtype_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="qanswer = case" suffix="end,">
                <foreach collection="list" item="q">
                    when mptqid=#{q.mptqid} then #{q.qanswer}
                </foreach>
            </trim>
            <trim prefix="loseoption = case" suffix="end,">
                <foreach collection="list" item="q">
                    when mptqid=#{q.mptqid} then #{q.loseoption}
                </foreach>
            </trim>
            <trim prefix="scoreset = case" suffix="end,">
                <foreach collection="list" item="q">
                    when mptqid=#{q.mptqid} then #{q.scoreset}
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                mptqid=#{q.mptqid}
            </foreach>
        </where>
    </update>
    <!--保存试题难易度-->
    <update id="updateBatchQuestionComplexity">
        update ms_paper_qtype_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="complexity = case" suffix="end,">
                <foreach collection="list" item="q">
                    when mptqid=#{q.mptqid} then #{q.complexity}
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                mptqid=#{q.mptqid}
            </foreach>
        </where>
    </update>

    <!--试卷中所有试题基础信息-->
    <select id="listQuestionsByMpid" resultType="com.xhkjedu.smarking.model.paper.MsPaperQtypeQuestion">
        select q.* from ms_paper_qtype_question q
        left join ms_paper_qtype t on q.mptid=t.mptid and q.mpid=t.mpid
        where q.mpid=#{mpid} order by t.mptorder,q.qorder
    </select>

    <!--清空试题难易度-->
    <update id="updateComplexityByMpid">
        update ms_paper_qtype_question set complexity=null where mpid=#{mpid}
    </update>

    <!--删除题型下试题-->
    <delete id="deleteByMptid">
        delete from ms_paper_qtype_question where mptid=#{mptid}
    </delete>

    <!--批量修改试题信息-线下-->
    <update id="updateBatchQuestionInfo">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_qtype_question set ctype=#{q.ctype},qtypename=#{q.qtypename},qn=#{q.qn},qscore=#{q.qscore},qorder=#{q.qorder}
            ,optionnum=#{q.optionnum},loseoption=#{q.loseoption},scoreset=#{q.scoreset} where mptqid=#{q.mptqid}
        </foreach>
    </update>
    <!--批量修改试题排序-线下-->
    <update id="updateBatchQuestionOrder">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_qtype_question set qorder=#{q.qorder} where mptqid=#{q.mptqid}
        </foreach>
    </update>
    <!--批量修改试题排序-移动-线下-->
    <update id="updateBatchMoveQuestionOrder">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_qtype_question set qorder=qorder + (#{q.qorder}) where mptqid=#{q.mptqid}
        </foreach>
    </update>

    <!--获取试卷中所有试题排序-->
    <select id="listQuesOrderByMpid" resultType="com.xhkjedu.smarking.model.paper.MsPaperQtypeQuestion">
        select q.mptqid,q.qn,q.qorder,q.qscore from ms_paper_qtype_question q
        left join ms_paper_qtype t on q.mptid=t.mptid and q.mpid=t.mpid
        where q.mpid=#{mpid} order by t.mptorder,q.qorder
    </select>

    <!--获取题型下试题数量-->
    <select id="getQuesnumByMptid" resultType="java.lang.Integer">
        select count(*) from ms_paper_qtype_question where mptid = #{mptid}
    </select>

    <!--试题移动-获取要交换位置的试题-->
    <select id="getQuestionForMoverOrder" resultType="com.xhkjedu.smarking.model.paper.MsPaperQtypeQuestion">
        select mptqid,qorder from ms_paper_qtype_question where mpid=#{p.mpid} and qorder=(
                (select qorder from ms_paper_qtype_question where mptqid=#{p.mptqid})+(#{p.movevalue}))
    </select>

    <!--获取试卷中所有试题-页面展示-->
    <select id="listQuestionsForShow" resultType="java.util.Map">
        select q.mptqid,q.qn,q.qnstr,q.qorder,q.qscore,q.pagenum,q.stemcoor,q.quecoor,t.mptqn,t.mptorder from ms_paper_qtype_question q
        left join ms_paper_qtype t on q.mptid=t.mptid and q.mpid=t.mpid
        where q.mpid=#{mpid} order by t.mptorder,q.qorder
    </select>

    <!--批量设置试题切割坐标-->
    <update id="updateBatchQuestionCoor">
        <foreach collection="list" item="q" separator=";">
            update ms_paper_qtype_question set pagenum=#{q.pagenum},stemcoor=#{q.stemcoor},quecoor=#{q.quecoor} where mptqid=#{q.mptqid}
        </foreach>
    </update>
    <!--清空试题切割坐标-->
    <update id="updateQuestionCoorByMpid">
        update ms_paper_qtype_question set pagenum=0,stemcoor=null,quecoor=null where mpid=#{mpid}
    </update>

    <!--试卷中所有试题-考试报告-->
    <select id="listQuestionsForReport" resultType="com.xhkjedu.smarking.model.paper.MsPaperQtypeQuestion">
        select q.mptqid,q.mptid,q.mpid,q.ctype,q.qtype,q.questionid,q.qtypeid,q.qtypename,q.qn,q.qnstr,q.questionid,q.questionpid,
        q.qscore,q.qorder,q.optionnum,q.qanswer,qt.mptqn,qt.mptnum,qt.mptscore
        from ms_paper_qtype_question q left join ms_paper_qtype qt on q.mptid=qt.mptid
        where q.mpid=#{mpid} order by qt.mptorder,q.qorder
    </select>
</mapper>
