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

    <!--根据试卷ID删除试卷结构-->
    <delete id="deleteByMpId">
        DELETE FROM ms_paper_qtype WHERE mpid = #{mpId}
    </delete>

    <!--试卷题型试题信息(题库)-->
    <resultMap id="qtypesQuestion" type="com.xhkjedu.smarking.vo.paper.MsPaperQtypeVo">
        <result column="mptid" property="mptid" />
        <result column="mpid" property="mpid" />
        <result column="mptqn" property="mptqn" />
        <result column="mptname" property="mptname" />
        <result column="mptnum" property="mptnum" />
        <result column="mptorder" property="mptorder" />
        <result column="mptctype" property="ctype" />
        <result column="mptscore" property="mptscore" />
        <collection property="questions" ofType="com.xhkjedu.smarking.vo.paper.MsPaperQtypeQuestionVo" javaType="java.util.List">
            <result column="mptqid" property="mptqid" />
            <result column="mptid" property="mptid" />
            <result column="questionid" property="questionid" />
            <result column="questionpid" property="questionpid" />
            <result column="qscore" property="qscore" />
            <result column="qorder" property="qorder" />
            <result column="qn" property="qn" />
            <result column="qnstr" property="qnstr" />
            <result column="qtypeid" property="qtypeid" />
            <result column="qtypename" property="qtypename" />
            <result column="answer" property="answer" />
            <result column="qctype" property="ctype" />
            <result column="qstem" property="qstem" />
            <result column="qstemtxt" property="qstemtxt" />
            <result column="qoption" property="qoption" />
            <result column="qanswer" property="qanswer" />
            <result column="qanalyze" property="qanalyze" />
            <result column="complexity" property="complexity" />
            <result column="hashear" property="hashear" />
            <result column="hearfile" property="hearfile" />
            <result column="qlevel" property="qlevel" />
            <result column="sorder" property="sorder" />
            <result column="snum" property="snum" />
            <result column="questionpid" property="questionpid" />
            <result column="mqstem" property="mqstem" />
            <result column="mcomplexity" property="mcomplexity" />
            <result column="mqtypeid" property="mqtypeid" />
            <result column="mqtypename" property="mqtypename" />
            <result column="mctype" property="mctype" />
            <result column="mqanswer" property="mqanswer" />
            <result column="mqanalyze" property="mqanalyze" />
            <result column="mqlevel" property="mqlevel" />
            <result column="mhashear" property="mhashear" />
            <result column="mhearfile" property="mhearfile" />
            <result column="loseoption" property="loseoption" />
            <result column="scoreset" property="scoreset" />
            <collection property="points" ofType="java.util.Map" javaType="java.util.List"
                        column="{questionid=questionid,qlevel=qlevel,questionpid=questionpid}" select="listQuestionPoints">
                <result column="pointid" property="pointid"></result>
                <result column="pointname" property="pointname"></result>
            </collection>
        </collection>
    </resultMap>
    <!--试题详情-->
    <select id="listPaperQtypeQuestions" resultMap="qtypesQuestion">
        select t.mptid,t.mpid,t.mptqn,t.mptname,t.mptnum,t.mptorder,t.ctype as mptctype ,t.mptscore,tq.mptqid,tq.questionid,tq.questionpid,tq.qanswer as answer,tq.qscore,tq.qn,
        tq.qnstr,tq.qorder,tq.loseoption,tq.scoreset,COALESCE(tq.complexity, q.complexity) as complexity,q.qtypeid,q.qtypename,q.ctype qctype,
        q.qstem,q.qstemtxt,q.qoption,q.qanswer,q.qanalyze,q.hashear,q.hearfile,q.qlevel,q.sorder,
        fq.snum,fq.qstem as mqstem,fq.complexity as mcomplexity,fq.qtypeid mqtypeid,fq.qtypename mqtypename,fq.ctype mctype,
        fq.qanalyze mqanalyze,fq.qanswer mqanswer,fq.qlevel mqlevel,fq.hashear mhashear,fq.hearfile mhearfile
        from ms_paper_qtype_question tq left join ms_paper_qtype t on t.mptid=tq.mptid
        left join t_question q on tq.questionid=q.questionid
        left JOIN t_question fq on tq.questionpid=fq.questionid
        where t.mpid=#{mpid} order by t.mptorder,tq.qorder,q.sorder
    </select>
    <!--试题关联知识点-->
    <select id="listQuestionPoints" resultType="java.util.Map">
        select qp.pointid,p.pointname from t_question_point qp
        left join t_point p on qp.pointid=p.pointid
        where
        <if test="qlevel==1">
            qp.questionid=#{questionid}
        </if>
        <if test="qlevel==3">
            qp.questionid=#{questionpid}
        </if>
        group by qp.pointid order by p.pointorder,p.pointid
    </select>

    <!--试卷题型试题信息（附件）-->
    <resultMap id="fjtypeQuestions" type="com.xhkjedu.smarking.vo.paper.MsPaperQtypeVo">
        <result column="mptid" property="mptid" />
        <result column="mpid" property="mpid" />
        <result column="mptqn" property="mptqn" />
        <result column="mptname" property="mptname" />
        <result column="mptnum" property="mptnum" />
        <result column="mptorder" property="mptorder" />
        <result column="mptscore" property="mptscore" />
        <result column="mptctype" property="ctype" />
        <collection property="questions" ofType="com.xhkjedu.smarking.vo.paper.MsPaperQtypeQuestionVo" javaType="java.util.List">
            <result column="mptqid" property="mptqid" />
            <result column="mptid" property="mptid" />
            <result column="qscore" property="qscore" />
            <result column="qorder" property="qorder" />
            <result column="qtypeid" property="qtypeid" />
            <result column="qtypename" property="qtypename" />
            <result column="ctype" property="ctype" />
            <result column="qn" property="qn" />
            <result column="qnstr" property="qnstr" />
            <result column="optionnum" property="optionnum" />
            <result column="qanswer" property="qanswer" />
            <result column="complexity" property="complexity" />
            <result column="loseoption" property="loseoption" />
            <result column="scoreset" property="scoreset" />
            <collection property="points" ofType="java.util.Map" javaType="java.util.List"
                        column="{mptqid=mptqid}" select="listQuestionPointsFj">
                <result column="pointid" property="pointid" />
                <result column="pointname" property="pointname" />
            </collection>
        </collection>
    </resultMap>
    <select id="listPaperQtypeQuesitonsForFj" resultMap="fjtypeQuestions">
        select  t.mptid,t.mpid,t.ctype as mptctype,t.mptqn,t.mptname,t.mptnum,t.mptorder,t.mptscore,q.mptqid,q.qtypeid,q.qtypename,
                q.ctype,q.qscore,q.qn,q.qnstr,q.qorder,q.optionnum,q.qanswer,q.optionnum,q.complexity,q.loseoption,q.scoreset
        from ms_paper_qtype_question q left join ms_paper_qtype t on t.mptid=q.mptid
        where t.mpid=#{mpid} order by t.mptorder,q.qorder
    </select>
    <!--试题关联知识点-->
    <select id="listQuestionPointsFj" resultType="java.util.Map">
        select distinct qp.pointid,qp.pointname from ms_paper_qtype_question_point qp
        where qp.mptqid=#{mptqid} order by qp.mporder,qp.createtime
    </select>

    <!--获取试卷所有主观题-->
    <resultMap id="subjectiveQuestions" type="java.util.Map">
        <result column="mptid" property="mptid" />
        <result column="mptqn" property="mptqn" />
        <result column="mptname" property="mptname" />
        <result column="mptnum" property="mptnum" />
        <result column="mptorder" property="mptorder" />
        <result column="mptscore" property="mptscore" />
        <collection property="questions" ofType="java.util.Map" javaType="java.util.List">
            <result column="mptqid" property="mptqid" />
            <result column="qscore" property="qscore" />
            <result column="qorder" property="qorder" />
            <result column="ctype" property="ctype" />
            <result column="qn" property="qn" />
            <result column="qnstr" property="qnstr" />
            <result column="qanswer" property="qanswer" />
            <result column="optionnum" property="optionnum" />
            <result column="loseoption" property="loseoption" />
            <result column="scoreset" property="scoreset" />
            <result column="sorder" property="sorder" />
        </collection>
    </resultMap>
    <select id="listSubjectiveQuestionsFj" resultMap="subjectiveQuestions">
        select t.mptid,t.mptqn,t.mptname,t.mptnum,t.mptorder,t.mptscore,
        q.mptqid,q.qtypeid,q.qtypename,q.ctype,q.qscore,q.qn,q.qnstr,q.qorder,q.qanswer,q.optionnum,q.loseoption,q.scoreset
        from ms_paper_qtype_question q left join ms_paper_qtype t on t.mptid=q.mptid
        where t.mpid=#{mpid}
        <if test="qtype!=null and qtype!=0"> and q.qtype=#{qtype}</if>
        order by t.mptorder,q.qorder
    </select>

    <select id="listSubjectiveQuestionsTk" resultMap="subjectiveQuestions">
        select t.mptid,t.mptqn,t.mptname,t.mptnum,t.mptorder,t.mptscore,q.mptqid,
        q.ctype,q.qscore,q.qn,qnstr,q.qorder,q.qanswer,q.optionnum,q.loseoption,q.scoreset,q2.sorder
        from ms_paper_qtype_question q left join ms_paper_qtype t on t.mptid=q.mptid
        left join t_question q2 on q.questionid=q2.questionid
        where t.mpid=#{mpid}
        <if test="qtype!=null and qtype!=0"> and q.qtype=#{qtype}</if>
        order by t.mptorder,q.qorder
    </select>

    <!--修改题型分，修改试卷分-->
    <update id="updateQtypeScore">
        update ms_paper_qtype set mptscore =
            (select sum(qscore) from ms_paper_qtype_question where mptid = #{mptid}) where mptid = #{mptid};
        update ms_paper set pscore = (select sum(qscore) from ms_paper_qtype_question where mpid = #{mpid}) where mpid = #{mpid}
    </update>
    <!--修改题型分和试题数量，修改试卷分和试题数量-->
    <update id="updateQtypeScoreAndNum">
        update ms_paper_qtype set mptnum=mptnum-1, mptscore =
        (select sum(qscore) from ms_paper_qtype_question where mptid = #{mptid}) where mptid = #{mptid};
        update ms_paper set pnum=pnum-1, pscore = (select sum(qscore) from ms_paper_qtype_question where mpid = #{mpid}) where mpid = #{mpid}
    </update>

    <!--获取试卷中试题基本信息-->
    <resultMap id="qtypeQuestionSimple" type="com.xhkjedu.smarking.model.paper.MsPaperQtype">
        <result column="mptid" property="mptid" />
        <result column="mpid" property="mpid" />
        <result column="mptqn" property="mptqn" />
        <result column="mptname" property="mptname" />
        <result column="mptnum" property="mptnum" />
        <result column="mptorder" property="mptorder" />
        <result column="mptscore" property="mptscore" />
        <collection property="questions" ofType="com.xhkjedu.smarking.model.paper.MsPaperQtypeQuestion" javaType="java.util.List">
            <result column="mptqid" property="mptqid" />
            <result column="qscore" property="qscore" />
            <result column="qorder" property="qorder" />
            <result column="qn" property="qn" />
            <result column="qtypeid" property="qtypeid" />
            <result column="qtypename" property="qtypename" />
            <result column="ctype" property="ctype" />
        </collection>

    </resultMap>

    <!--获取试卷中所有试题基础信息-->
    <select id="listQtypeQuestionSimple" resultMap="qtypeQuestionSimple">
        select t.mptid,t.mptqn,t.mptname,t.mptnum,t.mptorder,t.mptscore,
        q.mptqid,q.qtypeid,q.qtypename,q.ctype,q.qscore,q.qn,q.qorder
        from ms_paper_qtype_question q left join ms_paper_qtype t on t.mptid=q.mptid
        where t.mpid=#{mpid} order by t.mptorder,q.qorder
    </select>
    <!--批量修改题型排序-线下-->
    <update id="updateBatchQtypeOrder">
        <foreach collection="list" item="t" separator=";">
            update ms_paper_qtype set mptorder=#{t.mptorder} where mptid=#{t.mptid}
        </foreach>
    </update>
    <!--批量修改题型排序-移动-线下-->
    <update id="updateBatchMoveQtypeOrder">
        <foreach collection="list" item="t" separator=";">
            update ms_paper_qtype set mptorder=mptorder + (#{t.mptorder}) where mptid=#{t.mptid}
        </foreach>
    </update>

    <!--题型移动-获取要交换位置的题型-->
    <select id="getQtypeForMoverOrder" resultType="com.xhkjedu.smarking.model.paper.MsPaperQtype">
        select mptid,mptorder from ms_paper_qtype where mpid=#{p.mpid} and mptorder=(
                (select mptorder from ms_paper_qtype where mptid=#{p.mptid})+(#{p.movevalue})) limit 1
    </select>


</mapper>
