<?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.AiConversationMapper">
    
    <resultMap type="AiConversation" id="AiConversationResult">
        <result property="conversationId"      column="conversation_id"       />
        <result property="userId"              column="user_id"               />
        <result property="serviceId"           column="service_id"            />
        <result property="serviceName"         column="service_name"          />
        <result property="title"               column="title"                 />
        <result property="systemPrompt"        column="system_prompt"         />
        <result property="messageCount"        column="message_count"         />
        <result property="totalTokens"         column="total_tokens"          />
        <result property="lastMessageTime"     column="last_message_time"     />
        <result property="status"              column="status"                />
        <result property="isPinned"            column="is_pinned"             />
        <result property="createTime"          column="create_time"           />
        <result property="updateTime"          column="update_time"           />
    </resultMap>

    <sql id="selectAiConversationVo">
        select c.conversation_id, c.user_id, c.service_id, c.title, c.system_prompt,
               c.message_count, c.total_tokens, c.last_message_time, c.status, c.is_pinned, 
               c.create_time, c.update_time, s.service_name
        from ai_conversation c
        left join ai_service s on c.service_id = s.service_id
    </sql>

    <select id="selectAiConversationList" parameterType="AiConversation" resultMap="AiConversationResult">
        <include refid="selectAiConversationVo"/>
        <where>  
            <if test="userId != null"> and c.user_id = #{userId}</if>
            <if test="serviceId != null"> and c.service_id = #{serviceId}</if>
            <if test="status != null and status != ''"> and c.status = #{status}</if>
        </where>
        order by c.is_pinned desc, c.last_message_time desc, c.create_time desc
    </select>
    
    <select id="selectAiConversationByConversationId" parameterType="Long" resultMap="AiConversationResult">
        <include refid="selectAiConversationVo"/>
        where c.conversation_id = #{conversationId}
    </select>
        
    <insert id="insertAiConversation" parameterType="AiConversation" useGeneratedKeys="true" keyProperty="conversationId">
        insert into ai_conversation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="serviceId != null">service_id,</if>
            <if test="title != null">title,</if>
            <if test="systemPrompt != null">system_prompt,</if>
            <if test="messageCount != null">message_count,</if>
            <if test="totalTokens != null">total_tokens,</if>
            <if test="lastMessageTime != null">last_message_time,</if>
            <if test="status != null">status,</if>
            <if test="isPinned != null">is_pinned,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="serviceId != null">#{serviceId},</if>
            <if test="title != null">#{title},</if>
            <if test="systemPrompt != null">#{systemPrompt},</if>
            <if test="messageCount != null">#{messageCount},</if>
            <if test="totalTokens != null">#{totalTokens},</if>
            <if test="lastMessageTime != null">#{lastMessageTime},</if>
            <if test="status != null">#{status},</if>
            <if test="isPinned != null">#{isPinned},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateAiConversation" parameterType="AiConversation">
        update ai_conversation
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="messageCount != null">message_count = #{messageCount},</if>
            <if test="totalTokens != null">total_tokens = #{totalTokens},</if>
            <if test="lastMessageTime != null">last_message_time = #{lastMessageTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isPinned != null">is_pinned = #{isPinned},</if>
            update_time = NOW()
        </trim>
        where conversation_id = #{conversationId}
    </update>

    <delete id="deleteAiConversationByConversationId" parameterType="Long">
        delete from ai_conversation where conversation_id = #{conversationId}
    </delete>

    <delete id="deleteAiConversationByConversationIds" parameterType="String">
        delete from ai_conversation where conversation_id in 
        <foreach item="conversationId" collection="array" open="(" separator="," close=")">
            #{conversationId}
        </foreach>
    </delete>
</mapper>

