You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

196 line
6.2 KiB

  1. package model
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "gorm.io/gorm"
  8. "gorm.io/gorm/clause"
  9. )
  10. // 渠道定价缓存
  11. var (
  12. channelPricingCache = make(map[string]*ChannelPricing) // key: "modelName:channelId"
  13. channelPricingCacheLock sync.RWMutex
  14. channelPricingCacheTime time.Time
  15. channelPricingCacheTTL = time.Minute * 5 // 缓存5分钟
  16. )
  17. // QuotaType 计费类型
  18. const (
  19. QuotaTypeByTokens = 0 // 按量计费
  20. QuotaTypeByCall = 1 // 按次计费
  21. )
  22. // ChannelPricing 渠道定价表
  23. // 支持同一模型在不同渠道设置不同价格
  24. type ChannelPricing struct {
  25. Id int `json:"id" gorm:"primaryKey"`
  26. ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:idx_model_channel,priority:1"`
  27. ChannelId int `json:"channel_id" gorm:"not null;uniqueIndex:idx_model_channel,priority:2;index"`
  28. QuotaType int `json:"quota_type" gorm:"default:0"` // 0=按量, 1=按次
  29. ModelRatio float64 `json:"model_ratio" gorm:"default:0"`
  30. CompletionRatio float64 `json:"completion_ratio" gorm:"default:0"`
  31. ModelPrice float64 `json:"model_price" gorm:"default:0"`
  32. TagIds string `json:"tag_ids" gorm:"type:varchar(255)"` // 逗号分隔的标签ID
  33. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  34. UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
  35. DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
  36. }
  37. func (cp *ChannelPricing) Insert() error {
  38. now := common.GetTimestamp()
  39. cp.CreatedTime = now
  40. cp.UpdatedTime = now
  41. return DB.Create(cp).Error
  42. }
  43. func (cp *ChannelPricing) Update() error {
  44. cp.UpdatedTime = common.GetTimestamp()
  45. return DB.Model(&ChannelPricing{}).Where("id = ?", cp.Id).Updates(map[string]interface{}{
  46. "quota_type": cp.QuotaType,
  47. "model_ratio": cp.ModelRatio,
  48. "completion_ratio": cp.CompletionRatio,
  49. "model_price": cp.ModelPrice,
  50. "tag_ids": cp.TagIds,
  51. "updated_time": cp.UpdatedTime,
  52. }).Error
  53. }
  54. func (cp *ChannelPricing) Delete() error {
  55. return DB.Delete(cp).Error
  56. }
  57. // GetChannelPricing 获取指定模型在指定渠道的定价
  58. func GetChannelPricing(modelName string, channelId int) (*ChannelPricing, error) {
  59. var cp ChannelPricing
  60. err := DB.Where("model_name = ? AND channel_id = ?", modelName, channelId).First(&cp).Error
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &cp, nil
  65. }
  66. // GetChannelPricingByModel 获取指定模型的所有渠道定价
  67. func GetChannelPricingByModel(modelName string) ([]*ChannelPricing, error) {
  68. var list []*ChannelPricing
  69. err := DB.Where("model_name = ?", modelName).Find(&list).Error
  70. return list, err
  71. }
  72. // GetAllChannelPricing 获取所有渠道定价(分页)
  73. func GetAllChannelPricing(offset int, limit int) ([]*ChannelPricing, int64, error) {
  74. var list []*ChannelPricing
  75. var total int64
  76. if err := DB.Model(&ChannelPricing{}).Count(&total).Error; err != nil {
  77. return nil, 0, err
  78. }
  79. err := DB.Order("id DESC").Offset(offset).Limit(limit).Find(&list).Error
  80. return list, total, err
  81. }
  82. // BatchUpsertChannelPricing 批量创建或更新渠道定价
  83. func BatchUpsertChannelPricing(pricings []*ChannelPricing) error {
  84. if len(pricings) == 0 {
  85. return nil
  86. }
  87. now := common.GetTimestamp()
  88. for _, cp := range pricings {
  89. cp.UpdatedTime = now
  90. // 仅在 CreatedTime 为空时设置(新记录)
  91. if cp.CreatedTime == 0 {
  92. cp.CreatedTime = now
  93. }
  94. }
  95. // 使用 GORM 的 OnConflict 实现 upsert
  96. // 唯一索引为 idx_model_channel (model_name, channel_id)
  97. return DB.Clauses(clause.OnConflict{
  98. Columns: []clause.Column{
  99. {Name: "model_name"},
  100. {Name: "channel_id"},
  101. },
  102. DoUpdates: clause.AssignmentColumns([]string{
  103. "quota_type",
  104. "model_ratio",
  105. "completion_ratio",
  106. "model_price",
  107. "tag_ids",
  108. "updated_time",
  109. }),
  110. }).Create(&pricings).Error
  111. }
  112. // getChannelPricingCacheKey 生成缓存键
  113. func getChannelPricingCacheKey(modelName string, channelId int) string {
  114. return fmt.Sprintf("%s:%d", modelName, channelId)
  115. }
  116. // GetEffectivePricing 获取有效定价(优先渠道定价,回退全局定价)
  117. // 返回: modelRatio, completionRatio, modelPrice, usePrice, found
  118. func GetEffectivePricing(modelName string, channelId int) (modelRatio, completionRatio, modelPrice float64, usePrice, found bool) {
  119. cacheKey := getChannelPricingCacheKey(modelName, channelId)
  120. // 首先检查缓存
  121. channelPricingCacheLock.RLock()
  122. // 检查缓存是否过期
  123. if time.Since(channelPricingCacheTime) < channelPricingCacheTTL {
  124. if cp, ok := channelPricingCache[cacheKey]; ok {
  125. channelPricingCacheLock.RUnlock()
  126. return cp.ModelRatio, cp.CompletionRatio, cp.ModelPrice, true, true
  127. }
  128. }
  129. channelPricingCacheLock.RUnlock()
  130. // 缓存未命中或已过期,查询数据库
  131. var cp ChannelPricing
  132. err := DB.Where("model_name = ? AND channel_id = ?", modelName, channelId).First(&cp).Error
  133. if err != nil {
  134. // 未找到渠道定价,返回 false 让调用者使用全局定价
  135. return 0, 0, 0, false, false
  136. }
  137. // 更新缓存
  138. channelPricingCacheLock.Lock()
  139. if channelPricingCacheTime.IsZero() || time.Since(channelPricingCacheTime) >= channelPricingCacheTTL {
  140. // 缓存过期,清空并更新时间
  141. channelPricingCache = make(map[string]*ChannelPricing)
  142. channelPricingCacheTime = time.Now()
  143. }
  144. channelPricingCache[cacheKey] = &cp
  145. channelPricingCacheLock.Unlock()
  146. return cp.ModelRatio, cp.CompletionRatio, cp.ModelPrice, true, true
  147. }
  148. // RefreshChannelPricingCache 刷新渠道定价缓存
  149. func RefreshChannelPricingCache() {
  150. channelPricingCacheLock.Lock()
  151. defer channelPricingCacheLock.Unlock()
  152. // 清空缓存
  153. channelPricingCache = make(map[string]*ChannelPricing)
  154. channelPricingCacheTime = time.Now()
  155. // 预加载所有渠道定价
  156. var pricings []*ChannelPricing
  157. if err := DB.Find(&pricings).Error; err != nil {
  158. return
  159. }
  160. for _, cp := range pricings {
  161. cacheKey := getChannelPricingCacheKey(cp.ModelName, cp.ChannelId)
  162. channelPricingCache[cacheKey] = cp
  163. }
  164. }
  165. // InvalidateChannelPricingCache 使渠道定价缓存失效
  166. func InvalidateChannelPricingCache() {
  167. channelPricingCacheLock.Lock()
  168. defer channelPricingCacheLock.Unlock()
  169. channelPricingCache = make(map[string]*ChannelPricing)
  170. channelPricingCacheTime = time.Time{} // 重置为零值
  171. }