<?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.AiModelConfigMapper">
    
    <resultMap type="AiModelConfig" id="AiModelConfigResult">
        <result property="configId"          column="config_id"           />
        <result property="configName"        column="config_name"         />
        <result property="userId"            column="user_id"             />
        <result property="modelId"           column="model_id"            />
        <result property="modelName"         column="model_name"          />
        <result property="modelCode"         column="model_code"          />
        <result property="apiKey"            column="api_key"             />
        <result property="apiSecret"         column="api_secret"          />
        <result property="apiUrl"            column="api_url"             />
        <result property="proxyUrl"          column="proxy_url"           />
        <result property="temperature"       column="temperature"         />
        <result property="topP"              column="top_p"               />
        <result property="topK"              column="top_k"               />
        <result property="maxTokens"         column="max_tokens"          />
        <result property="frequencyPenalty"  column="frequency_penalty"   />
        <result property="presencePenalty"   column="presence_penalty"    />
        <result property="systemPrompt"      column="system_prompt"       />
        <result property="enableStream"      column="enable_stream"       />
        <result property="enableHistory"     column="enable_history"      />
        <result property="historyLength"     column="history_length"      />
        <result property="enableWebSearch"   column="enable_web_search"   />
        <result property="thinkingType"     column="thinking_type"       />
        <result property="isDefault"         column="is_default"          />
        <result property="isPublic"          column="is_public"           />
        <result property="status"            column="status"              />
        <result property="createBy"          column="create_by"           />
        <result property="createTime"        column="create_time"         />
        <result property="updateBy"          column="update_by"           />
        <result property="updateTime"        column="update_time"         />
        <result property="remark"            column="remark"              />
    </resultMap>

    <sql id="selectAiModelConfigVo">
        select c.config_id, c.config_name, c.user_id, c.model_id, c.api_key, c.api_secret, c.api_url, c.proxy_url,
               c.temperature, c.top_p, c.top_k, c.max_tokens, c.frequency_penalty, c.presence_penalty,
               c.system_prompt, c.enable_stream, c.enable_history, c.history_length, c.enable_web_search, c.thinking_type,
               c.is_default, c.is_public, c.status, c.create_by, c.create_time, c.update_by, c.update_time, c.remark,
               m.model_name, m.model_code
        from ai_model_config c
        left join ai_model m on c.model_id = m.model_id
    </sql>

    <select id="selectAiModelConfigList" parameterType="AiModelConfig" resultMap="AiModelConfigResult">
        <include refid="selectAiModelConfigVo"/>
        <where>  
            <if test="configName != null and configName != ''"> and c.config_name like concat('%', #{configName}, '%')</if>
            <if test="modelId != null"> and c.model_id = #{modelId}</if>
            <if test="userId != null"> and c.user_id = #{userId}</if>
            <if test="isDefault != null and isDefault != ''"> and c.is_default = #{isDefault}</if>
            <if test="status != null and status != ''"> and c.status = #{status}</if>
        </where>
        order by c.is_default desc, c.create_time desc
    </select>
    
    <select id="selectAiModelConfigByConfigId" parameterType="Long" resultMap="AiModelConfigResult">
        <include refid="selectAiModelConfigVo"/>
        where c.config_id = #{configId}
    </select>
        
    <insert id="insertAiModelConfig" parameterType="AiModelConfig" useGeneratedKeys="true" keyProperty="configId">
        insert into ai_model_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="configName != null and configName != ''">config_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="modelId != null">model_id,</if>
            <if test="apiKey != null">api_key,</if>
            <if test="apiSecret != null">api_secret,</if>
            <if test="apiUrl != null">api_url,</if>
            <if test="proxyUrl != null">proxy_url,</if>
            <if test="temperature != null">temperature,</if>
            <if test="topP != null">top_p,</if>
            <if test="topK != null">top_k,</if>
            <if test="maxTokens != null">max_tokens,</if>
            <if test="frequencyPenalty != null">frequency_penalty,</if>
            <if test="presencePenalty != null">presence_penalty,</if>
            <if test="systemPrompt != null">system_prompt,</if>
            <if test="enableStream != null">enable_stream,</if>
            <if test="enableHistory != null">enable_history,</if>
            <if test="historyLength != null">history_length,</if>
            <if test="enableWebSearch != null">enable_web_search,</if>
            <if test="thinkingType != null">thinking_type,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="isPublic != null">is_public,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="configName != null and configName != ''">#{configName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="modelId != null">#{modelId},</if>
            <if test="apiKey != null">#{apiKey},</if>
            <if test="apiSecret != null">#{apiSecret},</if>
            <if test="apiUrl != null">#{apiUrl},</if>
            <if test="proxyUrl != null">#{proxyUrl},</if>
            <if test="temperature != null">#{temperature},</if>
            <if test="topP != null">#{topP},</if>
            <if test="topK != null">#{topK},</if>
            <if test="maxTokens != null">#{maxTokens},</if>
            <if test="frequencyPenalty != null">#{frequencyPenalty},</if>
            <if test="presencePenalty != null">#{presencePenalty},</if>
            <if test="systemPrompt != null">#{systemPrompt},</if>
            <if test="enableStream != null">#{enableStream},</if>
            <if test="enableHistory != null">#{enableHistory},</if>
            <if test="historyLength != null">#{historyLength},</if>
            <if test="enableWebSearch != null">#{enableWebSearch},</if>
            <if test="thinkingType != null">#{thinkingType},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="isPublic != null">#{isPublic},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAiModelConfig" parameterType="AiModelConfig">
        update ai_model_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="configName != null and configName != ''">config_name = #{configName},</if>
            <if test="modelId != null">model_id = #{modelId},</if>
            <if test="apiKey != null">api_key = #{apiKey},</if>
            <if test="apiSecret != null">api_secret = #{apiSecret},</if>
            <if test="apiUrl != null">api_url = #{apiUrl},</if>
            <if test="proxyUrl != null">proxy_url = #{proxyUrl},</if>
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="topP != null">top_p = #{topP},</if>
            <if test="topK != null">top_k = #{topK},</if>
            <if test="maxTokens != null">max_tokens = #{maxTokens},</if>
            <if test="frequencyPenalty != null">frequency_penalty = #{frequencyPenalty},</if>
            <if test="presencePenalty != null">presence_penalty = #{presencePenalty},</if>
            <if test="systemPrompt != null">system_prompt = #{systemPrompt},</if>
            <if test="enableStream != null">enable_stream = #{enableStream},</if>
            <if test="enableHistory != null">enable_history = #{enableHistory},</if>
            <if test="historyLength != null">history_length = #{historyLength},</if>
            <if test="enableWebSearch != null">enable_web_search = #{enableWebSearch},</if>
            <if test="thinkingType != null">thinking_type = #{thinkingType},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="isPublic != null">is_public = #{isPublic},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where config_id = #{configId}
    </update>

    <delete id="deleteAiModelConfigByConfigId" parameterType="Long">
        delete from ai_model_config where config_id = #{configId}
    </delete>

    <delete id="deleteAiModelConfigByConfigIds" parameterType="String">
        delete from ai_model_config where config_id in 
        <foreach item="configId" collection="array" open="(" separator="," close=")">
            #{configId}
        </foreach>
    </delete>
</mapper>

