<?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.yonge.cooleshow.biz.dal.mapper.TenantAlbumMusicMapper">

	 
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.tenant_id_ AS tenantId
        , t.subject_type_ AS subjectType
        , t.tenant_album_id_ AS tenantAlbumId
        , t.music_sheet_id_ AS musicSheetId
        , t.sort_number_ AS sortNumber
        , t.del_flag_ AS delFlag
        , t.update_time_ AS updateTime
        , t.create_time_ AS createTime
        </sql> 
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumMusicWrapper$StudentTenantAlbumMusic">
		SELECT
        m.id_ as id,
        m.music_sheet_name_ as musicSheetName,
        m.composer_ as composer,
        m.user_id_ as userId,
        m.music_subject_ as musicSubject,
        m.title_img_ as titleImg,
        m.exquisite_flag_ as exquisiteFlag,
        m.music_sheet_type_ as musicSheetType,
        t.tenant_album_id_ as tenantAlbumId
		FROM music_sheet m
        left join tenant_album_music t on t.music_sheet_id_ = m.id_
        <where>
            m.state_ = true and m.del_flag_ = 0 and t.del_flag_ = 0
            <if test="param.musicTagId != null ">
                and (find_in_set(#{param.musicTagId},m.music_tag_))
            </if>
            <if test="param.subjectId != null" >
                and (find_in_set(#{param.subjectId},m.music_subject_) or m.music_subject_ is null or m.music_subject_ = '' or m.music_sheet_type_ = 'CONCERT')
            </if>
            <if test="param.subjectType != null">
                and t.subject_type_ = #{param.subjectType}
            </if>
            <if test="param.level != null and param.level != ''">
                and t.level_ = #{param.level}
            </if>
            <if test="param.type != null and param.type != ''">
                and t.type_ = #{param.type}
            </if>
            <if test="param.albumId != null">
                and t.tenant_album_id_ = #{param.albumId}
            </if>

            <if test="param.keyword != null and param.keyword != ''">
                and m.music_sheet_name_ LIKE CONCAT('%',#{param.keyword},'%')
            </if>
        </where>
        order by t.sort_number_
	</select>
    <select id="selectMusicSheetIds" resultType="java.lang.Long">
        select
        t.music_sheet_id_
        FROM tenant_album_music t
        <where>
            t.del_flag_ = false
           <if test="level != null and level != ''">
              and  t.level_ = #{level}
           </if>
            <if test="type != null and type != ''">
              and t.type_ = #{type}
            </if>
            <if test="subjectType != null and subjectType != ''">
              and  t.subject_type_ = #{subjectType}
            </if>
            <if test="albumId != null and albumId != ''">
              and  t.tenant_album_id_ = #{albumId}
            </if>
        </where>
        order by t.sort_number_ asc

    </select>

    <select id="selectMusicIdsByTenantIds" resultType="java.lang.Long">
        select
        distinct t.music_sheet_id_
        FROM tenant_album_music t
        left join tenant_album_ref t1 on t1.tenant_album_id_ = t.tenant_album_id_
        left join music_sheet t2 on t2.id_ = t.music_sheet_id_
        where t2.state_ = 1 and t2.del_flag_ = 0 and t2.audit_version_ = 0 and t1.tenant_id_ = #{tenantId} and t.del_flag_ = 0
    </select>

    <select id="getByAlbumAndEnable" resultType="com.yonge.cooleshow.biz.dal.entity.TenantAlbumMusic">
        select
        t.*
        from tenant_album_music t
        left join music_sheet t1 on t.music_sheet_id_ = t1.id_
        where
        t.del_flag_ = 0 and ((t1.state_ = 1 and t1.del_flag_ = 0 and t.subject_type_ in ('ENSEMBLE', 'MUSIC', 'SUBJECT')) or t.subject_type_ not in ('ENSEMBLE', 'MUSIC', 'SUBJECT'))
        and t.tenant_album_id_ in
        <foreach collection="tenantAlbumIds" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getList" resultType="com.yonge.cooleshow.biz.dal.entity.TenantAlbumMusic">
        select
        t.*
        from tenant_album_music t
        left join music_sheet m on t.music_sheet_id_ = m.id_
        where
        t.del_flag_ = 0
        <if test="param.tenantAlbumId != null">
            and t.tenant_album_id_ = #{param.tenantAlbumId}
        </if>
        <if test="param.subjectType != null">
            and t.subject_type_ = #{param.subjectType}
        </if>
        <if test="albumIds != null and albumIds.size() != 0">
            and t.tenant_album_id_ in
            <foreach collection="albumIds" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.subjectId != null">
            and ( find_in_set(#{param.subjectId},m.music_subject_) or m.music_subject_ is null or m.music_subject_ = ''
            or m.music_sheet_type_ = 'CONCERT'  or t.subject_type_ not in  ('ENSEMBLE', 'MUSIC', 'SUBJECT'))
        </if>
    </select>
</mapper>