ability.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package model
  2. import (
  3. "one-api/common"
  4. "strings"
  5. )
  6. type Ability struct {
  7. Group string `json:"group" gorm:"type:varchar(32);primaryKey;autoIncrement:false"`
  8. Model string `json:"model" gorm:"primaryKey;autoIncrement:false"`
  9. ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
  10. Enabled bool `json:"enabled"`
  11. Priority *int64 `json:"priority" gorm:"bigint;default:0;index"`
  12. }
  13. func GetGroupModels(group string) []string {
  14. var models []string
  15. // Find distinct models
  16. groupCol := "`group`"
  17. if common.UsingPostgreSQL {
  18. groupCol = `"group"`
  19. }
  20. DB.Table("abilities").Where(groupCol+" = ? and enabled = ?", group, true).Distinct("model").Pluck("model", &models)
  21. return models
  22. }
  23. func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
  24. ability := Ability{}
  25. groupCol := "`group`"
  26. trueVal := "1"
  27. if common.UsingPostgreSQL {
  28. groupCol = `"group"`
  29. trueVal = "true"
  30. }
  31. var err error = nil
  32. maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(groupCol+" = ? and model = ? and enabled = "+trueVal, group, model)
  33. channelQuery := DB.Where(groupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = (?)", group, model, maxPrioritySubQuery)
  34. if common.UsingSQLite || common.UsingPostgreSQL {
  35. err = channelQuery.Order("RANDOM()").First(&ability).Error
  36. } else {
  37. err = channelQuery.Order("RAND()").First(&ability).Error
  38. }
  39. if err != nil {
  40. return nil, err
  41. }
  42. channel := Channel{}
  43. channel.Id = ability.ChannelId
  44. err = DB.First(&channel, "id = ?", ability.ChannelId).Error
  45. return &channel, err
  46. }
  47. func (channel *Channel) AddAbilities() error {
  48. models_ := strings.Split(channel.Models, ",")
  49. groups_ := strings.Split(channel.Group, ",")
  50. abilities := make([]Ability, 0, len(models_))
  51. for _, model := range models_ {
  52. for _, group := range groups_ {
  53. ability := Ability{
  54. Group: group,
  55. Model: model,
  56. ChannelId: channel.Id,
  57. Enabled: channel.Status == common.ChannelStatusEnabled,
  58. Priority: channel.Priority,
  59. }
  60. abilities = append(abilities, ability)
  61. }
  62. }
  63. return DB.Create(&abilities).Error
  64. }
  65. func (channel *Channel) DeleteAbilities() error {
  66. return DB.Where("channel_id = ?", channel.Id).Delete(&Ability{}).Error
  67. }
  68. // UpdateAbilities updates abilities of this channel.
  69. // Make sure the channel is completed before calling this function.
  70. func (channel *Channel) UpdateAbilities() error {
  71. // A quick and dirty way to update abilities
  72. // First delete all abilities of this channel
  73. err := channel.DeleteAbilities()
  74. if err != nil {
  75. return err
  76. }
  77. // Then add new abilities
  78. err = channel.AddAbilities()
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func UpdateAbilityStatus(channelId int, status bool) error {
  85. return DB.Model(&Ability{}).Where("channel_id = ?", channelId).Select("enabled").Update("enabled", status).Error
  86. }