<?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.ym.mec.biz.dal.dao.ImLiveRoomReservationDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.ImLiveRoomReservation">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="room_uid_" jdbcType="VARCHAR" property="roomUid"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="push_state_" jdbcType="INTEGER" property="pushState"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , room_uid_, user_id_, push_state_, create_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.ImLiveRoomReservation">
        insert into im_live_room_reservation(room_uid_, user_id_, push_state_, create_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.roomUid}, #{entity.userId}, #{entity.pushState}, #{entity.createTime})
        </foreach>
    </insert>

    <select id="queryPageStudent" resultType="com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomVo">
        select distinct a.id_ AS id,
        a.tenant_id_ AS tenantId,
        t.name_ AS tenantName,
        t.logo_ AS tenantLogo,
        a.os_ AS os,
        a.room_uid_ AS roomUid,
        a.room_title_ AS roomTitle,
        a.live_remark_ AS liveRemark,
        a.speaker_id_ AS speakerId,
        b.real_name_ AS speakerName,
        b.im_token_ as imToken,
        b.avatar_ AS speakerPic,
        a.live_start_time_ AS liveStartTime,
        a.live_state_ AS liveState,
        a.room_state_ AS roomState,
        c.real_name_ AS createdByName,
        a.pre_template_ AS preTemplate,
        a.room_config_ AS roomConfig,
        a.popularize_ AS popularize,
        a.popularize_type_ AS popularizeType,
        IF(d.user_id_ is null, 0, 1) as reserve
        from (
        select *
        from im_live_broadcast_room
        where popularize_type_ = 'ALL'
        union all
        select a.*
        from im_live_broadcast_room as a
        inner join im_live_room_purview as b on a.room_uid_ = b.room_uid_
        where b.user_id_ = #{param.userId}
        ) as a
        left join tenant_info AS t on a.tenant_id_ = t.id_
        left join sys_user AS b on a.speaker_id_ = b.id_
        left join sys_user AS c on a.created_by_ = c.id_
        left join im_live_room_reservation as d on a.room_uid_ = d.room_uid_ and d.user_id_ = #{param.userId}
        <where>
        a.room_state_ in(0, 2)
            <if test="param.tenantId != null ">
                AND a.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.liveState != null">
                and a.live_state_ = #{param.liveState}
            </if>
            <if test="param.startTime != null">
                <![CDATA[ AND a.live_start_time_  >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime != null">
                <![CDATA[ AND a.live_start_time_  <= #{param.endTime} ]]>
            </if>
            <if test="param.popularize != null">
                and a.popularize_ = #{param.popularize}
            </if>
            <if test="param.roomUid != null">
                and a.room_uid_ = #{param.roomUid}
            </if>
        </where>
    </select>

    <select id="selectCountRoomReservation" resultType="com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomVo">
        select
        lbr.room_uid_ as roomUid,
        count(1) as roomReservationNum
        from im_live_room_reservation lbr

        <where>
            <if test="roomIdList != null and roomIdList.size() != 0">
                and lbr.room_uid_ in
                <foreach collection="roomIdList" separator="," close=")" open="(" item="item" >
                    #{item}
                </foreach>
            </if>
        </where>
        group by lbr.room_uid_
    </select>

    <select id="queryMusicGroupIds" resultType="string">
        select group_concat(music_group_id_)
        from student_registration
        where user_id_ = #{userId}
          and music_group_status_ != 'QUIT'
        group by user_id_
    </select>

</mapper>
