<?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.AiModelMapper">
    
    <resultMap type="AiModel" id="AiModelResult">
        <result property="modelId"           column="model_id"            />
        <result property="providerId"        column="provider_id"         />
        <result property="providerName"      column="provider_name"       />
        <result property="modelCode"         column="model_code"          />
        <result property="modelName"         column="model_name"          />
        <result property="modelType"         column="model_type"          />
        <result property="modelVersion"      column="model_version"       />
        <result property="maxTokens"         column="max_tokens"          />
        <result property="contextWindow"     column="context_window"      />
        <result property="supportFunctions"  column="support_functions"   />
        <result property="supportVision"     column="support_vision"      />
        <result property="supportStream"     column="support_stream"      />
        <result property="supportWebSearch"  column="support_web_search"  />
        <result property="defaultApiUrl"     column="default_api_url"     />
        <result property="apiVersion"        column="api_version"         />
        <result property="inputPrice"        column="input_price"         />
        <result property="outputPrice"       column="output_price"        />
        <result property="currency"          column="currency"            />
        <result property="rateLimitRpm"      column="rate_limit_rpm"      />
        <result property="rateLimitRpd"      column="rate_limit_rpd"      />
        <result property="rateLimitTpm"      column="rate_limit_tpm"      />
        <result property="isRecommended"     column="is_recommended"      />
        <result property="status"            column="status"              />
        <result property="sortOrder"         column="sort_order"          />
        <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="selectAiModelVo">
        select m.model_id, m.provider_id, m.model_code, m.model_name, m.model_type, m.model_version,
               m.max_tokens, m.context_window, m.support_functions, m.support_vision, m.support_stream, m.support_web_search,
               m.default_api_url, m.api_version, m.input_price, m.output_price, m.currency,
               m.rate_limit_rpm, m.rate_limit_rpd, m.rate_limit_tpm, m.is_recommended,
               m.status, m.sort_order, m.create_by, m.create_time, m.update_by, m.update_time, m.remark,
               p.provider_name
        from ai_model m
        left join ai_provider p on m.provider_id = p.provider_id
    </sql>

    <select id="selectAiModelList" parameterType="AiModel" resultMap="AiModelResult">
        <include refid="selectAiModelVo"/>
        <where>  
            <if test="providerId != null"> and m.provider_id = #{providerId}</if>
            <if test="modelCode != null and modelCode != ''"> and m.model_code like concat('%', #{modelCode}, '%')</if>
            <if test="modelName != null and modelName != ''"> and m.model_name like concat('%', #{modelName}, '%')</if>
            <if test="modelType != null and modelType != ''"> and m.model_type = #{modelType}</if>
            <if test="status != null and status != ''"> and m.status = #{status}</if>
        </where>
        order by m.sort_order asc, m.create_time desc
    </select>
    
    <select id="selectAiModelByModelId" parameterType="Long" resultMap="AiModelResult">
        <include refid="selectAiModelVo"/>
        where m.model_id = #{modelId}
    </select>
        
    <insert id="insertAiModel" parameterType="AiModel" useGeneratedKeys="true" keyProperty="modelId">
        insert into ai_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="providerId != null">provider_id,</if>
            <if test="modelCode != null and modelCode != ''">model_code,</if>
            <if test="modelName != null and modelName != ''">model_name,</if>
            <if test="modelType != null">model_type,</if>
            <if test="modelVersion != null">model_version,</if>
            <if test="maxTokens != null">max_tokens,</if>
            <if test="contextWindow != null">context_window,</if>
            <if test="supportFunctions != null">support_functions,</if>
            <if test="supportVision != null">support_vision,</if>
            <if test="supportStream != null">support_stream,</if>
            <if test="supportWebSearch != null">support_web_search,</if>
            <if test="defaultApiUrl != null">default_api_url,</if>
            <if test="apiVersion != null">api_version,</if>
            <if test="inputPrice != null">input_price,</if>
            <if test="outputPrice != null">output_price,</if>
            <if test="currency != null">currency,</if>
            <if test="rateLimitRpm != null">rate_limit_rpm,</if>
            <if test="rateLimitRpd != null">rate_limit_rpd,</if>
            <if test="rateLimitTpm != null">rate_limit_tpm,</if>
            <if test="isRecommended != null">is_recommended,</if>
            <if test="status != null">status,</if>
            <if test="sortOrder != null">sort_order,</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="providerId != null">#{providerId},</if>
            <if test="modelCode != null and modelCode != ''">#{modelCode},</if>
            <if test="modelName != null and modelName != ''">#{modelName},</if>
            <if test="modelType != null">#{modelType},</if>
            <if test="modelVersion != null">#{modelVersion},</if>
            <if test="maxTokens != null">#{maxTokens},</if>
            <if test="contextWindow != null">#{contextWindow},</if>
            <if test="supportFunctions != null">#{supportFunctions},</if>
            <if test="supportVision != null">#{supportVision},</if>
            <if test="supportStream != null">#{supportStream},</if>
            <if test="supportWebSearch != null">#{supportWebSearch},</if>
            <if test="defaultApiUrl != null">#{defaultApiUrl},</if>
            <if test="apiVersion != null">#{apiVersion},</if>
            <if test="inputPrice != null">#{inputPrice},</if>
            <if test="outputPrice != null">#{outputPrice},</if>
            <if test="currency != null">#{currency},</if>
            <if test="rateLimitRpm != null">#{rateLimitRpm},</if>
            <if test="rateLimitRpd != null">#{rateLimitRpd},</if>
            <if test="rateLimitTpm != null">#{rateLimitTpm},</if>
            <if test="isRecommended != null">#{isRecommended},</if>
            <if test="status != null">#{status},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAiModel" parameterType="AiModel">
        update ai_model
        <trim prefix="SET" suffixOverrides=",">
            <if test="providerId != null">provider_id = #{providerId},</if>
            <if test="modelCode != null and modelCode != ''">model_code = #{modelCode},</if>
            <if test="modelName != null and modelName != ''">model_name = #{modelName},</if>
            <if test="modelType != null">model_type = #{modelType},</if>
            <if test="modelVersion != null">model_version = #{modelVersion},</if>
            <if test="maxTokens != null">max_tokens = #{maxTokens},</if>
            <if test="contextWindow != null">context_window = #{contextWindow},</if>
            <if test="supportFunctions != null">support_functions = #{supportFunctions},</if>
            <if test="supportVision != null">support_vision = #{supportVision},</if>
            <if test="supportStream != null">support_stream = #{supportStream},</if>
            <if test="supportWebSearch != null">support_web_search = #{supportWebSearch},</if>
            <if test="defaultApiUrl != null">default_api_url = #{defaultApiUrl},</if>
            <if test="apiVersion != null">api_version = #{apiVersion},</if>
            <if test="inputPrice != null">input_price = #{inputPrice},</if>
            <if test="outputPrice != null">output_price = #{outputPrice},</if>
            <if test="currency != null">currency = #{currency},</if>
            <if test="rateLimitRpm != null">rate_limit_rpm = #{rateLimitRpm},</if>
            <if test="rateLimitRpd != null">rate_limit_rpd = #{rateLimitRpd},</if>
            <if test="rateLimitTpm != null">rate_limit_tpm = #{rateLimitTpm},</if>
            <if test="isRecommended != null">is_recommended = #{isRecommended},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</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 model_id = #{modelId}
    </update>

    <delete id="deleteAiModelByModelId" parameterType="Long">
        delete from ai_model where model_id = #{modelId}
    </delete>

    <delete id="deleteAiModelByModelIds" parameterType="String">
        delete from ai_model where model_id in 
        <foreach item="modelId" collection="array" open="(" separator="," close=")">
            #{modelId}
        </foreach>
    </delete>
</mapper>

