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

    <select id="listSubjectBooks" resultType="com.xhkjedu.sstudy.model.subjectbook.TSubjectBook">
        SELECT sb.lsbid,sb.level,sb.subjectid,sb.coverpath,s.subjectname,sb.lsbname,sb.createtime
        ,sb.versionid,v.versionname,sb.lsborder,sb.belong
        ,(select u.username from t_user u where u.userid=sb.createid)createname
        FROM t_subject_book sb LEFT JOIN t_subject s ON sb.subjectid=s.subjectid
        LEFT JOIN t_version v ON sb.versionid=v.versionid
        WHERE sb.level=#{book.level} and (sb.belong=1
        <if test="book.schoolid!=null">
            or sb.schoolid=#{book.schoolid}
        </if>
        )
        <if test="book.subjectid!=null and book.subjectid!='0'.toString() and book.subjectid!=''">
            and sb.subjectid=#{book.subjectid}
        </if>
        <if test="book.versionid!=null and book.versionid!='0'.toString() and book.versionid!=''">
            and sb.versionid=#{book.versionid}
        </if>
        ORDER BY s.subjectorder ASC,v.versionorder,sb.lsborder
    </select>

    <!--查找使用教材的模块-->
    <select id="findUseModule" resultType="java.lang.String">
        SELECT CONCAT(
        (select (case when count(*)>0 then '章节' else '' end) from t_director where lsbid=#{lsbid} and directorstate=1 and disabled=0)
        ,(select (case when count(*)>0 then ',试题' else '' end) from t_question_director where lsbid=#{lsbid})
        ,(select (case when count(*)>0 then ',资源' else '' end) from t_resource_director where lsbid=#{lsbid})
        ,(select (case when count(*)>0 then ',作业' else '' end) from t_paper where lsbid=#{lsbid})
        ,(select (case when count(*)>0 then ',课堂' else '' end) from t_classroom where lsbid=#{lsbid})
        ,(select (case when count(*)>0 then ',课堂考试' else '' end) from t_classroom_paper where lsbid=#{lsbid})
        )
    </select>

    <!--教材选择列表-->
    <resultMap id="lsbResult" type="java.util.Map">
        <result property="level" column="level"></result>
        <collection property="subjects" ofType="java.util.Map" javaType="java.util.List">
            <result property="subjectid" column="subjectid"></result>
            <result property="subjectname" column="subjectname"></result>
            <collection property="versions" ofType="java.util.Map" javaType="java.util.List">
                <result property="versionid" column="versionid"></result>
                <result property="versionname" column="versionname"></result>
                <collection property="lsbs" ofType="java.util.Map" javaType="java.util.List">
                    <result property="lsbid" column="lsbid"></result>
                    <result property="lsbname" column="lsbname"></result>
                    <result property="coverpath" column="coverpath"></result>
                </collection>
            </collection>
        </collection>
    </resultMap>
    <select id="listForSel" resultMap="lsbResult">
        select sb.level,sb.subjectid,s.subjectname,sb.versionid,v.versionname,sb.lsbname,sb.lsbid,sb.coverpath
        from t_subject_book sb left join t_subject s on sb.subjectid=s.subjectid
        left join t_version v on sb.versionid=v.versionid
        order by sb.level,s.subjectorder asc,v.versionorder asc,sb.lsborder
    </select>
    <!--查询教材名称是否存在-->
    <select id="checkExisted" resultType="java.lang.Integer">
        select count(*) from t_subject_book where level=#{book.level} and subjectid=#{book.subjectid}
        and versionid=#{book.versionid} and lsbname=#{book.lsbname}
    </select>
</mapper>
