No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

94 líneas
3.1 KiB

  1. package model
  2. import (
  3. "github.com/QuantumNous/new-api/common"
  4. "gorm.io/gorm"
  5. )
  6. // QuotaType 计费类型
  7. const (
  8. QuotaTypeByTokens = 0 // 按量计费
  9. QuotaTypeByCall = 1 // 按次计费
  10. )
  11. // ChannelPricing 渠道定价表
  12. // 支持同一模型在不同渠道设置不同价格
  13. type ChannelPricing struct {
  14. Id int `json:"id" gorm:"primaryKey"`
  15. ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:idx_model_channel,priority:1"`
  16. ChannelId int `json:"channel_id" gorm:"not null;uniqueIndex:idx_model_channel,priority:2;index"`
  17. QuotaType int `json:"quota_type" gorm:"default:0"` // 0=按量, 1=按次
  18. ModelRatio float64 `json:"model_ratio" gorm:"default:0"`
  19. CompletionRatio float64 `json:"completion_ratio" gorm:"default:0"`
  20. ModelPrice float64 `json:"model_price" gorm:"default:0"`
  21. TagIds string `json:"tag_ids" gorm:"type:varchar(255)"` // 逗号分隔的标签ID
  22. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  23. UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
  24. DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
  25. }
  26. func (cp *ChannelPricing) Insert() error {
  27. now := common.GetTimestamp()
  28. cp.CreatedTime = now
  29. cp.UpdatedTime = now
  30. return DB.Create(cp).Error
  31. }
  32. func (cp *ChannelPricing) Update() error {
  33. cp.UpdatedTime = common.GetTimestamp()
  34. return DB.Model(&ChannelPricing{}).Where("id = ?", cp.Id).Updates(map[string]interface{}{
  35. "quota_type": cp.QuotaType,
  36. "model_ratio": cp.ModelRatio,
  37. "completion_ratio": cp.CompletionRatio,
  38. "model_price": cp.ModelPrice,
  39. "tag_ids": cp.TagIds,
  40. "updated_time": cp.UpdatedTime,
  41. }).Error
  42. }
  43. func (cp *ChannelPricing) Delete() error {
  44. return DB.Delete(cp).Error
  45. }
  46. // GetChannelPricing 获取指定模型在指定渠道的定价
  47. func GetChannelPricing(modelName string, channelId int) (*ChannelPricing, error) {
  48. var cp ChannelPricing
  49. err := DB.Where("model_name = ? AND channel_id = ?", modelName, channelId).First(&cp).Error
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &cp, nil
  54. }
  55. // GetChannelPricingByModel 获取指定模型的所有渠道定价
  56. func GetChannelPricingByModel(modelName string) ([]*ChannelPricing, error) {
  57. var list []*ChannelPricing
  58. err := DB.Where("model_name = ?", modelName).Find(&list).Error
  59. return list, err
  60. }
  61. // GetAllChannelPricing 获取所有渠道定价(分页)
  62. func GetAllChannelPricing(offset int, limit int) ([]*ChannelPricing, int64, error) {
  63. var list []*ChannelPricing
  64. var total int64
  65. if err := DB.Model(&ChannelPricing{}).Count(&total).Error; err != nil {
  66. return nil, 0, err
  67. }
  68. err := DB.Order("id DESC").Offset(offset).Limit(limit).Find(&list).Error
  69. return list, total, err
  70. }
  71. // BatchUpsertChannelPricing 批量创建或更新渠道定价
  72. func BatchUpsertChannelPricing(pricings []*ChannelPricing) error {
  73. if len(pricings) == 0 {
  74. return nil
  75. }
  76. now := common.GetTimestamp()
  77. for _, cp := range pricings {
  78. cp.CreatedTime = now
  79. cp.UpdatedTime = now
  80. }
  81. // GORM 的 GORM:OnConflict 会自动处理唯一键冲突
  82. return DB.Create(&pricings).Error
  83. }