<?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.AiMessageMapper">
    
    <resultMap type="AiMessage" id="AiMessageResult">
        <result property="messageId"         column="message_id"          />
        <result property="conversationId"    column="conversation_id"     />
        <result property="role"              column="role"                />
        <result property="content"           column="content"             />
        <result property="reasoning"         column="reasoning"           />
        <result property="tokens"            column="tokens"              />
        <result property="responseTime"      column="response_time"       />
        <result property="isError"           column="is_error"            />
        <result property="createTime"        column="create_time"         />
    </resultMap>

    <sql id="selectAiMessageVo">
        select message_id, conversation_id, role, content, reasoning, tokens, response_time, is_error, create_time
        from ai_message
    </sql>

    <select id="selectAiMessageList" parameterType="AiMessage" resultMap="AiMessageResult">
        <include refid="selectAiMessageVo"/>
        <where>  
            <if test="conversationId != null"> and conversation_id = #{conversationId}</if>
            <if test="role != null and role != ''"> and role = #{role}</if>
        </where>
        order by create_time asc
    </select>
    
    <select id="selectAiMessageByMessageId" parameterType="Long" resultMap="AiMessageResult">
        <include refid="selectAiMessageVo"/>
        where message_id = #{messageId}
    </select>

    <select id="selectMessagesByConversationId" parameterType="Long" resultMap="AiMessageResult">
        <include refid="selectAiMessageVo"/>
        where conversation_id = #{conversationId}
        order by create_time asc
    </select>
        
    <insert id="insertAiMessage" parameterType="AiMessage" useGeneratedKeys="true" keyProperty="messageId">
        insert into ai_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="conversationId != null">conversation_id,</if>
            <if test="role != null">role,</if>
            <if test="content != null">content,</if>
            <if test="reasoning != null">reasoning,</if>
            <if test="tokens != null">tokens,</if>
            <if test="responseTime != null">response_time,</if>
            <if test="isError != null">is_error,</if>
            create_time,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="conversationId != null">#{conversationId},</if>
            <if test="role != null">#{role},</if>
            <if test="content != null">#{content},</if>
            <if test="reasoning != null">#{reasoning},</if>
            <if test="tokens != null">#{tokens},</if>
            <if test="responseTime != null">#{responseTime},</if>
            <if test="isError != null">#{isError},</if>
            NOW(),
        </trim>
    </insert>

    <update id="updateAiMessage" parameterType="AiMessage">
        update ai_message
        <trim prefix="SET" suffixOverrides=",">
            <if test="content != null">content = #{content},</if>
            <if test="reasoning != null">reasoning = #{reasoning},</if>
            <if test="tokens != null">tokens = #{tokens},</if>
            <if test="responseTime != null">response_time = #{responseTime},</if>
            <if test="isError != null">is_error = #{isError},</if>
        </trim>
        where message_id = #{messageId}
    </update>

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

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

