<?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="virtual_number_" property="virtualNumber" />
        <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"/>
        <result column="album_type_" jdbcType="BIGINT" property="albumType"/>
    </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.virtual_number_,
        t.create_time_,
        t.audit_version_,
        t.create_by_,
        t.album_price_,
        t.payment_type_,
        t.update_time_,
        t.update_by_,
        t.album_type_
    </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
        ,(select count(1) as num
            from album_music_relate amr
            join music_sheet ms on amr.music_sheet_id_ = ms.id_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS'
            and amr.album_id_ = t.id_) as musicSheetCount
        ,t2.num as albumFavoriteCount
        <if test="query.userId != null">
            ,(select count(1) from album_favorite af where af.album_id_ = t.id_ and af.user_id_ = #{query.userId}) as favorite
        </if>
        from music_album t
        left join (select count(1) as num,af.album_id_ from album_favorite af
            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>
                    or t.subject_id_ is null or t.subject_id_ = ''
                )
            </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>
            <if test="query.paymentType != null">
                and t.payment_type_ = #{query.paymentType}
            </if>
            <if test="query.albumIds != null and query.albumIds.size() != 0">
                and t.id_ in
                <foreach collection="query.albumIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        order by t.sort_number_ desc
        <if test="query.sortBy != null">
            <choose>
                <when test="query.sortBy == 1">
                    ,t2.num desc
                </when>
            </choose>
        </if>
        ,t.id_ desc
    </select>

    <select id="selectFavoritePage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select distinct <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
        <if test="query.albumFavorite != null">
            left join album_favorite af2 on af2.album_id_ = t.id_
        </if>
        <if test="query.albumPurchased != null">
            JOIN music_sheet_purchase_record t2 ON (t.id_ = t2.music_sheet_id_)
        </if>
        <where>
            <if test="query.albumFavorite != null">
                t.del_flag_ = 0
                <if test="query.studentId != null">
                    and af2.user_id_ = #{query.studentId}
                </if>
                <if test="clientType != null">
                    AND af2.client_type_ = #{clientType}
                </if>
            </if>
            <if test="query.albumPurchased != null">
                <if test="query.clientType != null">
                    AND t2.client_type_ = #{query.clientType}
                </if>
                <if test="query.orderStatus != null">
                    AND t2.order_status_ = #{query.orderStatus}
                </if>
                <if test="query.purchaseType != null">
                    AND t2.purchase_type_ = #{query.purchaseType}
                </if>
                <if test="query.courseGift != null">
                    AND t2.course_music_album_id_ <choose><when test="query.courseGift == 0"> = </when><otherwise> > </otherwise> </choose> 0
                </if>
                <if test="query.studentId != null">
                    AND t2.student_id_ = #{query.studentId}
                </if>
            </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>
                or t.subject_id_ is null or t.subject_id_ = ''
                )
            </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>
        <if test="query.albumFavorite != null">
            order by  af2.favorite_time_ desc
        </if>
    </select>

    <select id="selectFavoriteNumAndMusicNum" resultType="com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo">
        select
        t.id_ as id,
        ifnull(t1.num,0) as musicSheetCount
        ,ifnull(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_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS'
            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
              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
            , (
                select count(1) as num from album_music_relate amr
                join music_sheet ms on amr.music_sheet_id_ = ms.id_
                where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS'
                and amr.album_id_ = t.id_
            ) as musicSheetCount
            ,(
                select count(1) as num from album_favorite af
                where af.album_id_ = t.id_
            ) as albumFavoriteCount
        from music_album t
        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
        ,(select count(1) as num
            from album_music_relate amr
            join music_sheet ms on amr.music_sheet_id_ = ms.id_
            where ms.del_flag_ = 0 and ms.state_ = 1 and ms.audit_status_ = 'PASS'
           and amr.album_id_ = t.id_) as musicSheetCount
        ,if(t2.num >0,t2.num,0) as albumFavoriteCount
        from music_album t
        <if test="query.musicId != null">
            JOIN album_music_relate t1 ON (t.id_ = t1.album_id_ AND t1.music_sheet_id_ = #{query.musicId})
        </if>
        left join (select count(1) as num,af.album_id_ from album_favorite af
            group by af.album_id_
        ) t2 on t2.album_id_ = t.id_
        <if test="clientType != null and query.userId != null">
            left join music_sheet_purchase_record t3 on t3.music_sheet_id_ = t.id_ and t3.client_type_ = #{clientType} and t3.student_id_ = #{query.userId}
        </if>
        <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>
                or t.subject_id_ is null or t.subject_id_ = ''
                )
            </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>
        order by
        <if test="clientType != null and query.userId != null">
            field(t3.id_,null,t3.id_) desc,
        </if>

        t.sort_number_ desc
        <if test="query.sortBy != null">
            <choose>
                <when test="query.sortBy == 1">
                    ,t2.num desc
                </when>
            </choose>
        </if>
        ,t.id_ desc
    </select>

    <!--专辑曲目付费方式统计-->
    <select id="selectAlbumMusicSheetStatInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.album_id_ AS id, COUNT(t1.id_) AS total
        FROM album_music_relate t1 JOIN music_sheet t2 ON (t1.music_sheet_id_ = t2.id_)
        <where>
            <if test="albumId != null">
                AND t1.album_id_ = #{albumId}
            </if>
            <if test="paymentType != null">
                AND t2.payment_type_ = #{paymentType}
            </if>
        </where>
        GROUP BY t1.album_id_
    </select>
    <!--专辑曲目付费方式统计-->
    <select id="selectStudentOrderPage" resultType="com.yonge.cooleshow.biz.dal.vo.StudentMusicAlbumOrderVo">
        select distinct mspr.music_sheet_id_ as musicAlbumId
        ,ms.album_name_ as musicAlbumName
        ,ms.album_cover_url_ as titleImg
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,ms.album_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as TagNames
        ,mspr.purchase_price_ as purchasePrice
        ,mspr.order_no_ as orderNo
        ,mspr.purchase_time_ as purchaseTime
        from music_sheet_purchase_record mspr
        left join music_album ms on mspr.music_sheet_id_ = ms.id_
        <where>
            mspr.order_status_ = 'PAID' and mspr.purchase_type_ = 'ALBUM'
            <if test="param.idAndName != null and param.idAndName != ''">
                and (ms.id_ like concat('%',#{param.idAndName} ,'%')
                or ms.album_name_ like concat('%',#{param.idAndName},'%'))
            </if>
            <if test="param.studentId != null">
                and mspr.student_id_ = #{param.studentId}
            </if>
            <if test="param.orderNo != null and param.orderNo != ''">
                and mspr.order_no_ = #{param.orderNo}
            </if>
            <if test="param.musicTagList != null and param.musicTagList.size() != 0">
                and
                <foreach collection="param.musicTagList" separator="or" item="item"  open="(" close=")" >
                    find_in_set(#{item},ms.album_tag_)
                </foreach>
            </if>
            <if test="param.startTime != null">
                and mspr.purchase_time_ &gt; #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and mspr.purchase_time_ &lt; #{param.endTime}
            </if>
        </where>
        order by mspr.id_ desc
    </select>
</mapper>