ability.go 2.6 KB

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