<?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.yushu.system.mapper.SysChatMessageMapper">
    
    <resultMap type="SysChatMessage" id="SysChatMessageResult">
        <result property="messageId"    column="message_id"    />
        <result property="conversationId"    column="conversation_id"    />
        <result property="senderId"    column="sender_id"    />
        <result property="senderName"    column="sender_name"    />
        <result property="messageType"    column="message_type"    />
        <result property="content"    column="content"    />
        <result property="replyToMessageId"    column="reply_to_message_id"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSysChatMessageVo">
        select message_id, conversation_id, sender_id, sender_name, message_type, content, reply_to_message_id, status, create_time, update_time from sys_chat_message
    </sql>

    <select id="selectSysChatMessageList" parameterType="SysChatMessage" resultMap="SysChatMessageResult">
        <include refid="selectSysChatMessageVo"/>
        <where>  
            <if test="conversationId != null  and conversationId != ''"> and conversation_id = #{conversationId}</if>
            <if test="senderId != null "> and sender_id = #{senderId}</if>
            <if test="senderName != null  and senderName != ''"> and sender_name like concat('%', #{senderName}, '%')</if>
            <if test="messageType != null  and messageType != ''"> and message_type = #{messageType}</if>
            <if test="content != null  and content != ''"> and content like concat('%', #{content}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectSysChatMessageByMessageId" parameterType="Long" resultMap="SysChatMessageResult">
        <include refid="selectSysChatMessageVo"/>
        where message_id = #{messageId}
    </select>

    <!-- 查询会话的消息列表 -->
    <select id="selectConversationMessages" resultMap="SysChatMessageResult">
        <include refid="selectSysChatMessageVo"/>
        WHERE conversation_id = #{conversationId} 
        AND status != '0'
        ORDER BY create_time ASC
        <if test="pageNum != null and pageSize != null and pageNum >= 0 and pageSize > 0">
            LIMIT #{pageSize} OFFSET #{pageNum}
        </if>
    </select>

    <!-- 查询会话的所有消息（不分页） -->
    <select id="selectAllConversationMessages" resultMap="SysChatMessageResult">
        <include refid="selectSysChatMessageVo"/>
        WHERE conversation_id = #{conversationId} 
        AND status != '0'
        ORDER BY create_time ASC
    </select>

    <!-- 统计会话未读消息数 -->
    <select id="countUnreadMessages" resultType="int">
        SELECT cm.unread_count
        FROM sys_conversation_member cm
        WHERE cm.conversation_id = #{conversationId} 
        AND cm.user_id = #{userId}
    </select>
        
    <insert id="insertSysChatMessage" parameterType="SysChatMessage" useGeneratedKeys="true" keyProperty="messageId">
        insert into sys_chat_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="conversationId != null and conversationId != ''">conversation_id,</if>
            <if test="senderId != null">sender_id,</if>
            <if test="senderName != null">sender_name,</if>
            <if test="messageType != null">message_type,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="replyToMessageId != null">reply_to_message_id,</if>
            <if test="status != null">status,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="conversationId != null and conversationId != ''">#{conversationId},</if>
            <if test="senderId != null">#{senderId},</if>
            <if test="senderName != null">#{senderName},</if>
            <if test="messageType != null">#{messageType},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="replyToMessageId != null">#{replyToMessageId},</if>
            <if test="status != null">#{status},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSysChatMessage" parameterType="SysChatMessage">
        update sys_chat_message
        <trim prefix="SET" suffixOverrides=",">
            <if test="conversationId != null and conversationId != ''">conversation_id = #{conversationId},</if>
            <if test="senderId != null">sender_id = #{senderId},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="messageType != null">message_type = #{messageType},</if>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="replyToMessageId != null">reply_to_message_id = #{replyToMessageId},</if>
            <if test="status != null">status = #{status},</if>
            update_time = sysdate()
        </trim>
        where message_id = #{messageId}
    </update>

    <delete id="deleteSysChatMessageByMessageId" parameterType="Long">
        delete from sys_chat_message where message_id = #{messageId}
    </delete>

    <delete id="deleteSysChatMessageByMessageIds" parameterType="String">
        delete from sys_chat_message where message_id in 
        <foreach item="messageId" collection="array" open="(" separator="," close=")">
            #{messageId}
        </foreach>
    </delete>

    <!-- 标记会话为已读 -->
    <update id="markConversationAsRead">
        UPDATE sys_conversation_member 
        SET unread_count = 0
        WHERE conversation_id = #{conversationId} AND user_id = #{userId}
    </update>

    <!-- 获取会话的最后一条消息 -->
    <select id="selectLastMessage" parameterType="String" resultMap="SysChatMessageResult">
        <include refid="selectSysChatMessageVo"/>
        WHERE conversation_id = #{conversationId} 
        AND status != '0'
        ORDER BY create_time DESC
        LIMIT 1
    </select>

    <!-- 增加用户的未读消息数 -->
    <update id="incrementUnreadCount">
        UPDATE sys_conversation_member 
        SET unread_count = unread_count + 1
        WHERE conversation_id = #{conversationId} AND user_id = #{userId}
    </update>

</mapper>
