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

    <!-- 根据阶段获取科目 -->
    <select id="listSubjects" resultType="com.xhkjedu.sstudy.model.subjectbook.TSubject">
        SELECT s.subjectid,s.subjectname,s.level,s.subjectorder,s.createid,s.createtime,
        u.username AS createname,s.belong
        FROM t_subject s LEFT JOIN t_user u ON s.createid=u.userid
        WHERE s.level=#{subject.level} and (s.belong=1
        <if test="subject.schoolid!=null">
            or s.schoolid=#{subject.schoolid}
        </if>
        )
        order by s.subjectorder asc
    </select>

    <!-- 根据阶段获取科目用于选择学科 -->
    <select id="listSubjectsForSel" resultType="com.xhkjedu.sstudy.vo.subjectbook.SubjectVo">
        SELECT subjectid,subjectname,belong from t_subject
        WHERE level=#{subject.level} and (belong=1
        <if test="subject.schoolid!=null">
            or schoolid=#{subject.schoolid}
        </if>
        ) order by subjectorder asc
    </select>
    <!--获取科目名称-->
    <select id="getNameById" resultType="java.lang.String">
        select subjectname from t_subject where subjectid=#{subjectid}
    </select>

    <!--根据学段id获取科目id集合-->
    <select id="getSubjectIds" resultType="java.lang.String">
        select group_concat(subjectid) from t_subject where level=#{level}
    </select>
    <!--教师科目集合-->
    <select id="listTeacherSubjects" resultType="com.xhkjedu.sstudy.model.subjectbook.TSubject">
        select sb.subjectid,s.subjectname,sb.belong
        from t_teacher_powersb ts left join t_subject_book sb on ts.lsbid=sb.lsbid
        left join t_subject s on sb.subjectid=s.subjectid
        where ts.teacherid=#{teacherid} and (sb.belong=1 or sb.schoolid=ts.schoolid)
        group by sb.subjectid order by s.subjectorder
    </select>

    <!--查找使用科目的模块-->
    <select id="findUseModule" resultType="java.lang.String">
        SELECT CONCAT(
        (select (case when count(*)>0 then '教材' else '' end) from t_subject_book where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',知识点' else '' end) from t_point where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',题型' else '' end) from t_subject_questiontype where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',试题' else '' end) from t_question where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',资源' else '' end) from t_resource where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',作业' else '' end) from t_paper where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',背诵' else '' end) from t_recite where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',学生错题' else '' end) from t_question_student where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',直播' else '' end) from l_course where subjectid=#{subjectid})
        ,(select (case when count(*)>0 then ',课堂考试' else '' end) from t_classroom_paper where subjectid=#{subjectid})
        )
    </select>
    <!--根据科目ids获取名称列表-->
    <select id="listNameByIds" resultType="java.util.Map">
        select subjectid,subjectname from t_subject where subjectid in
        <foreach collection="subjectids" item="subjectid" index="index" open="(" separator="," close=")">
            #{subjectid}
        </foreach>
    </select>

    <!--根据科目id获取科目名称-->
    <select id="getSubjectnameById" resultType="java.lang.String">
        select subjectname from t_subject where subjectid=#{subjectid}
    </select>
</mapper>
