ability.go 2.7 KB

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