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

    <resultMap id="BaseResultMap" type="org.xyzh.api.ai.dto.TbAgent">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="agent_id" property="agentId" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="link" property="link" jdbcType="VARCHAR"/>
        <result column="api_key" property="apiKey" jdbcType="VARCHAR"/>
        <result column="is_outer"  property="isOuter" jdbcType="BOOLEAN"/>
        <result column="introduce" property="introduce" jdbcType="VARCHAR"/>
        <result column="prompt_cards" property="promptCards" jdbcType="OTHER" typeHandler="org.xyzh.ai.handler.PromptCardsTypeHandler"/>
        <result column="category" property="category" jdbcType="VARCHAR"/>
        <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">
        optsn, agent_id, name, description, link, api_key, is_outer, introduce, prompt_cards,
        category, creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertAgent" parameterType="org.xyzh.api.ai.dto.TbAgent">
        INSERT INTO ai.tb_agent (
            optsn, agent_id, name, api_key, introduce, category
            <if test="isOuter !=null">, is_outer</if>
            <if test="description != null">, description</if>
            <if test="link != null">, link</if>
            <if test="promptCards != null">, prompt_cards</if>
            <if test="creator != null">, creator</if>
        ) VALUES (
            #{optsn}, #{agentId}, #{name}, #{apiKey}, #{introduce}, #{category}
            <if test="isOuter !=null">, #{isOuter}</if>
            <if test="description != null">, #{description}</if>
            <if test="link != null">, #{link}</if>
            <if test="promptCards != null">, #{promptCards, typeHandler=org.xyzh.ai.handler.PromptCardsTypeHandler}</if>
            <if test="creator != null">, #{creator}</if>
        )
    </insert>

    <update id="updateAgent" parameterType="org.xyzh.api.ai.dto.TbAgent">
        UPDATE ai.tb_agent
        <set>
            <if test="name != null">name = #{name},</if>
            <if test="description != null">description = #{description},</if>
            <if test="link != null">link = #{link},</if>
            <if test="apiKey != null">api_key = #{apiKey},</if>
            <if test="isOuter != null">is_outer = #{isOuter},</if>
            <if test="introduce != null">introduce = #{introduce},</if>
            <if test="promptCards != null">prompt_cards = #{promptCards, typeHandler=org.xyzh.ai.handler.PromptCardsTypeHandler},</if>
            <if test="category != null">category = #{category},</if>
            <if test="updater != null">updater = #{updater},</if>
            update_time = now()
        </set>
        WHERE agent_id = #{agentId} AND deleted = false
    </update>

    <update id="deleteAgent" parameterType="org.xyzh.api.ai.dto.TbAgent">
        UPDATE ai.tb_agent
        SET deleted = true,
            delete_time = now(),
            updater = #{updater}
        WHERE agent_id = #{agentId} AND deleted = false
    </update>

    <select id="selectAgentById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_agent
        WHERE agent_id = #{agentId} AND deleted = false
    </select>

    <select id="selectAgentByApiKey" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_agent
        WHERE api_key = #{apiKey} AND deleted = false
    </select>

    <select id="selectAgentList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_agent
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectAgentPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_agent
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </if>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countAgents" resultType="long">
        SELECT COUNT(*)
        FROM ai.tb_agent
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </if>
    </select>

    <select id="countByName" resultType="int">
        SELECT COUNT(*)
        FROM ai.tb_agent
        WHERE deleted = false AND name = #{name}
        <if test="excludeId != null and excludeId != ''">
            AND agent_id != #{excludeId}
        </if>
    </select>
</mapper>
