<?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.sstudy.mapper.question.QuestionMapper">
    <!--批量更新试题信息-->
    <update id="updateBatch" parameterType="java.util.List">
        UPDATE t_question
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="qstem = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.qstem}
                </foreach>
            </trim>
            <trim prefix="qstemtxt = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.qstemtxt}
                </foreach>
            </trim>
            <trim prefix="qoption = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.qoption}
                </foreach>
            </trim>
            <trim prefix="qanswer = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.qanswer}
                </foreach>
            </trim>
            <trim prefix="qanalyze = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.qanalyze}
                </foreach>
            </trim>
            <trim prefix="score = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.score}
                </foreach>
            </trim>
            <trim prefix="complexity = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.complexity}
                </foreach>
            </trim>
            <trim prefix="ctype = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when questionid = #{item.questionid} then #{item.ctype}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            questionid =#{item.questionid}
        </foreach>
    </update>
    <!--试题知识点列表-->
    <resultMap id="questionResult" type="com.xhkjedu.sstudy.vo.question.QuestionVo">
        <result property="questionid" column="questionid"/>
        <result property="score" column="score"/>
        <result property="complexity" column="complexity"/>
        <result property="ctype" column="ctype"/>
        <result property="qstem" column="qstem"/>
        <result property="qstemtxt" column="qstemtxt"/>
        <result property="qoption" column="qoption"/>
        <result property="qanswer" column="qanswer"/>
        <result property="qanalyze" column="qanalyze"/>
        <result property="qtypeid" column="qtypeid"/>
        <result property="qtypename" column="qtypename"/>
        <result property="createtime" column="createtime"/>
        <result property="createname" column="createname"/>
        <result property="count" column="count"/>
        <result property="directorid" column="directorid"/>
        <result property="directorname" column="directorname"/>
        <result property="hashear" column="hashear"/>
        <result property="hearfile" column="hearfile"/>
        <result property="belong" column="belong"/>
        <result property="createid" column="createid"/>
        <result property="source" column="source"/>
        <result property="year" column="year"/>
        <result property="region" column="region"/>
        <result property="schoolname" column="schoolname"/>
        <result property="schoolid" column="schoolid"/>
        <result property="subjectid" column="subjectid"/>
        <result property="areaall" column="areaall"/>
        <result property="qlevel" column="qlevel"/>
        <collection property="points" ofType="com.xhkjedu.sstudy.vo.subjectbook.PointVo" javaType="java.util.List"
                    select="listPoint" column="{questionid=questionid}">
        </collection>
        <collection property="directors" ofType="com.xhkjedu.sstudy.vo.subjectbook.DirectorVo" javaType="java.util.List">
            <result property="lsbid" column="lsbid"/>
            <result property="directorid" column="directorid"/>
            <result property="directorname" column="directorname"/>
            <result property="level" column="level"/>
            <result property="subjectid" column="subjectid"/>
            <result property="subjectname" column="subjectname"/>
            <result property="versionname" column="versionname"/>
            <result property="lsbname" column="lsbname"/>
        </collection>
    </resultMap>
    <select id="listPoint" resultType="com.xhkjedu.sstudy.vo.subjectbook.PointVo">
        select qp.pointid,p.pointname from t_question_point qp left join t_point p on qp.pointid = p.pointid
        where qp.questionid=#{questionid}
    </select>
    <!--详情-->
    <select id="findById" resultMap="questionResult">
        select q.questionid,q.score,q.complexity,q.ctype,q.qstem,q.qstemtxt,q.qoption,q.qanswer,q.qanalyze,q.belong
        ,q.qtypeid,q.qtypename,q.createtime,q.source,q.year,q.region,q.schoolname,q.subjectid,q.schoolid
        ,qd.directorid,d.directorall as directorname,d.lsbid,q.hashear,q.hearfile,q.qlevel,
        sb.level,sb.subjectid,s.subjectname,v.versionname,sb.lsbname
        ,(select a.areaall from t_areazone a where a.areacode=q.region)areaall
        from t_question q left join t_question_director qd on q.questionid=qd.questionid
        left join t_director d on qd.directorid = d.directorid
        left join t_subject_book sb on d.lsbid = sb.lsbid
        left join t_subject s on sb.subjectid = s.subjectid
        left join t_version v on sb.versionid = v.versionid
        where q.questionid=#{questionid}
    </select>
    <select id="listSonQuestionsForDetail" resultType="java.util.Map">
        select q.questionid,q.score,q.complexity,q.qstem,q.qstemtxt,q.qoption,q.qanswer,q.qanalyze
             ,q.qtypeid,q.qtypename,q.ctype,q.qlevel,q.sorder
        from t_question q where q.qlevel=3 and q.questionpid=#{questionid} order by q.sorder
    </select>
    <!--复合题子题分享集团校用-->
    <select id="listSonQuestionsForShare" resultType="com.xhkjedu.sstudy.model.question.TQuestion">
        select q.* from t_question q where q.qlevel=3 and q.questionpid=#{questionid} order by q.sorder
    </select>
    <!--管理端试题列表-->
    <select id="findAll" resultType="java.util.Map">
        select q.questionid,q.score,q.complexity,q.ctype,q.qstem,q.qoption,q.qanswer,q.qanalyze,q.belong,q.createid,q.schoolid
        ,q.qtypeid,q.qtypename,q.createtime,q.count,q.qstate,q.source,q.year,q.region,q.schoolname,q.hashear,q.hearfile,q.qlevel
        from t_question q
        <if test="question.belong!=null and question.belong==5">
            left join t_question_collect qc on q.questionid = qc.questionid
        </if>
        left join t_question_director qd on q.questionid=qd.questionid
        left join t_director d on qd.directorid=d.directorid
        where d.disabled=0
        <choose>
            <when test="question.belong==0">
                and (q.belong in(1,2)
                <if test="question.schoolid!=null">
                    or (q.belong in(3,10) and q.schoolid=#{question.schoolid})
                </if>
                <if test="question.createid!=null">
                    or (q.belong=4 and q.createid=#{question.createid})
                </if>
                )
            </when>
            <when test="question.belong==2">
                and q.belong in(1,2)
            </when>
            <when test="question.belong==3 or question.belong==10">
                and q.schoolid=#{question.schoolid} and q.belong=#{question.belong}
            </when>
            <when test="question.belong==4">
                and q.belong=#{question.belong} and q.createid=#{question.createid}
            </when>
            <when test="question.belong==5">
                and qc.userid=#{question.createid}
            </when>
            <when test="question.belong==6">
                and q.schoolid=#{question.schoolid} and q.createid=#{question.createid}
            </when>
        </choose>
        <if test="question.subjectid!=null and question.subjectid!='0'.toString() and question.subjectid!=''">
            and q.subjectid=#{question.subjectid}
        </if>
        <if test="question.qstate!=null and question.qstate!=0">
            and q.qstate=#{question.qstate}
        </if>
        <if test="question.qstate==null or question.qstate==0">
            and q.qstate!=10
        </if>
        <if test="question.lsbid!=null and question.lsbid!='0'.toString() and question.lsbid!=''">
            and qd.lsbid=#{question.lsbid}
        </if>
        <if test="question.directorid!=null and question.directorid!='0'.toString() and question.directorid!='' and question.directorLeaf==1">
            and qd.directorid=#{question.directorid}
        </if>
        <if test="question.directorid!=null and question.directorid!='0'.toString() and question.directorid!='' and question.directorLeaf==0">
            and d.directorall like concat('%',#{question.directorall},'%')
        </if>
        <if test="question.hashear!=null and question.hashear == 1">
            and q.hashear=1
        </if>
        <if test="question.hashear!=1 and question.qtypeid!=null and question.qtypeid!='0'.toString() and question.qtypeid!=''">
            and q.hashear=0 and q.qtypeid=#{question.qtypeid}
        </if>
        <if test="question.complexity!=null and question.complexity!=0">
            and q.complexity=#{question.complexity}
        </if>
        <if test="question.year!=null and question.year!=0">
            and FROM_UNIXTIME(q.createtime,'%Y')=#{question.year}
        </if>
        and q.qlevel &lt; 3
        group by q.questionid order by q.qstate,q.createtime desc
    </select>

    <!--章节、知识点选题-->
    <select id="listForSelect" resultType="java.util.Map">
        select q.questionid,q.score,q.complexity,q.qstem,q.qstemtxt,q.qoption,q.qanswer,q.qanalyze
        ,q.qtypeid,q.qtypename,q.count,q.ctype,q.source,q.year,q.region,q.schoolname
        ,q.qlevel,q.hearfile,q.hashear,q.belong,q.createid,q.schoolid
        from t_question q
        <if test="question.pointid!=null and question.pointid!='0'.toString() and question.pointid!=''">
            left join t_question_point qp on q.questionid = qp.questionid
        </if>
        <if test="question.belong!=null and question.belong==5">
            left join t_question_collect qc on q.questionid = qc.questionid
        </if>
        left join t_question_director qd on q.questionid=qd.questionid
        left join t_director d on qd.directorid=d.directorid
        <if test="question.pointid!=null and question.pointid!='0'.toString() and question.pointid!='' and question.pointLeaf==0">
            left join t_point p on qp.pointid = p.pointid
        </if>
        where d.disabled=0 and q.qstate=1
        <if test="question.lsbid!=null and question.lsbid!='0'.toString() and question.lsbid!=''">
            and qd.lsbid=#{question.lsbid}
        </if>
        <if test="question.directorid!=null and question.directorid!='0'.toString() and question.directorid!='' and question.directorLeaf==1">
            and qd.directorid=#{question.directorid}
        </if>
        <if test="question.directorid!=null and question.directorid!='0'.toString() and question.directorid!='' and question.directorLeaf==0">
            and d.directorall like concat('%',#{question.directorall},'%')
        </if>
        <if test="question.subjectid!=null and question.subjectid!='0'.toString() and question.subjectid!=''">
            and q.subjectid=#{question.subjectid}
        </if>
        <if test="question.pointid!=null and question.pointid!='0'.toString() and question.pointid!='' and question.pointLeaf==1">
            and qp.pointid=#{question.pointid}
        </if>
        <if test="question.pointid!=null and question.pointid!='0'.toString() and question.pointid!='' and question.pointLeaf==0">
            and p.pointall like concat('%',#{question.pointall},'%')
        </if>
        <if test="question.hashear!=null and question.hashear == 1">
            and q.hashear=1
        </if>
        <if test="question.hashear!=1 and question.qtypeid!=null and question.qtypeid!='0'.toString() and question.qtypeid!=''">
            and q.hashear=0 and q.qtypeid=#{question.qtypeid}
        </if>
        <if test="question.complexity!=null and question.complexity!=0">
            and q.complexity=#{question.complexity}
        </if>
        <choose>
            <when test="question.belong==0">
                and (q.belong in(1,2)
                <if test="question.schoolid!=null">
                    or (q.belong in(3,10) and q.schoolid=#{question.schoolid})
                </if>
                <if test="question.createid!=null">
                    or (q.belong=4 and q.createid=#{question.createid})
                </if>
                )
            </when>
            <when test="question.belong==2">
                and q.belong in(1,2)
            </when>
            <when test="question.belong==3 or question.belong==10">
                and q.schoolid=#{question.schoolid} and q.belong=#{question.belong}
            </when>
            <when test="question.belong==4">
                and q.belong=#{question.belong} and q.createid=#{question.createid}
            </when>
            <when test="question.belong==5">
                and qc.userid=#{question.createid}
            </when>
            <when test="question.belong==6">
                and q.schoolid=#{question.schoolid} and q.createid=#{question.createid}
            </when>
        </choose>
        <if test="question.year!=null and question.year!=0">
            and FROM_UNIXTIME(q.createtime,'%Y')=#{question.year}
        </if>
        and q.qlevel &lt; 3
        group by q.questionid order by q.createtime desc
    </select>
    <!--试题是否分享过-->
    <select id="listShareNumByIds" resultType="java.util.Map">
        select shareqid,count(*) as sharenum
        from t_question where qstate=1 and shareqid in('${shareqids}')
        group by shareqid
    </select>

    <select id="listSonQuestionForSelect" resultType="java.util.Map">
        select q.questionid,q.questionpid,q.score,q.complexity,q.qstem,q.qstemtxt,q.qoption,q.qanswer,q.qanalyze
        ,q.qtypeid,q.qtypename,q.ctype,q.qlevel,q.sorder,q.hashear,q.hearfile
        from t_question q where q.qlevel=3 and q.questionpid in('${questionids}') order by q.sorder
    </select>
    <select id="selectQuesitonPoints" 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 qp.questionid=#{questionid} order by p.pointorder
    </select>
    <!--修改试题状态-->
    <update id="updateQuestionState">
        update t_question set qstate=#{qstate} where questionid in (${questionIds}) or shareqid in (${questionIds})
    </update>
    <!--更新单个试题状态-->
    <update id="updateState">
        update t_question set qstate=#{qstate} where questionid=#{questionid} or shareqid=#{questionid}
    </update>
    <!--更新组题次数-->
    <update id="updateCount">
        update t_question set count=count+1 where questionid=#{questionid}
    </update>

    <!--批量保存-->
    <insert id="saveBathQuestion" parameterType="com.xhkjedu.sstudy.model.question.TQuestion">
        INSERT INTO t_question (questionid,qstem,qstemtxt,qoption,qanswer,qanalyze,score,complexity,
                                ctype,hashear,hearfile,qtypeid,qtypename, subjectid,qlevel,snum,sorder,questionpid,
                                qstate,source,year,region,schoolname,createid,createtime,belong,schoolid,shareqid)
        VALUES
        <foreach collection ="list" item="p" index= "index" separator =",">
            (#{p.questionid},#{p.qstem},#{p.qstemtxt},#{p.qoption},#{p.qanswer},#{p.qanalyze},#{p.score},#{p.complexity},
             #{p.ctype},#{p.hashear},#{p.hearfile},#{p.qtypeid},#{p.qtypename},#{p.subjectid},#{p.qlevel},#{p.snum},#{p.sorder},#{p.questionpid},
             #{p.qstate},#{p.source},#{p.year},#{p.region},#{p.schoolname},#{p.createid},#{p.createtime},#{p.belong},#{p.schoolid},#{p.shareqid})
        </foreach>
    </insert>

    <!--获取科目下听力题数量-->
    <select id="getQuestionHasHearNum" resultType="java.lang.Integer">
        select count(*) from t_question where hashear=1 and qstate=1 and subjectid=#{subjectid}
    </select>
    <!--获取试题详情及关联章节、知识点、伙伴校-->
    <resultMap id="questionMap" type="com.xhkjedu.sstudy.model.question.TQuestion">
        <collection property="questionPoints" select="listPoint2" column="questionid"/>
        <collection property="directors" select="listDirector" column="questionid"/>
        <collection property="schools" select="listSchool" column="questionid"/>
    </resultMap>
    <select id="listPoint2" resultType="com.xhkjedu.sstudy.model.question.TQuestionPoint">
        select * from t_question_point where questionid=#{questionid}
    </select>
    <select id="listDirector" resultType="com.xhkjedu.sstudy.model.question.TQuestionDirector">
        select * from t_question_director where questionid=#{questionid}
    </select>
    <select id="listSchool" resultType="java.util.Map">
        select schoolid from t_question where shareqid=#{questionid} group by schoolid
    </select>
    <select id="getQuestionAndDirectorPointById" resultMap="questionMap">
        select * from t_question where questionid=#{questionid}
    </select>
    <!--获取分享的伙伴校-->
    <select id="listSharePartnerSchool" resultType="java.util.Map">
        select s.schoolid,s.schoolname
        from t_question q left join t_school s on q.schoolid=s.schoolid
        where q.shareqid=#{questionid} and s.schoolstate=1
        group by s.schoolid order by s.schoolid
    </select>
    <!--更新分享过伙伴校试题的时间-->
    <update id="batchUpdateTime">
        update t_question set createtime=#{timestamp}
        where shareqid=#{shareqid} and schoolid in
        <foreach collection="list" item="schoolid" open="(" separator="," close=")">${schoolid}</foreach>
    </update>
</mapper>
