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

	 
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.user_id_ AS userId
        , t.tenant_id_ AS tenantId
        , t.tenant_album_id_ AS tenantAlbumId
        , t.order_no_ AS orderNo
        , t.source_type_ AS sourceType
        , t.client_type_ AS clientType
        , t.sub_order_no_ AS subOrderNo
        , t.start_time_ AS startTime
        , t.end_time_ AS endTime
        , t.create_time_ AS createTime
        , t.create_by_ AS createBy
        , t.update_time_ AS updateTime
        , t.type_ AS type
        , t.times_ AS times
        , t.msg_status_ AS msgStatus
        , t.reason_ AS reason
        </sql> 
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumWrapper$TenantAlbum">
		SELECT         
        	<include refid="baseColumns" />
		FROM user_tenant_album_record t
        <where>

        </where>
	</select>
    <select id="selectTenantIds" resultType="java.lang.Long">
        select distinct tenant_album_id_
        from user_tenant_album_record
        <where>
            end_time_ > now()
          <if test="id != null">
              and user_id_ = #{id}
          </if>
        </where>
    </select>
    <select id="selectTenantAlbumInfo" resultType="com.yonge.cooleshow.biz.dal.entity.TenantAlbum">
        select id_ AS id,
               name_ AS name,
               describe_ AS `describe`,
               cover_img_ AS coverImg,
               subject_types_ AS subjectTypes
        FROM tenant_album
        <where>
            <if test="tenantIds != null">
                and id_ in
                <foreach collection="tenantIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>

    </select>
    <select id="ifBuy" resultType="java.lang.Long">
        select distinct tenant_album_id_
        from user_tenant_album_record
        <where>
            end_time_ > now()
            <if test="tenantAlbumId != null and tenantAlbumId.size() != 0">
                and tenant_album_id_ in
                <foreach collection="tenantAlbumId" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="studentId != null">
                and user_id_ = #{studentId}
            </if>
        </where>

    </select>

    <select id="selectTemporaryRecord" resultType="com.yonge.cooleshow.biz.dal.wrapper.UserTenantAlbumRecordWrapper$UserTenantAlbumRecord">
        select
        <include refid="baseColumns"/>,
        u.phone_ as phone
        from (
        select max(id_) as id_ from user_tenant_album_record group by user_id_
        ) a
        left join user_tenant_album_record t on a.id_ = t.id_
        left join sys_user u on t.user_id_ = u.id_
        where t.end_time_ &lt;= now()
        and (msg_status_ = 0 or msg_status_ is null)
        order by end_time_ desc
    </select>

    <select id="selectUserTenantAlbumRecordPage" resultType="com.yonge.cooleshow.biz.dal.wrapper.UserTenantAlbumRecordWrapper$UserTenantAlbumRecord">
        SELECT
        <include refid="baseColumns" />
        ,su.username_ createName
        FROM user_tenant_album_record t
        <if test="param.keyword != null and param.keyword.trim() != ''">
            LEFT JOIN tenant_album ta on t.tenant_album_id_ = ta.id_
        </if>
        LEFT JOIN sys_user su on su.id_ = t.create_by_
        <where>
            <if test="param.keyword != null and param.keyword.trim() != ''">
                AND (
                su.username_ LIKE concat('%',#{param.keyword},'%')
                OR ta.name_ LIKE concat('%',#{param.keyword},'%')
                )
            </if>
            <if test="param.userId != null">
                AND t.user_id_ = #{param.userId}
            </if>
            <if test="param.sourceType != null">
                AND t.source_type_ = #{param.sourceType}
            </if>
            <if test="param.startTime != null">
                AND t.create_time_ >= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                AND #{param.endTime} >= t.create_time_
            </if>
        </where>
        ORDER BY t.id_ DESC
    </select>
</mapper>