From 50866158c6adaf7fb1aea2b1aa74fe701a41ac58 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 23 Mar 2025 22:16:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=B7=E6=A0=BC=E5=80=8D?= =?UTF-8?q?=E7=8E=87=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8map=E5=AD=98=E5=82=A8=E6=A8=A1=E5=9E=8B=E5=8F=8A?= =?UTF-8?q?=E5=85=B6=E5=AF=B9=E5=BA=94=E7=9A=84=E6=9C=80=E9=AB=98=E5=80=8D?= =?UTF-8?q?=E7=8E=87=EF=BC=8C=E7=A1=AE=E4=BF=9D=E4=B8=8D=E5=8C=BA=E5=88=86?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E7=9A=84=E6=AF=94=E8=BE=83=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD=E5=92=8C=E5=87=86=E7=A1=AE?= =?UTF-8?q?=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/handlers/rates/price_rates.go | 35 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/backend/handlers/rates/price_rates.go b/backend/handlers/rates/price_rates.go index 71e8bba..8d4d992 100644 --- a/backend/handlers/rates/price_rates.go +++ b/backend/handlers/rates/price_rates.go @@ -2,6 +2,7 @@ package rates import ( "net/http" + "strings" "time" "github.com/gin-gonic/gin" @@ -40,8 +41,8 @@ func GetPriceRates(c *gin.Context) { return } - // 预分配rates切片,减少内存分配 - rates := make([]PriceRate, 0, len(prices)) + // 创建map用于存储模型及其对应的最高倍率 + modelRateMap := make(map[string]PriceRate) // 计算倍率 for _, price := range prices { @@ -58,13 +59,39 @@ func GetPriceRates(c *gin.Context) { outputRate = price.OutputPrice / 14 } - rates = append(rates, PriceRate{ + // 创建当前价格的PriceRate + currentRate := PriceRate{ Model: price.Model, Type: price.BillingType, ChannelType: price.ChannelType, Input: inputRate, Output: outputRate, - }) + } + + // 转换为小写以实现不区分大小写比较 + modelLower := strings.ToLower(price.Model) + + // 检查是否已存在相同模型名称(不区分大小写) + if existingRate, exists := modelRateMap[modelLower]; exists { + // 比较倍率,保留较高的那个 + // 这里我们以输入和输出倍率的总和作为比较标准 + existingTotal := existingRate.Input + existingRate.Output + currentTotal := inputRate + outputRate + + if currentTotal > existingTotal { + // 当前倍率更高,替换已存在的 + modelRateMap[modelLower] = currentRate + } + } else { + // 不存在相同模型名称,直接添加 + modelRateMap[modelLower] = currentRate + } + } + + // 从map中提取结果到slice + rates := make([]PriceRate, 0, len(modelRateMap)) + for _, rate := range modelRateMap { + rates = append(rates, rate) } // 存入缓存,有效期24小时