<?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.MusicAlbumDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.MusicAlbum">
        <id column="id_" jdbcType="BIGINT" property="id"/>
        <result column="album_name_" jdbcType="VARCHAR" property="albumName"/>
        <result column="album_desc_" jdbcType="VARCHAR" property="albumDesc"/>
        <result column="album_tag_" jdbcType="VARCHAR" property="albumTag"/>
        <result column="subject_id_" jdbcType="VARCHAR" property="subjectId"/>
        <result column="album_cover_url_" jdbcType="VARCHAR" property="albumCoverUrl"/>
        <result column="album_status_" jdbcType="TINYINT" property="albumStatus"/>
        <result column="sort_number_" jdbcType="INTEGER" property="sortNumber"/>
        <result column="hot_flag_" jdbcType="TINYINT" property="hotFlag"/>
        <result column="top_flag_" jdbcType="TINYINT" property="topFlag"/>
        <result column="audit_version_" jdbcType="TINYINT" property="auditVersion"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_by_" jdbcType="BIGINT" property="createBy"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_by_" jdbcType="BIGINT" property="updateBy"/>
    </resultMap>

    <sql id="Base_Column_List">
        t.id_,
        t.album_name_,
        t.album_desc_,
        t.album_tag_,
        t.subject_id_,
        t.album_cover_url_,
        t.album_status_,
        t.sort_number_,
        t.hot_flag_,
        t.top_flag_,
        t.create_time_,
        t.audit_version_,
        t.create_by_,
        t.update_time_,
        t.update_by_
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select <include refid="Base_Column_List"/>
        ,(select group_concat(mt.name_) from music_tag mt
            where find_in_set(mt.id_,t.album_tag_) and mt.del_flag_ = 0 and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
        where find_in_set(s.id_,t.subject_id_) and s.del_flag_ = 0 ) as subjectNames
        , t1.num as musicSheetCount
        ,t2.num as albumFavoriteCount
        from music_album t
        left join (select count(1) as num,amr.album_id_
            from album_music_relate amr
            join music_sheet ms on amr.music_sheet_id_ = ms.id_
            left join sys_user su on su.id_ = ms.user_id_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS' and su.del_flag_ = 0
        group by amr.album_id_) t1 on t1.album_id_ = t.id_
        left join (select count(1) as num,af.album_id_ from album_favorite af
            left join sys_user su on af.user_id_ = su.id_
            where su.del_flag_ = 0 group by af.album_id_
        ) t2 on t2.album_id_ = t.id_
        <where>
            t.del_flag_ = 0
            <if test="query.idAndName != null and query.idAndName != ''">
                and (t.id_ like concat('%',#{query.idAndName},'%') or
                    t.album_name_ like concat('%',#{query.idAndName},'%'))
            </if>
            <if test="query.albumTagIdList != null and query.albumTagIdList.size() != 0">
                and
                <foreach collection="query.albumTagIdList"  open="(" close=")"  separator="or" item="item">
                    find_in_set(#{item},t.album_tag_)
                </foreach>
            </if>
            <if test="query.subjectIdList != null and query.subjectIdList.size() != 0">
                and
                <foreach collection="query.subjectIdList"  open="(" close=")"  separator="or" item="item">
                    find_in_set(#{item},t.subject_id_)
                </foreach>
            </if>
            <if test="query.albumStatus != null">
                and t.album_status_ = #{query.albumStatus}
            </if>
            <if test="query.auditVersion != null">
                and t.audit_version_ = #{query.auditVersion}
            </if>
        </where>
        <if test="query.sortBy != null">
            <choose>
                <when test="query.sortBy == 1">
                    order by t2.num desc
                </when>
            </choose>
        </if>
        <if test="query.sortBy == null">
            order by t.id_ desc
        </if>
    </select>

    <select id="selectFavoritePage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select <include refid="Base_Column_List"/>
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.album_tag_) and mt.del_flag_ = 0 and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
        where find_in_set(s.id_,t.subject_id_) and s.del_flag_ = 0 ) as subjectNames
        from music_album t
        left join album_favorite af2 on af2.album_id_ = t.id_
        <where>
            t.del_flag_ = 0
            <if test="query.studentId != null">
                and af2.user_id_ = #{query.studentId}
            </if>
            <if test="query.idAndName != null and query.idAndName != ''">
                and (t.id_ like concat('%',#{query.idAndName},'%') or
                t.album_name_ like concat('%',#{query.idAndName},'%'))
            </if>
            <if test="query.albumTagIdList != null and query.albumTagIdList.size() != 0">
                and
                <foreach collection="query.albumTagIdList"  open="(" close=")"  separator="or" item="item">
                    find_in_set(#{item},t.album_tag_)
                </foreach>
            </if>
            <if test="query.subjectIdList != null and query.subjectIdList.size() != 0">
                and
                <foreach collection="query.subjectIdList"  open="(" close=")"  separator="or" item="item">
                    find_in_set(#{item},t.subject_id_)
                </foreach>
            </if>
            <if test="query.auditVersion != null">
                and t.audit_version_ = #{query.auditVersion}
            </if>
            <if test="query.albumStatus != null">
                and t.album_status_ = #{query.albumStatus}
            </if>
        </where>
        order by  af2.favorite_time_ desc
    </select>

    <select id="selectFavoriteNumAndMusicNum" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select
        t1.num as musicSheetCount
        ,t2.num as albumFavoriteCount
        from music_album t
        left join (select count(1) as num,amr.album_id_
            from album_music_relate amr
            join music_sheet ms on amr.music_sheet_id_ = ms.id_
            left join sys_user su on su.id_ = ms.user_id_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS' and su.del_flag_ = 0
            group by amr.album_id_) t1 on t1.album_id_ = t.id_
        left join (select count(1) as num,af.album_id_ from album_favorite af
                left join sys_user su on af.user_id_ = su.id_
                where su.del_flag_ = 0 group by af.album_id_
        ) t2 on t2.album_id_ = t.id_
        <where>
            t.del_flag_ = 0
            <if test="records != null and records.size() != 0">
                and t.id_ in
                <foreach collection="records" item="item" open="(" close=")" separator=",">
                    #{item.id}
                </foreach>
            </if>
        </where>
        order by
        <if test="records != null and records.size() != 0">
            field(t.id_,
            <foreach collection="records" item="item" open="" close=")" separator=",">
                #{item.id}
            </foreach>
        </if>
    </select>

    <select id="selectMusicAlbumById" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select <include refid="Base_Column_List"/>
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.album_tag_) and mt.del_flag_ = 0 and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
        where find_in_set(s.id_,t.subject_id_) and s.del_flag_ = 0 ) as subjectNames
        , t1.num as musicSheetCount
        ,t2.num as albumFavoriteCount
        from music_album t
        left join (select count(1) as num,amr.album_id_
            from album_music_relate amr
            join music_sheet ms on amr.music_sheet_id_ = ms.id_
            left join sys_user su on su.id_ = ms.user_id_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS' and su.del_flag_ = 0
            group by amr.album_id_) t1 on t1.album_id_ = t.id_
        left join (select count(1) as num,af.album_id_ from album_favorite af
            left join sys_user su on af.user_id_ = su.id_
            where su.del_flag_ = 0 group by af.album_id_
        ) t2 on t2.album_id_ = t.id_
        where t.id_ = #{musicAlbumId}
    </select>

    <select id="selectStudentPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select <include refid="Base_Column_List"/>
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.album_tag_) and mt.del_flag_ = 0 and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
        where find_in_set(s.id_,t.subject_id_) and s.del_flag_ = 0 ) as subjectNames
        ,if( t1.num>0,t1.num,0) as musicSheetCount
        ,if(t2.num >0,t2.num,0) as albumFavoriteCount
        from music_album t
        left join (select count(1) as num,amr.album_id_
            from album_music_relate amr
            join music_sheet ms on amr.music_sheet_id_ = ms.id_
            left join sys_user su on su.id_ = ms.user_id_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS' and su.del_flag_ = 0
        group by amr.album_id_) t1 on t1.album_id_ = t.id_
        left join (select count(1) as num,af.album_id_ from album_favorite af
            left join sys_user su on af.user_id_ = su.id_
            where su.del_flag_ = 0 group by af.album_id_
        ) t2 on t2.album_id_ = t.id_
        <where>
            t.del_flag_ = 0
            <if test="query.idAndName != null and query.idAndName != ''">
                and (t.id_ like concat('%',#{query.idAndName},'%') or
                t.album_name_ like concat('%',#{query.idAndName},'%'))
            </if>
            <if test="query.albumTagIdList != null and query.albumTagIdList.size() != 0">
                and
                <foreach collection="query.albumTagIdList" open="(" close=")" separator="or" item="item">
                    find_in_set(#{item},t.album_tag_)
                </foreach>
            </if>
            <if test="query.subjectIdList != null and query.subjectIdList.size() != 0">
                and
                <foreach collection="query.subjectIdList"  open="(" close=")"  separator="or" item="item">
                    find_in_set(#{item},t.subject_id_)
                </foreach>
            </if>
            <if test="query.albumStatus != null">
                and t.album_status_ = #{query.albumStatus}
            </if>
            <if test="query.auditVersion != null">
                and t.audit_version_ = #{query.auditVersion}
            </if>
        </where>
        <if test="query.sortBy != null">
            <choose>
                <when test="query.sortBy == 1">
                    order by t2.num desc
                </when>
            </choose>
        </if>
        <if test="query.sortBy == null">
            order by t.id_ desc
        </if>
    </select>
</mapper>