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

    <!--试卷题型试题信息(题库)-->
    <resultMap id="qtypesQuestion" type="com.xhkjedu.sexam.vo.paper.EPaperQtypeVo">
        <result column="eptid" property="eptid"></result>
        <result column="epid" property="epid"></result>
        <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="com.xhkjedu.sexam.vo.paper.EPaperQtypeQuestionVo" javaType="java.util.List">
            <result column="eptqid" property="eptqid"></result>
            <result column="eptid" property="eptid"></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="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="answer" property="answer"></result>
            <result column="qctype" property="ctype"></result>
            <result column="qstem" property="qstem"></result>
            <result column="qstemtxt" property="qstemtxt"></result>
            <result column="qoption" property="qoption"></result>
            <result column="qanswer" property="qanswer"></result>
            <result column="qanalyze" property="qanalyze"></result>
            <result column="complexity" property="complexity"></result>
            <result column="hashear" property="hashear"></result>
            <result column="hearfile" property="hearfile"></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="mqstem" property="mqstem"></result>
            <result column="mcomplexity" property="mcomplexity"></result>
            <result column="mqtypeid" property="mqtypeid"></result>
            <result column="mqtypename" property="mqtypename"></result>
            <result column="mctype" property="mctype"></result>
            <result column="mqanswer" property="mqanswer"></result>
            <result column="mqanalyze" property="mqanalyze"></result>
            <result column="mqlevel" property="mqlevel"></result>
            <result column="mhashear" property="mhashear"></result>
            <result column="mhearfile" property="mhearfile"></result>
            <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.eptid,t.epid,t.ctype,t.eptname,t.eptnum,t.eptorder,t.eptscore,tq.eptqid,tq.questionid,tq.answer,
               tq.score,tq.qn,tq.qorder,q.qtypeid,q.qtypename,q.ctype qctype,q.qstem,q.qstemtxt,q.qoption,q.qanswer,q.qanalyze,
               q.complexity,q.hashear,q.hearfile,q.qlevel,q.sorder,q.questionpid,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 e_paper_qtype_question tq left join e_paper_qtype t on t.eptid=tq.eptid
        left join t_question q on tq.questionid=q.questionid
        left JOIN t_question fq on q.questionpid=fq.questionid
        where t.epid=#{epid} order by t.eptorder,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.sexam.vo.paper.EPaperQtypeVo">
        <result column="eptid" property="eptid"></result>
        <result column="epid" property="epid"></result>
        <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="eptqid" property="eptqid"></result>
            <result column="eptid" property="eptid"></result>
            <result column="score" property="score"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="qctype" property="ctype"></result>
            <result column="qn" property="qn"></result>
            <result column="optionnum" property="optionnum"></result>
            <result column="answer" property="answer"></result>
        </collection>
    </resultMap>
    <select id="listPaperQtypeQuesitonsForFj" resultMap="fjtypeQuestions">
        select  t.eptid,t.epid,t.ctype,t.eptname,t.eptnum,t.eptorder,t.eptscore,
        q.eptqid,q.qtypeid,q.qtypename,q.ctype qctype,q.score,q.qn,q.qorder,q.answer,q.optionnum
        from e_paper_qtype_question q left join e_paper_qtype t on t.eptid=q.eptid
        where t.epid=#{epid} order by t.eptorder,q.qorder
    </select>

    <resultMap id="qtypesQuestionsForScan" type="java.util.Map">
        <result column="eptname" property="eptname"></result>
        <result column="ctype" property="ctype"></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="eptqid" property="eptqid"></result>
            <result column="qctype" property="ctype"></result>
            <result column="score" property="score"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="optionnum" property="optionnum"></result>
            <result column="coord" property="coord"></result>
            <result column="gnum" property="gnum"></result>
            <result column="height" property="height"></result>
            <result column="queshtml" property="queshtml"></result>
        </collection>
    </resultMap>

    <!--制作答题卡时获取试卷中试题-->
    <select id="listPaperqtypeQuestionsForScan" resultMap="qtypesQuestionsForScan">
        select t.eptname,t.ctype,t.eptnum,t.eptorder,t.eptscore,tq.eptqid,tq.ctype qctype,tq.score,
        tq.qorder,tq.qn,tq.optionnum,tq.coord,tq.gnum,tq.height,tq.queshtml
        from e_paper_qtype_question tq  left join e_paper_qtype t on t.eptid=tq.eptid
        where t.epid=#{epid} order by t.eptorder,tq.qorder
    </select>

    <!--保存试题正确答案-->
    <update id="updateBatchQuestionAnswer">
        update e_paper_qtype_question set
        <trim prefix="answer=case" suffix="end">
            <foreach collection="list" item="q">
                when eptqid=#{q.eptqid} then #{q.answer}
            </foreach>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                eptqid=#{q.eptqid}
            </foreach>
        </where>
    </update>

    <!--保存试卷答题卡坐标-->
    <update id="updateBatchQuestionForScan">
        update e_paper_qtype_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="coord = case" suffix="end,">
                <foreach collection="list" item="q">
                    when eptqid=#{q.eptqid} then #{q.coord}
                </foreach>
            </trim>
            <trim prefix="gnum = case" suffix="end,">
                <foreach collection="list" item="q">
                    when eptqid=#{q.eptqid} then #{q.gnum}
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" item="q" separator="or">
                eptqid=#{q.eptqid}
            </foreach>
        </where>
    </update>

    <!--试卷中所有试题基础信息-->
    <select id="listQuestionsByEpid" resultType="com.xhkjedu.sexam.model.paper.EPaperQtypeQuestion">
        select * from e_paper_qtype_question where epid=#{epid} order by qorder
    </select>

    <!--根据试卷id删除试题-->
    <delete id="deleteByEpid">
        delete t.*,q.* from e_paper_qtype t
        left join e_paper_correctquestion q on t.epid = q.epid
        where t.epid=#{epid}
    </delete>
</mapper>
