ability.go 2.5 KB

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