<?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.ImLiveRoomPurviewDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.ImLiveRoomPurview">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="room_uid_" jdbcType="VARCHAR" property="roomUid"/>
        <result column="user_id_" jdbcType="VARCHAR" property="userId"/>
        <result column="type_" jdbcType="VARCHAR" property="type"/>
        <result column="created_by_" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , room_uid_, user_id_, type_, created_by_, created_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.ImLiveRoomPurview">
        insert into im_live_room_purview(room_uid_, user_id_, type_, created_by_, created_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.roomUid}, #{entity.userId}, #{entity.type}, #{entity.createdBy}, #{entity.createdTime})
        </foreach>
        ON DUPLICATE KEY UPDATE
        created_by_ = VALUES(created_by_),
        created_time_ = VALUES(created_time_)
    </insert>

    <select id="selectRoomPurviewStudent" parameterType="map" resultType="com.ym.mec.biz.dal.dto.SysUserDto">
        SELECT distinct
        s.user_id_ userId,
        su.username_ username,
        su.phone_ phone,
        o.name_ organName,
        sb.name_ subjectName
        FROM student s
        <choose>
            <when test="param.groupIds != null">
                INNER JOIN
            </when>
            <otherwise>
                LEFT JOIN
            </otherwise>
        </choose>
                (select
                     b.user_id_,
                     replace(group_concat(a.name_,'|'),',','')  imGroupName,
                     group_concat( a.id_ ) as im_group_id_
                  from im_group a
                  left join im_group_member b on a.id_ = b.im_group_id_
                 <where>
                     <if test="param.groupIds != null">
                         and find_in_set(a.id_, #{param.groupIds})
                     </if>
                 </where>
        group by b.user_id_) gm on s.user_id_ = gm.user_id_
        LEFT JOIN sys_user su ON su.id_ = s.user_id_
        LEFT JOIN organization o ON o.id_ = su.organ_id_
        LEFT JOIN `subject` sb ON sb.id_ = s.subject_id_list_
        LEFT JOIN cooperation_organ co on co.organ_id_ = o.id_
        <choose>
            <when test="param.teamIds != null">
                INNER JOIN
            </when>
            <otherwise>
                LEFT JOIN
            </otherwise>
        </choose>
            (select
                user_id_,
                replace(group_concat(a.name_,'|'),',','')  musicGroupName
                from
                music_group a left join
                student_registration b  on a.id_ = b.music_group_id_
                where b.music_group_status_ != 'QUIT'
                <if test="param.teamIds != null">
                     and find_in_set(a.id_ ,#{param.teamIds})
                </if>
        group by b.user_id_) sr on sr.user_id_ = s.user_id_
        <where>
            <if test="param.tenantId != null ">
                AND s.tenant_id_ = #{param.tenantId}
            </if>
            <choose>
                <when test="param.inExist != null ">
                    AND s.user_id_ in (select user_id_ from im_live_room_purview where room_uid_ = #{param.roomUid})
                </when>
                <otherwise>
                    AND s.user_id_ not in (select user_id_ from im_live_room_purview where room_uid_ = #{param.roomUid})
                </otherwise>
            </choose>
            <if test="param.organIds != null and param.organIds != ''">
                AND FIND_IN_SET(su.organ_id_,#{param.organIds})
            </if>
            <if test="param.subjectId != null">
                AND FIND_IN_SET(s.subject_id_list_,#{param.subjectId})
            </if>
            <if test="param.schoolIds != null">
                AND FIND_IN_SET(s.cooperation_organ_id_ ,#{param.schoolIds})
            </if>
            <if test="param.search != null ">
                AND (
                s.`user_id_` LIKE CONCAT('%', #{param.search},'%')
                or su.phone_ like concat('%',#{param.search},'%')
                or su.username_ like concat('%',#{param.search},'%')
                )
            </if>
        </where>
    </select>

    <resultMap type="com.ym.mec.biz.dal.entity.ImGroup" id="ImGroup">
        <result column="id_" property="id" />
        <result column="name_" property="name" />
        <result column="introduce_" property="introduce" />
        <result column="member_num_" property="memberNum" />
        <result column="memo_" property="memo" />
        <result column="tags_" property="tags" />
        <result column="img_" property="img" />
        <result column="type_" property="type" />
        <result column="group_type_" property="groupType" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <select id="selectRoomPurviewGroup" parameterType="map" resultMap="ImGroup">
        select distinct a.*
        from im_group as a
                 inner join im_group_member b on a.id_ = b.im_group_id_
        where b.user_id_ = (param.userId)
        <if test="param.search != null and search != ''">
            AND (a.name_ like concat('%',#{param.search},'%') or a..tags_ like concat('%',#{param.search},'%'))
        </if>
        <if test="param.groupType != null">
            AND a.group_type_ = #{param.groupType}
        </if>
    </select>

</mapper>
