Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

100 рядки
3.0 KiB

  1. package model
  2. import (
  3. "github.com/QuantumNous/new-api/common"
  4. "gorm.io/gorm"
  5. "gorm.io/gorm/clause"
  6. )
  7. // ChannelAssetCredential stores the China Mobile asset credentials separately
  8. // from the channel video-generation key.
  9. type ChannelAssetCredential struct {
  10. Id int `json:"id"`
  11. ChannelId int `json:"channel_id" gorm:"uniqueIndex;not null"`
  12. AccessKey string `json:"-" gorm:"not null;size:255"`
  13. SecretKey string `json:"-" gorm:"not null;size:255"`
  14. PoolID string `json:"pool_id" gorm:"size:255"`
  15. CreatedAt int64 `json:"created_at" gorm:"bigint;not null"`
  16. UpdatedAt int64 `json:"updated_at" gorm:"bigint;not null"`
  17. }
  18. // ChannelAssetCredentialSummary is safe to expose in channel management APIs.
  19. type ChannelAssetCredentialSummary struct {
  20. ChannelId int
  21. PoolID string
  22. }
  23. func GetChannelAssetCredential(channelID int) (*ChannelAssetCredential, error) {
  24. var credential ChannelAssetCredential
  25. err := DB.Where("channel_id = ?", channelID).First(&credential).Error
  26. if err == gorm.ErrRecordNotFound {
  27. return nil, nil
  28. }
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &credential, nil
  33. }
  34. func UpsertChannelAssetCredential(credential *ChannelAssetCredential) error {
  35. return DB.Transaction(func(tx *gorm.DB) error {
  36. return UpsertChannelAssetCredentialWithTx(tx, credential)
  37. })
  38. }
  39. func UpsertChannelAssetCredentialWithTx(tx *gorm.DB, credential *ChannelAssetCredential) error {
  40. now := common.GetTimestamp()
  41. credential.CreatedAt = now
  42. credential.UpdatedAt = now
  43. return tx.Clauses(clause.OnConflict{
  44. Columns: []clause.Column{{Name: "channel_id"}},
  45. DoUpdates: clause.Assignments(map[string]any{
  46. "access_key": credential.AccessKey,
  47. "secret_key": credential.SecretKey,
  48. "pool_id": credential.PoolID,
  49. "updated_at": credential.UpdatedAt,
  50. }),
  51. }).Create(credential).Error
  52. }
  53. func DeleteChannelAssetCredentialWithTx(tx *gorm.DB, channelID int) error {
  54. return tx.Where("channel_id = ?", channelID).Delete(&ChannelAssetCredential{}).Error
  55. }
  56. func DeleteChannelAssetCredential(channelID int) error {
  57. return DB.Transaction(func(tx *gorm.DB) error {
  58. return DeleteChannelAssetCredentialWithTx(tx, channelID)
  59. })
  60. }
  61. func DeleteChannelAssetCredentialsWithTx(tx *gorm.DB, channelIDs []int) error {
  62. if len(channelIDs) == 0 {
  63. return nil
  64. }
  65. return tx.Where("channel_id IN ?", channelIDs).Delete(&ChannelAssetCredential{}).Error
  66. }
  67. func DeleteChannelAssetCredentials(channelIDs []int) error {
  68. return DB.Transaction(func(tx *gorm.DB) error {
  69. return DeleteChannelAssetCredentialsWithTx(tx, channelIDs)
  70. })
  71. }
  72. func GetChannelAssetCredentialSummaries(channelIDs []int) (map[int]ChannelAssetCredentialSummary, error) {
  73. summaries := make(map[int]ChannelAssetCredentialSummary)
  74. if len(channelIDs) == 0 {
  75. return summaries, nil
  76. }
  77. var rows []ChannelAssetCredentialSummary
  78. if err := DB.Model(&ChannelAssetCredential{}).
  79. Select("channel_id", "pool_id").
  80. Where("channel_id IN ?", channelIDs).
  81. Find(&rows).Error; err != nil {
  82. return nil, err
  83. }
  84. for _, row := range rows {
  85. summaries[row.ChannelId] = row
  86. }
  87. return summaries, nil
  88. }