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

	 
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.room_uid_ AS roomUid
        , t.user_id_ AS userId
        , t.join_time_ AS joinTime
        , t.total_time_ AS totalTime
        , t.online_status_ AS onlineStatus
        , t.ban_status_ AS banStatus
        , t.live_room_status_ AS liveRoomStatus
        , t.whether_mic_status_ AS whetherMicStatus
        , t.microphone_flag_ AS microphoneFlag
        , t.fingerprint_ AS fingerprint
        , t.visitor_name_ AS visitorName
        , t.create_time_ AS createTime
        </sql> 
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.entity.ImLiveBroadcastRoomMember">
		SELECT         
        	<include refid="baseColumns" />
		FROM im_live_broadcast_room_member t
	</select>


    <select id="queryMemberNum"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.liveroom.ImLiveBroadcastRoomMemberWrapper$RoomMemberNumDto">
        select room_uid_ as roomUid,
        count(1) as totalNum ,
        sum(if(online_status_ = 1 and live_room_status_ = 1,1,0)) as onlineNum
        from im_live_broadcast_room_member
        where room_uid_ = #{roomUid}
        group by room_uid_
    </select>


    <update id="userWhetherMic">
        update im_live_broadcast_room_member
        set whether_mic_status_ = #{whetherMicStatus}
        where room_uid_ = #{roomUid} and user_id_ = #{userId}
    </update>


    <!--学生声部信息-->
    <select id="selectStudentSubject"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.liveroom.ImLiveBroadcastRoomMemberWrapper$ImLiveBroadcastRoomMember">
        SELECT
        st.user_id_ as studentId, b.name_ as subName
        FROM
        student st left join subject as b on st.subject_id_ = b.id_
        WHERE st.user_id_ IN (<foreach collection="studentIds" separator="," item="item">#{item}</foreach>)
    </select>

    <select id="queryMemberPage"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.liveroom.ImLiveBroadcastRoomMemberWrapper$ImLiveBroadcastRoomMember">

        select
        a.room_uid_ as roomUid,
        a.user_id_ as studentId,
        a.whether_mic_status_ as whetherMicStatus,
        a.fingerprint_ as fingerprint,
        IFNULL(su.username_, a.visitor_name_) as studentName,
        su.avatar_ as avatar,
        su.phone_ as phone,
        a.join_time_ as joinTime,
        a.total_time_ as totalViewTime,
        a.microphone_flag_ as microphoneFlag
        from
        im_live_broadcast_room_member as a
        left join sys_user as su on su.id_ = a.user_id_
        where a.room_uid_ = #{param.roomUid}
        <if test="param.search != null ">
            AND (
            a.user_id_ LIKE CONCAT('%', #{param.search},'%')
            OR a.visitor_name_ LIKE CONCAT('%', #{param.search},'%')
            OR su.username_ LIKE CONCAT('%', #{param.search},'%')
            )
        </if>
        <if test="param.onlineStatus != null">
            AND (a.online_status_ = #{param.onlineStatus} and a.live_room_status_ = 1)
        </if>
        <if test="param.whetherMicStatus != null">
            and a.whether_mic_status_ = #{param.whetherMicStatus}
        </if>
        <if test="param.visitorQuery != null">
            <if test="param.visitorQuery == 1">
                AND a.fingerprint_ IS NOT NULL
            </if>
            <if test="param.visitorQuery == 0">
                AND a.fingerprint_ IS NULL
            </if>
        </if>
        group by a.user_id_
        ORDER BY a.join_time_
    </select>


    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true">
        insert into im_live_broadcast_room_member(room_uid_, user_id_, join_time_, total_time_,online_status_,live_room_status_) values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.roomUid}, #{entity.userId}, #{entity.joinTime}, #{entity.totalTime},0,0)
        </foreach>
        ON DUPLICATE KEY UPDATE join_time_ = VALUES(join_time_), total_time_ = VALUES(total_time_),online_status_ = 0,live_room_status_ = 0
    </insert>


    <update id="updateOnlineStatus">
        update im_live_broadcast_room_member
        set online_status_ = #{onlineStatus}
        where room_uid_ = #{groupId} and user_id_ in <foreach collection="userIds" item="userId" open="(" separator="," close=")">#{userId}</foreach>
    </update>

    <update id="updateLiveRoomStatus">
        update im_live_broadcast_room_member
        set live_room_status_ = #{liveRoomStatus}
        where room_uid_ = #{groupId} and user_id_ in <foreach collection="userIds" item="userId" open="(" separator="," close=")">#{userId}</foreach>

    </update>


    <select id="queryMember" resultType="com.yonge.cooleshow.biz.dal.entity.ImLiveBroadcastRoomMember">
        select * from im_live_broadcast_room_member
        where room_uid_ = #{groupId} and user_id_ in <foreach collection="userIds" item="userId" open="(" separator="," close=")">#{userId}</foreach>

    </select>
</mapper>