<?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.AiServiceMapper">
    
    <resultMap type="AiService" id="AiServiceResult">
        <id     property="serviceId"      column="service_id"      />
        <result property="serviceName"    column="service_name"    />
        <result property="serviceCode"    column="service_code"    />
        <result property="provider"       column="provider"        />
        <result property="providerName"   column="provider_name"   />
        <result property="modelCode"      column="model_code"      />
        <result property="apiUrl"         column="api_url"         />
        <result property="apiKey"         column="api_key"         />
        <result property="apiSecret"      column="api_secret"      />
        <result property="temperature"    column="temperature"     />
        <result property="topP"           column="top_p"           />
        <result property="maxTokens"      column="max_tokens"      />
        <result property="systemPrompt"   column="system_prompt"   />
        <result property="contextWindow"  column="context_window"  />
        <result property="supportsStream"   column="supports_stream"   />
        <result property="supportsVision"   column="supports_vision"   />
        <result property="supportsTools"    column="supports_tools"    />
        <result property="supportsThinking" column="supports_thinking" />
        <result property="supportsSearch"   column="supports_search"   />
        <result property="supportsFiles"    column="supports_files"    />
        <result property="extraParams"      column="extra_params"      />
        <result property="enabled"        column="enabled"         />
        <result property="isDefault"      column="is_default"      />
        <result property="sortOrder"      column="sort_order"      />
        <result property="rateLimit"      column="rate_limit"      />
        <result property="dailyLimit"     column="daily_limit"     />
        <result property="remark"         column="remark"          />
        <result property="createBy"       column="create_by"       />
        <result property="createTime"     column="create_time"     />
        <result property="updateBy"       column="update_by"       />
        <result property="updateTime"     column="update_time"     />
    </resultMap>

    <sql id="selectAiServiceVo">
        select service_id, service_name, service_code, provider, provider_name, model_code, 
               api_url, api_key, api_secret, temperature, top_p, max_tokens, system_prompt,
               context_window, supports_stream, supports_vision, supports_tools,
               supports_thinking, supports_search, supports_files, extra_params,
               enabled, is_default, sort_order, rate_limit, daily_limit, remark,
               create_by, create_time, update_by, update_time
        from ai_service
    </sql>

    <select id="selectAiServiceByServiceId" parameterType="Long" resultMap="AiServiceResult">
        <include refid="selectAiServiceVo"/>
        where service_id = #{serviceId}
    </select>

    <select id="selectAiServiceByServiceCode" parameterType="String" resultMap="AiServiceResult">
        <include refid="selectAiServiceVo"/>
        where service_code = #{serviceCode}
    </select>

    <select id="selectAiServiceList" parameterType="AiService" resultMap="AiServiceResult">
        <include refid="selectAiServiceVo"/>
        <where>
            <if test="serviceName != null and serviceName != ''">
                AND service_name like concat('%', #{serviceName}, '%')
            </if>
            <if test="serviceCode != null and serviceCode != ''">
                AND service_code = #{serviceCode}
            </if>
            <if test="provider != null and provider != ''">
                AND provider = #{provider}
            </if>
            <if test="enabled != null and enabled != ''">
                AND enabled = #{enabled}
            </if>
        </where>
        order by sort_order, create_time desc
    </select>

    <select id="selectEnabledServiceList" resultMap="AiServiceResult">
        <include refid="selectAiServiceVo"/>
        where enabled = '1'
        order by sort_order, create_time desc
    </select>

    <select id="selectDefaultService" resultMap="AiServiceResult">
        <include refid="selectAiServiceVo"/>
        where is_default = '1' and enabled = '1'
        limit 1
    </select>

    <insert id="insertAiService" parameterType="AiService" useGeneratedKeys="true" keyProperty="serviceId">
        insert into ai_service (
            service_name, service_code, provider, provider_name, model_code,
            api_url, api_key, api_secret, temperature, top_p, max_tokens, system_prompt,
            context_window, supports_stream, supports_vision, supports_tools,
            supports_thinking, supports_search, supports_files, extra_params,
            enabled, is_default, sort_order, rate_limit, daily_limit, remark,
            create_by, create_time
        ) values (
            #{serviceName}, #{serviceCode}, #{provider}, #{providerName}, #{modelCode},
            #{apiUrl}, #{apiKey}, #{apiSecret}, #{temperature}, #{topP}, #{maxTokens}, #{systemPrompt},
            #{contextWindow}, #{supportsStream}, #{supportsVision}, #{supportsTools},
            #{supportsThinking}, #{supportsSearch}, #{supportsFiles}, #{extraParams},
            #{enabled}, #{isDefault}, #{sortOrder}, #{rateLimit}, #{dailyLimit}, #{remark},
            #{createBy}, sysdate()
        )
    </insert>

    <update id="updateAiService" parameterType="AiService">
        update ai_service
        <set>
            <if test="serviceName != null and serviceName != ''">service_name = #{serviceName},</if>
            <if test="serviceCode != null and serviceCode != ''">service_code = #{serviceCode},</if>
            <if test="provider != null">provider = #{provider},</if>
            <if test="providerName != null">provider_name = #{providerName},</if>
            <if test="modelCode != null">model_code = #{modelCode},</if>
            <if test="apiUrl != null">api_url = #{apiUrl},</if>
            <if test="apiKey != null">api_key = #{apiKey},</if>
            <if test="apiSecret != null">api_secret = #{apiSecret},</if>
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="topP != null">top_p = #{topP},</if>
            <if test="maxTokens != null">max_tokens = #{maxTokens},</if>
            <if test="systemPrompt != null">system_prompt = #{systemPrompt},</if>
            <if test="contextWindow != null">context_window = #{contextWindow},</if>
            <if test="supportsStream != null">supports_stream = #{supportsStream},</if>
            <if test="supportsVision != null">supports_vision = #{supportsVision},</if>
            <if test="supportsTools != null">supports_tools = #{supportsTools},</if>
            <if test="supportsThinking != null">supports_thinking = #{supportsThinking},</if>
            <if test="supportsSearch != null">supports_search = #{supportsSearch},</if>
            <if test="supportsFiles != null">supports_files = #{supportsFiles},</if>
            <if test="extraParams != null">extra_params = #{extraParams},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="rateLimit != null">rate_limit = #{rateLimit},</if>
            <if test="dailyLimit != null">daily_limit = #{dailyLimit},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where service_id = #{serviceId}
    </update>

    <delete id="deleteAiServiceByServiceId" parameterType="Long">
        delete from ai_service where service_id = #{serviceId}
    </delete>

    <delete id="deleteAiServiceByServiceIds" parameterType="String">
        delete from ai_service where service_id in 
        <foreach item="serviceId" collection="array" open="(" separator="," close=")">
            #{serviceId}
        </foreach>
    </delete>

    <update id="resetDefaultService">
        update ai_service set is_default = '0' where is_default = '1'
    </update>
</mapper>
