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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.ai.TbAiMessage">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="conversation_id" property="conversationID" jdbcType="VARCHAR"/>
        <result column="user_id" property="userID" jdbcType="VARCHAR"/>
        <result column="role" property="role" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="LONGVARCHAR"/>
        <result column="file_ids" property="fileIDs" jdbcType="VARCHAR"/>
        <result column="knowledge_ids" property="knowledgeIDs" jdbcType="VARCHAR"/>
        <result column="token_count" property="tokenCount" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, conversation_id, user_id, role, content, file_ids, knowledge_ids, 
        token_count, create_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="conversationID != null and conversationID != ''">
                AND conversation_id = #{conversationID}
            </if>
            <if test="userID != null and userID != ''">
                AND user_id = #{userID}
            </if>
            <if test="role != null and role != ''">
                AND role = #{role}
            </if>
        </where>
    </sql>

    <!-- selectAiMessages -->
    <select id="selectAiMessages" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_ai_message
        <include refid="Where_Clause"/>
        ORDER BY create_time ASC
    </select>

</mapper>
