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

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , create_time, update_time, last_message, member_count, chat_type, fk_appid, creator, name, attributes, system_flag
    </sql>

    <select id="getImConversationById" resultType="com.wecloud.im.param.ImConversationQueryVo">
        select
        <include refid="Base_Column_List"/>
        from im_conversation where id = #{id}
    </select>

    <select id="getImConversationPageList" parameterType="com.wecloud.im.param.ImConversationPageParam"
            resultType="com.wecloud.im.param.ImConversationQueryVo">
        select
        <include refid="Base_Column_List"/>
        from im_conversation
    </select>
    <select id="getMyImConversationListAndMsgCount" resultType="com.wecloud.im.vo.MyConversationListVo">
        SELECT imConversation.id,
               imConversation.create_time,
               imConversation.`name`,
               imConversation.chat_type,
               imConversation.member_count,
               imConversation.attributes            as attribute,
               imConversation.system_flag,
               im_client.client_id                  AS creator,
               (SELECT COUNT(im_inbox.id)
                FROM im_inbox
                WHERE im_inbox.fk_conversation_id = imConversation.id
                  AND im_inbox.receiver = #{clientId}
                  AND im_inbox.read_msg_status = 0) AS msg_not_read_count,
               (
                   SELECT GROUP_CONCAT(im_client.client_id)
                   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 = imConversation.id
               )                                    AS members
        FROM im_conversation_members AS imConversationMembers
                 INNER JOIN im_conversation AS imConversation
                            ON imConversation.id = imConversationMembers.fk_conversation_id
                 INNER JOIN im_client AS im_client ON im_client.id = imConversation.creator
        WHERE imConversationMembers.fk_client_id = #{clientId}
          AND imConversationMembers.display_status = 1
        GROUP BY imConversation.id
    </select>

    <select id="getMyImConversationList" resultType="com.wecloud.im.entity.ImConversation">
        SELECT imConversation.*
        FROM im_conversation_members AS imConversationMembers
                 INNER JOIN im_conversation imConversation
                            ON imConversation.id = imConversationMembers.fk_conversation_id
        WHERE imConversationMembers.fk_client_id = #{clientId}
    </select>

    <select id="getRepetitionConversationSingle" resultType="com.wecloud.im.entity.ImConversation">
        -- 查询重复的单聊会话第一条
        select con.* from im_conversation con
            inner join im_conversation_members mem1
                on con.id = mem1.fk_conversation_id and mem1.fk_client_id = #{clientId1}
            inner join im_conversation_members mem2
                on con.id = mem2.fk_conversation_id and mem2.fk_client_id = #{clientId2}
          where con.chat_type = 1 and con.member_count = 2 order by con.id asc LIMIT 1
    </select>

    <select id="getRepetitionConversationAttributes" resultType="java.lang.Long">
        SELECT *
        FROM im_conversation
        WHERE id IN (
            SELECT im_conversation_members.fk_conversation_id
            FROM im_conversation_members
                     INNER JOIN (SELECT *
                                 FROM im_conversation_members
                                 WHERE im_conversation_members.fk_client_id = #{clientId2}) AS im_conversation_members2
                                ON im_conversation_members.fk_conversation_id =
                                   im_conversation_members2.fk_conversation_id
            WHERE im_conversation_members.fk_client_id = #{clientId1}
        )
          AND attributes = CAST(#{attributes} AS json) LIMIT 1
    </select>

    <update id="addMemberCount">
        update im_conversation set member_count = member_count + #{addCount} where fk_appid = #{appId} and id = #{conversationId}
    </update>

    <update id="updateMemberCount">
        update im_conversation set chat_type = 3 where fk_appid = #{appId} and id = #{conversationId}
    </update>

</mapper>
