<?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.subjectbook.SubjectQuestiontypeMapper">

    <!-- 根据科目下题型列表 -->
    <select id="listQuestiontypes" resultType="com.xhkjedu.sstudy.model.subjectbook.TSubjectQuestiontype">
        SELECT s.qtypeid,s.qtypename,s.subjectid,s.qtypeorder,s.ctype,s.createid,s.createtime,
        u.username AS createname,s.belong
        FROM t_subject_questiontype s LEFT JOIN t_user u ON s.createid=u.userid
        WHERE s.subjectid=#{type.subjectid} and s.deleted=0 and (s.belong=1
        <if test="type.schoolid!=null">
            or s.schoolid=#{type.schoolid}
        </if>)
        order by s.qtypeorder asc
    </select>

    <!-- 根据科目下题型列表用于选择题型 -->
    <select id="listQuestiontypesForSel" resultType="java.util.Map">
        SELECT qtypeid,qtypename,qtypeorder,ctype,belong from t_subject_questiontype
        WHERE subjectid=#{type.subjectid} and deleted=0 and (belong=1
        <if test="type.schoolid!=null">
            or schoolid=#{type.schoolid}
        </if>)
        order by qtypeorder asc
    </select>

    <!--获取有听力题的题型-->
    <select id="listQtypesHasHear" resultType="java.util.Map">
        select q.qtypeid,s.qtypename,s.qtypeorder,s.ctype,s.belong from t_question q
        left join t_subject_questiontype s on q.qtypeid=s.qtypeid
        where q.hashear=1 and q.qstate=1 and q.subjectid=#{type.subjectid} and s.deleted=0
        and (q.belong=1
        <if test="type.schoolid!=null">
            or q.schoolid=#{type.schoolid}
        </if>)
        group by q.qtypeid order by s.qtypeorder
    </select>

    <!--查找使用题型的模块-->
    <select id="findUseModule" resultType="java.lang.String">
        SELECT CONCAT((select (case when count(*)>0 then '试题' else '' end) from t_question where qtypeid=#{qtypeid})
        ,(select (case when count(*)>0 then ',班级错题' else '' end) from t_question_class where qtypeid=#{qtypeid})
        ,(select (case when count(*)>0 then ',试题收藏' else '' end) from t_question_collect where qtypeid=#{qtypeid})
        ,(select (case when count(*)>0 then ',学生错题' else '' end) from t_question_student where qtypeid=#{qtypeid})
        ,(select (case when count(*)>0 then ',题库作业' else '' end) from t_paper_qtype where qtypeid=#{qtypeid})
        ,(select (case when count(*)>0 then ',附件作业' else '' end) from t_paper_scantron where qtypeid=#{qtypeid})
        )
    </select>

</mapper>
