<?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.ImLiveBroadcastRoomMemberDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.ImLiveBroadcastRoomMember">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="tenant_id_" jdbcType="INTEGER" property="tenantId"/>
        <result column="room_uid_" jdbcType="VARCHAR" property="roomUid"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="join_time_" jdbcType="TIMESTAMP" property="joinTime"/>
        <result column="total_time_" jdbcType="INTEGER" property="totalTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        , tenant_id_, room_uid_, user_id_, join_time_, total_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.ImLiveBroadcastRoomMember">
        insert into im_live_broadcast_room_member(tenant_id_, room_uid_, user_id_, join_time_, total_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.tenantId}, #{entity.roomUid}, #{entity.userId}, #{entity.joinTime}, #{entity.totalTime})
        </foreach>
    </insert>

    <select id="queryMemberPage" resultType="com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomMemberVo">
        select i.room_uid_ as roomUid,
        i.room_title_ as roomTitle,
        tu.real_name_ speakerName,
        a.user_id_ as studentId,
        su.username_ as studentName,
        su.phone_ as phone,
        b.name_ as subName,
        o.name_ organName,
        GROUP_CONCAT(distinct mg.name_) musicGroupName,
        a.join_time_ as joinTime,
        a.total_time_ as totalViewTime
        from im_live_broadcast_room_member as a
        left join im_live_broadcast_room as i on a.room_uid_ = i.room_uid_
        left join sys_user as su on su.id_ = a.user_id_
        left join student st on su.id_ = st.user_id_
		LEFT JOIN student_registration sr ON sr.user_id_ = a.user_id_
		LEFT JOIN music_group mg ON  mg.id_ = sr.music_group_id_
        left join subject as b on st.subject_id_list_ = b.id_
		left join `sys_user` tu on tu.`id_` = i.`speaker_id_` 
		LEFT JOIN `organization` o on o.`id_` = su.`organ_id_` 
        where a.room_uid_ = #{param.roomUid}
        <if test="param.search != null ">
            AND (
            a.user_id_ LIKE CONCAT('%', #{param.search},'%')
            OR su.username_ LIKE CONCAT('%', #{param.search},'%')
            )
        </if>
        group by a.user_id_
    </select>

</mapper>
