Ver a proveniência

fix: implement proper upsert in BatchUpsertChannelPricing

The previous implementation only did INSERT without handling conflicts.
Now uses GORM's OnConflict clause to properly update existing records
when (model_name, channel_id) unique constraint is violated.

On conflict updates: quota_type, model_ratio, completion_ratio,
model_price, tag_ids, updated_time

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
feat/alipay-payment
fengsilin há 1 mês
ascendente
cometimento
961a845b28
1 ficheiros alterados com 21 adições e 3 eliminações
  1. +21
    -3
      model/channel_pricing.go

+ 21
- 3
model/channel_pricing.go Ver ficheiro

@@ -3,6 +3,7 @@ package model
import (
"github.com/QuantumNous/new-api/common"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

// QuotaType 计费类型
@@ -85,9 +86,26 @@ func BatchUpsertChannelPricing(pricings []*ChannelPricing) error {
}
now := common.GetTimestamp()
for _, cp := range pricings {
cp.CreatedTime = now
cp.UpdatedTime = now
// 仅在 CreatedTime 为空时设置(新记录)
if cp.CreatedTime == 0 {
cp.CreatedTime = now
}
}
// GORM 的 GORM:OnConflict 会自动处理唯一键冲突
return DB.Create(&pricings).Error
// 使用 GORM 的 OnConflict 实现 upsert
// 唯一索引为 idx_model_channel (model_name, channel_id)
return DB.Clauses(clause.OnConflict{
Columns: []clause.Column{
{Name: "model_name"},
{Name: "channel_id"},
},
DoUpdates: clause.AssignmentColumns([]string{
"quota_type",
"model_ratio",
"completion_ratio",
"model_price",
"tag_ids",
"updated_time",
}),
}).Create(&pricings).Error
}

Carregando…
Cancelar
Guardar