<?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.dao.MusicTagDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.MusicTag">
        <result column="id_" property="id" />
        <result column="parent_tag_id_" property="parentTagId" />
        <result column="name_" property="name" />
        <result column="state_" property="state" />
        <result column="sort_number_" property="sortNumber" />
        <result column="type_" property="type" />
        <result column="del_flag_" property="delFlag" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
        <result column="update_by_" property="updateBy" />
        <result column="create_by_" property="createBy" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.parent_tag_id_ as parentTagId
        , t.name_ as name
        , t.state_ as state
        , t.type_ as type
        , t.sort_number_ as sortNumber
        , t.del_flag_ as delFlag
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.update_by_ as updateBy
        , t.create_by_ as createBy
        </sql> 
    
    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.MusicTagVo">
        SELECT
            <include refid="baseColumns"/>
        FROM music_tag t
        where t.id_ = #{id}
    </select>
    
    <select id="selectPage"  resultType="com.yonge.cooleshow.biz.dal.vo.MusicTagVo">
		SELECT         
        	<include refid="baseColumns" />,
            su.username_ as updateName
		FROM music_tag t
        left join sys_user su on t.update_by_ = su.id_
        <where>
            t.del_flag_ = 0
            <if test="param.state != null">
                and t.state_ = #{param.state}
            </if>
            <if test="param.type != null and param.type != ''">
                and t.type_ = #{param.type}
            </if>
            <if test="param.idAndName != null and param.idAndName != ''">
                and ( t.id_ like concat( '%',#{param.idAndName},'%') or
                    t.name_ like  concat ('%',#{param.idAndName},'%')
                    )
            </if>
        </where>
        order by  t.sort_number_ desc,t.id_ desc
	</select>


	<select id="getChildrenByParentIdList" resultType="com.yonge.cooleshow.biz.dal.vo.MusicTagVo">
        SELECT
        <include refid="baseColumns" />,
        su.username_ as updateName,
        (select count(1) from music_sheet ms where find_in_set(t.id_,ms.music_tag_) and ms.del_flag_ = 0 and find_in_set('PLATFORM',ms.provider_type_) and  ms.state_ = 1 and ms.source_type_ = 'PLATFORM') as enablePlatformMusicSheetNum,
        (select count(1) from music_sheet ms where find_in_set(t.id_,ms.music_tag_) and ms.del_flag_ = 0 and find_in_set('PLATFORM',ms.provider_type_)  and ms.source_type_ = 'PLATFORM') as musicPlatformSheetNum,
        (select count(1) from music_sheet ms where find_in_set(t.id_,ms.music_tag_) and ms.del_flag_ = 0 and find_in_set('PLATFORM',ms.provider_type_) and ms.source_type_ = 'TEACHER' and ms.audit_status_ = 'PASS') as musicTeacherSheetNum,
        (select count(1) from music_sheet ms where find_in_set(t.id_,ms.music_tag_) and ms.del_flag_ = 0 and find_in_set('PLATFORM',ms.provider_type_)  and ms.source_type_ = 'TEACHER' and ms.state_ = 1 and ms.audit_status_ = 'PASS') as enableTeacherMusicSheetNum,
        (select count(1) from music_album ma where find_in_set(t.id_,ma.album_tag_) and ma.del_flag_ = 0    and ma.album_status_ = 1) as enablePlatformAlbumNum,
        (select count(1) from music_album ma where find_in_set(t.id_,ma.album_tag_) and ma.del_flag_ = 0 ) as musicPlatformAlbumNum
        FROM music_tag t
        left join sys_user su on t.update_by_ = su.id_
        <where>
            t.del_flag_ = 0
            <if test="longList != null and longList.size() != 0">
                and t.id_ in
                <foreach collection="longList" close=")" item="item" open="(" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="state != null">
                and t.state_ = #{state}
            </if>
        </where>
    </select>

    <select id="checkTagBeUsedMusicSheet" resultType="java.lang.Integer">

        select count(1) from music_sheet ms
        <where>
            ms.del_flag_ = 0 and find_in_set('PLATFORM',ms.provider_type_)
            <if test="longs != null and longs.size() != 0">
                and
                <foreach collection="longs" item="item" open="(" close=")"  separator="or">
                    find_in_set(#{item},ms.music_tag_)
                </foreach>
            </if>
        </where>
    </select>

    <select id="checkTagBeUsedMusicAlbum" resultType="java.lang.Integer">

        select count(1) from music_album ms
        <where>
            ms.del_flag_ = 0
            <if test="longs != null and longs.size() != 0">
                and
                <foreach collection="longs" item="item" open="(" close=")"  separator="or">
                    find_in_set(#{item},ms.album_tag_)
                </foreach>
            </if>
        </where>
    </select>
    <select id="tagList"
            resultType="com.dayaedu.cbs.openfeign.wrapper.music.CbsMusicSheetTagWrapper$MusicSheetTag">
        SELECT t.id_ id,t.name_,t.state_ enable FROM music_tag t
        <where>
            type_ = 'MUSIC'
            <if test="param.ids != null and param.ids.size > 0">
                and t.id_ IN
                <foreach collection="param.ids" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="param.id != null">
                and t.id_ = #{param.id}
            </if>
            <if test="param.delFlag != null">
                and t.del_flag_ = #{param.delFlag}
            </if>
            <if test="param.enable != null">
                and t.state_ = #{param.enable}
            </if>
        </where>
    </select>
    <select id="categoryList"
            resultType="com.dayaedu.cbs.openfeign.wrapper.music.CbsMusicSheetCategoryWrapper$MusicSheetCategory">
        SELECT t.id_ id,t.name_ name,t.state_ enable,t.parent_tag_id_ parentId FROM music_tag t
        <where>
            <if test="param.ids != null and param.ids.size > 0">
                and t.id_ IN
                <foreach collection="param.ids" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="param.id != null">
                and t.id_ = #{param.id}
            </if>
            <if test="param.delFlag != null">
                and t.del_flag_ = #{param.delFlag}
            </if>
        </where>
    </select>

    <select id="getAll" resultType="com.yonge.cooleshow.biz.dal.entity.MusicTag">
        SELECT
            <include refid="baseColumns"/>
        FROM music_tag t
    </select>
</mapper>