ability.go 2.8 KB

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