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

    <!--根据mpid查询试卷模板列表-->
    <select id="listTemplatesByMpid" resultType="java.util.Map">
        select t.mtid,t.mpid,t.currstep,t.tstate,t.createtime,t.tfront,t.tback,t.blankindex,t.anglefront,t.angleback
        from ms_template t where t.mpid=#{mpid} order by t.createtime desc
    </select>

    <!--设置试卷模板信息-->
    <update id="updateTemplateForCurrstep">
        update ms_template
        <if test="t.currstep &lt; 7">
            set currstep=(#{t.currstep} + 1)
            <if test="t.currstep == 1">
                ,blankindex=#{t.blankindex}
            </if>
            <if test="t.currstep == 2">
                ,anglefront=#{t.anglefront},angleback=#{t.angleback}
            </if>
            <if test="t.currstep == 3">
                ,coorfront=#{t.coorfront},coorback=#{t.coorback},dwtfront=#{t.dwtfront},dwtback=#{t.dwtback}
            </if>
            <if test="t.currstep == 4">
                ,examnocoor=#{t.examnocoor},cardcoor=#{t.cardcoor}
            </if>
            <if test="t.currstep == 6">
                ,misscoor=#{t.misscoor}
            </if>
        </if>
        <if test="t.currstep == 7">
            set violatecoor=#{t.violatecoor},tstate=1
        </if>
        where mtid=#{t.mtid}
    </update>

    <!--获取试卷未提交模板数量-->
    <select id="getUnCommitTemplateCount" resultType="java.lang.Integer">
        select count(*) from ms_template where mpid=#{mpid} and tstate=0
    </select>

    <!--修改模板题块框选状态-->
    <update id="updateTemplateTbstate">
        update ms_template set tbstate=#{tbstate} where mtid=#{mtid}
    </update>
    <!--恢复模板状态-->
    <update id="updateTemplateAllState">
        update ms_template set tstate=0, tbstate=0 where mtid=#{mtid}
    </update>

    <!--获取试卷未框选主观题模板数量-->
    <select id="getUnSelTemplateCount" resultType="java.lang.Integer">
        select count(*) from ms_template where mpid=#{mpid} and tbstate=0
    </select>

    <!--试卷所有模板信息-->
    <resultMap id="paperTemplate" type="com.xhkjedu.smarking.vo.scan.TemplateVo">
        <result property="mtid" column="mtid"/>
        <result property="mpid" column="mpid"/>
        <result property="tsize" column="tsize"/>
        <result property="twidth" column="twidth"/>
        <result property="theight" column="theight"/>
        <result property="tfront" column="tfront"/>
        <result property="tback" column="tback"/>
        <result property="blankindex" column="blankindex"/>
        <result property="anglefront" column="anglefront"/>
        <result property="angleback" column="angleback"/>
        <result property="coorfront" column="coorfront"/>
        <result property="coorback" column="coorback"/>
        <result property="dwtfront" column="dwtfront"/>
        <result property="dwtback" column="dwtback"/>
        <result property="misscoor" column="misscoor"/>
        <result property="violatecoor" column="violatecoor"/>
        <result property="enumtype" column="enumtype"/>
        <result property="examnocoor" column="examnocoor"/>
        <result property="cardcoor" column="cardcoor"/>
        <collection property="tblocks" ofType="com.xhkjedu.smarking.vo.scan.TemplateBlockVo" javaType="java.util.ArrayList">
            <result property="mtbid" column="mtbid"/>
            <result property="mblockid" column="mblockid"/>
            <result property="mttype" column="mttype"/>
            <result property="mtborder" column="mtborder"/>
            <result property="localcoor" column="localcoor"/>
            <result property="extendcoor" column="extendcoor"/>
            <result property="scorecoor" column="scorecoor"/>
            <collection property="tbquestions" ofType="com.xhkjedu.smarking.vo.scan.TemplateBlockQuestionVo" javaType="java.util.ArrayList">
                <result property="mtbqid" column="mtbqid"/>
                <result property="qn" column="qn"/>
                <result property="mptqid" column="mptqid"/>
                <result property="ctype" column="ctype"></result>
                <result property="qlocalcoor" column="qlocalcoor"/>
                <result property="mtbqorder" column="mtbqorder"/>
            </collection>
        </collection>
    </resultMap>
    <select id="listTemplateBlockQueByMpid" resultMap="paperTemplate">
        select t.mtid,t.mpid,t.tsize,t.twidth,t.theight,t.tfront,t.tback,t.blankindex,t.anglefront,t.angleback,t.coorfront,t.coorback,
        t.dwtfront,t.dwtback,t.misscoor,t.violatecoor,t.enumtype,t.examnocoor,t.cardcoor
        ,tb.mtbid,tb.mblockid,tb.mttype,tb.mtborder,tb.localcoor,tb.extendcoor,tb.scorecoor
        ,q.mtbqid,q.qn,q.mptqid,q.qlocalcoor,q.mtbqorder,tq.ctype
        from ms_template t left join ms_template_block tb on t.mtid=tb.mtid
        left join ms_template_block_question q on tb.mtbid=q.mtbid
        left join ms_paper_qtype_question tq on q.mptqid=tq.mptqid
        where t.mpid=#{mpid} order by t.mtid,tb.mttype,tb.mtborder,q.mtbqorder
    </select>

    <!--获取模版详情和考试id-->
    <select id="getTemplateAndExamidByMtid" resultType="com.xhkjedu.smarking.model.paper.MsTemplate">
        select t.*,p.examid from ms_template t left join ms_paper p on t.mpid=p.mpid where t.mtid=#{mtid}
    </select>
</mapper>
