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

	 
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.tenant_group_id_ AS tenantGroupId
        , t.tenant_album_id_ AS tenantAlbumId
        , t.status_ AS status
        , t.purchase_num_ AS purchaseNum
        , t.purchase_period_ AS purchasePeriod
        , t.original_price_ AS originalPrice
        , t.actual_price_ AS actualPrice
        , t.buy_times_flag_ AS buyTimesFlag
        , t.buy_times_ AS buyTimes
        , t.del_flag_ AS delFlag
        , t.create_time_ AS createTime
        , t.create_by_ AS createBy
        , t.update_by_ AS updateBy
        , t.update_time_ AS updateTime
        </sql> 
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper$TenantGroupAlbumVo">
		SELECT         
        	<include refid="baseColumns" />
        ,tg.name_ tenantGroupName
        ,ta.name_ tenantAlbumName
		FROM tenant_group_album t
        LEFT JOIN tenant_group tg ON t.tenant_group_id_ = tg.id_
        LEFT JOIN tenant_album ta ON t.tenant_album_id_ = ta.id_
        <where>
            t.del_flag_ = 0
            <if test="param.tenantAlbumId != null">
                AND t.tenant_album_id_ = #{param.tenantAlbumId}
            </if>
            <if test="param.tenantId != null">
                AND tg.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.tenantGroupId != null">
                AND tg.id_ = #{param.tenantGroupId}
            </if>
        </where>
	</select>

    <select id="getBuyAlbumInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper$BuyTenantAlbum">
        SELECT
        distinct
            t.id_,
        t.name_,
        t.describe_,
        t.cover_img_,
        t.music_num_,
        t.subject_types_,
        t.purchase_types_,
        t.purchase_json_
        <if test="param.tenantGroupId != null or param.tenantGroupAlbumId != null">
            ,
            t2.id_ as tenantGroupAlbumId,
            t2.tenant_group_id_,
            t2.purchase_num_,
            t2.purchase_period_,
            t2.original_price_,
            t2.actual_price_,
            t2.buy_times_flag_,
            t2.buy_times_
        </if>
        from tenant_album t

        <if test="param.tenantGroupId != null or param.tenantGroupAlbumId != null">
            left join tenant_group_album t2 on t2.tenant_album_id_ = t.id_
        </if>
        <if test="param.tenantId != null">
            left join tenant_album_ref t1 on t1.tenant_album_id_ = t.id_
        </if>
        <where>
            t.del_flag_ = 0 and t.status_ = 1
            <if test="param.tenantId != null">
                and t1.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.tenantGroupId != null  or param.tenantGroupAlbumId != null">
                and t2.status_ = 1 and t2.del_flag_ = 0
                <if test="param.tenantGroupId != null">
                    and t2.tenant_group_id_ = #{param.tenantGroupId}
                </if>
                <if test="param.tenantGroupAlbumId != null">
                    and t2.id_ = #{param.tenantGroupAlbumId}
                </if>
            </if>
            <if test="param.tenantAlbumIds != null and param.tenantAlbumIds.size() != 0">
                and t.id_ in
                <foreach collection="param.tenantAlbumIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by t.id_ desc
    </select>

    <select id="countGroupAlbumByGroupId" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper$TenantGroupAlbumCount">
        select t.tenant_group_id_ as tenantGroupId, count(1) as count
        from tenant_group_album t
        where find_in_set(t.tenant_group_id_, #{ids})
          and t.del_flag_ = 0
          and t.status_ = 1
        group by t.tenant_group_id_
    </select>
</mapper>