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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.ai.TbAiAgentConfig">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="avatar" property="avatar" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="connect_internet" property="connectInternet" jdbcType="INTEGER"/>
        <result column="dify_app_id" property="difyAppId" jdbcType="VARCHAR"/>
        <result column="dify_api_key" property="difyApiKey" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" 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">
        id, name, avatar, description, connect_internet, dify_app_id, dify_api_key, status, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </sql>

    <!-- 插入智能体配置 -->
    <insert id="insertAgentConfig" parameterType="org.xyzh.common.dto.ai.TbAiAgentConfig">
        INSERT INTO tb_ai_agent_config (
            id, name, avatar, description, connect_internet, dify_app_id, dify_api_key, status,
            creator, updater, create_time, update_time, deleted
        ) VALUES (
            #{id}, #{name}, #{avatar}, #{description}, #{connectInternet}, #{difyAppId}, #{difyApiKey}, #{status},
            #{creator}, #{updater}, #{createTime}, #{updateTime}, #{deleted}
        )
    </insert>

    <!-- 更新智能体配置（动态更新） -->
    <update id="updateAgentConfig" parameterType="org.xyzh.common.dto.ai.TbAiAgentConfig">
        UPDATE tb_ai_agent_config
        <set>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="description != null">description = #{description},</if>
            <if test="connectInternet != null">connect_internet = #{connectInternet},</if>
            <if test="difyAppId != null">dify_app_id = #{difyAppId},</if>
            <if test="difyApiKey != null">dify_api_key = #{difyApiKey},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updater != null">updater = #{updater},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </set>
        WHERE id = #{ID} AND deleted = 0
    </update>

    <!-- 逻辑删除智能体配置 -->
    <update id="deleteAgentConfig" parameterType="org.xyzh.common.dto.ai.TbAiAgentConfig">
        UPDATE tb_ai_agent_config
        SET deleted = 1, 
            delete_time = NOW(),
            updater = #{updater}
        WHERE id = #{ID} AND deleted = 0
    </update>

    <!-- 根据ID查询智能体配置 -->
    <select id="selectAgentConfigById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tb_ai_agent_config
        WHERE id = #{agentId} AND deleted = 0
    </select>

    <!-- 查询所有智能体配置（支持过滤） -->
    <select id="selectAgentConfigs" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tb_ai_agent_config
        WHERE deleted = 0
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
        </if>
        ORDER BY create_time DESC
    </select>

    <!-- 分页查询智能体配置 -->
    <select id="selectAgentConfigsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tb_ai_agent_config
        WHERE deleted = 0
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
        </if>
        ORDER BY create_time DESC
        LIMIT #{pageParam.offset}, #{pageParam.pageSize}
    </select>

    <!-- 统计智能体配置总数 -->
    <select id="countAgentConfigs" resultType="java.lang.Long">
        SELECT COUNT(1)
        FROM tb_ai_agent_config
        WHERE deleted = 0
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
        </if>
    </select>

    <!-- 根据名称统计数量 -->
    <select id="countAgentConfigByName" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM tb_ai_agent_config
        WHERE name = #{name}
          AND deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- selectAiAgentConfigs (原有方法保留兼容性) -->
    <select id="selectAiAgentConfigs" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_ai_agent_config
        <include refid="Where_Clause"/>
        ORDER BY create_time DESC
    </select>

</mapper>
