<?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.ruoyi.generator.mapper.front.AiQuotaBucketMapper">

    <resultMap type="AiQuotaBucket" id="AiQuotaBucketResult">
        <id property="bucketId" column="bucket_id" />
        <result property="userId" column="user_id" />
        <result property="periodType" column="period_type" />
        <result property="periodKey" column="period_key" />
        <result property="taskLimit" column="task_limit" />
        <result property="taskUsed" column="task_used" />
        <result property="tokenLimit" column="token_limit" />
        <result property="tokenUsed" column="token_used" />
        <result property="costLimitCents" column="cost_limit_cents" />
        <result property="costUsedCents" column="cost_used_cents" />
        <result property="runningLimit" column="running_limit" />
        <result property="runningCount" column="running_count" />
        <result property="createTime" column="create_time" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <select id="selectQuotaBucket" resultMap="AiQuotaBucketResult">
        select bucket_id, user_id, period_type, period_key, task_limit, task_used, token_limit, token_used,
               cost_limit_cents, cost_used_cents, running_limit, running_count, create_time, update_time
        from front_ai_quota_bucket
        where user_id = #{userId} and period_type = #{periodType} and period_key = #{periodKey}
    </select>

    <insert id="insertQuotaBucket" parameterType="AiQuotaBucket" useGeneratedKeys="true" keyProperty="bucketId">
        insert into front_ai_quota_bucket
        (user_id, period_type, period_key, task_limit, task_used, token_limit, token_used,
         cost_limit_cents, cost_used_cents, running_limit, running_count, create_time, update_time)
        values
        (#{userId}, #{periodType}, #{periodKey}, #{taskLimit}, #{taskUsed}, #{tokenLimit}, #{tokenUsed},
         #{costLimitCents}, #{costUsedCents}, #{runningLimit}, #{runningCount}, sysdate(), sysdate())
    </insert>

    <update id="updateQuotaBucket" parameterType="AiQuotaBucket">
        update front_ai_quota_bucket
        set task_limit = #{taskLimit},
            task_used = #{taskUsed},
            token_limit = #{tokenLimit},
            token_used = #{tokenUsed},
            cost_limit_cents = #{costLimitCents},
            cost_used_cents = #{costUsedCents},
            running_limit = #{runningLimit},
            running_count = #{runningCount},
            update_time = sysdate()
        where bucket_id = #{bucketId}
    </update>
</mapper>
