<?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.wecloud.im.mapper.ImConversationMembersMapper">

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , create_time, update_time, fk_appid, fk_conversation_id, fk_client_id
    </sql>

    <select id="getImConversationMembersById" resultType="com.wecloud.im.param.ImConversationMembersQueryVo">
        select
        <include refid="Base_Column_List"/>
        from im_conversation_members where id = #{id}
    </select>

    <select id="getImConversationMembersPageList" parameterType="com.wecloud.im.param.ImConversationMembersPageParam"
            resultType="com.wecloud.im.param.ImConversationMembersQueryVo">
        select
        <include refid="Base_Column_List"/>
        from im_conversation_members
    </select>
    <select id="getRestApiImConversationMembersList"
            resultType="com.wecloud.im.param.ApiImConversationMembersQueryVo">

        SELECT im_client.client_id AS clientId
        FROM im_conversation_members AS imConversationMembers
                 INNER JOIN im_client AS im_client ON im_client.id = imConversationMembers.fk_client_id
        WHERE fk_conversation_id = #{conversationId}

    </select>
    <select id="getImConversationMembersList" resultType="com.wecloud.im.vo.ConversationMemberVo">

        SELECT im_client.client_id                        as clientId,
               im_conversation_members.id                 as id,
               im_conversation_members.client_remark_name as clientRemarkName,
               im_client.head_portrait                    as headPortrait,
               im_client.nickname,
               im_conversation_members.attributes         AS memberAttributes,
               im_conversation_members.role               AS role,
               im_conversation_members.muted              AS muted,
               im_client.attributes                       AS clientAttributes
        FROM im_conversation_members AS im_conversation_members
                 INNER JOIN im_client AS im_client ON im_client.id = im_conversation_members.fk_client_id
        WHERE im_conversation_members.fk_conversation_id = #{param.conversationId}
        <if test="param.roles != null and param.roles.size() > 0">
            and im_conversation_members.role in
            <foreach collection="param.roles" item="role" index="index" open="(" close=")" separator=",">
                #{role}
            </foreach>
        </if>
        <if test="param.clientIds != null and param.clientIds.size() > 0">
            and im_client.client_id in
            <foreach collection="param.clientIds" item="clientId" index="index" open="(" close=")" separator=",">
                #{clientId}
            </foreach>
        </if>
        <if test="param.muted != null">
            AND im_conversation_members.muted = #{param.muted}
        </if>

    </select>

    <select id="findThousandGroupsByClientId" resultType="java.lang.Long">
        select a.id from im_conversation a
            inner join im_conversation_members b on a.id = b.fk_conversation_id
        where b.fk_client_id = #{clientId} and a.chat_type = 3
    </select>

    <update id="setAdminsForConversation">
        update im_conversation_members set role = #{role}, update_time=NOW()
        where fk_conversation_id = #{conversationId} and fk_client_id in
        <foreach collection="fkClientIds" item="fkClientId" index="index" open="(" close=")" separator=",">
            #{fkClientId}
        </foreach>
    </update>
    
    <delete id="deleteByConversationId">
        DELETE FROM im_conversation_members WHERE fk_conversation_id = #{conversationId}
    </delete>

</mapper>
