<?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="org.xyzh.ai.mapper.TbChatMessageMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.ai.dto.TbChatMessage">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="message_id" property="messageId" jdbcType="VARCHAR"/>
        <result column="dify_message_id" property="difyMessageId" jdbcType="VARCHAR"/>
        <result column="chat_id" property="chatId" jdbcType="VARCHAR"/>
        <result column="role" property="role" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="files" property="files" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="comment" property="comment" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        optsn, message_id, dify_message_id, chat_id, role, content, files, comment,
        create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertChatMessage" parameterType="org.xyzh.api.ai.dto.TbChatMessage">
        INSERT INTO ai.tb_chat_message (
            optsn, message_id, chat_id, role, content
            <if test="difyMessageId != null">, dify_message_id</if>
            <if test="files != null">, files</if>
            <if test="comment != null">, comment</if>
        ) VALUES (
            #{optsn}, #{messageId}, #{chatId}, #{role}, #{content}
            <if test="difyMessageId != null">, #{difyMessageId}</if>
            <if test="files != null">, #{files, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="comment != null">, #{comment}</if>
        )
    </insert>

    <update id="updateChatMessage" parameterType="org.xyzh.api.ai.dto.TbChatMessage">
        UPDATE ai.tb_chat_message
        <set>
            <if test="content != null">content = #{content},</if>
            <if test="comment != null">comment = #{comment},</if>
            update_time = now()
        </set>
        WHERE message_id = #{messageId} AND deleted = false
    </update>

    <update id="deleteChatMessage" parameterType="org.xyzh.api.ai.dto.TbChatMessage">
        UPDATE ai.tb_chat_message
        SET deleted = true,
            delete_time = now()
        WHERE message_id = #{messageId} AND deleted = false
    </update>

    <select id="selectMessageById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_chat_message
        WHERE message_id = #{messageId} AND deleted = false
    </select>

    <select id="selectMessagesByChatId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_chat_message
        WHERE chat_id = #{chatId} AND deleted = false
        ORDER BY create_time ASC
    </select>

    <select id="selectMessagePage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_chat_message
        WHERE chat_id = #{chatId} AND deleted = false
        ORDER BY create_time ASC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countMessages" resultType="long">
        SELECT COUNT(*)
        FROM ai.tb_chat_message
        WHERE chat_id = #{chatId} AND deleted = false
    </select>

    <update id="deleteMessagesByChatId">
        UPDATE ai.tb_chat_message
        SET deleted = true,
            delete_time = now()
        WHERE chat_id = #{chatId} AND deleted = false
    </update>
</mapper>
