2023-06-07 15:26:00 +00:00
package model
import (
"one-api/common"
"strings"
)
type Ability struct {
Group string ` json:"group" gorm:"type:varchar(32);primaryKey;autoIncrement:false" `
Model string ` json:"model" gorm:"primaryKey;autoIncrement:false" `
ChannelId int ` json:"channel_id" gorm:"primaryKey;autoIncrement:false;index" `
2023-06-08 01:26:54 +00:00
Enabled bool ` json:"enabled" `
2023-09-18 14:39:10 +00:00
Priority * int64 ` json:"priority" gorm:"bigint;default:0;index" `
2023-06-07 15:26:00 +00:00
}
func GetRandomSatisfiedChannel ( group string , model string ) ( * Channel , error ) {
ability := Ability { }
var err error = nil
if common . UsingSQLite {
2023-09-18 14:39:10 +00:00
maxPrioritySubQuery := DB . Model ( & Ability { } ) . Select ( "MAX(priority)" ) . Where ( "`group` = ? and model = ? and enabled = 1" , group , model )
err = DB . Where ( "`group` = ? and model = ? and enabled = 1 and priority = (?)" , group , model , maxPrioritySubQuery ) . Order ( "RANDOM()" ) . Limit ( 1 ) . First ( & ability ) . Error
2023-06-07 15:26:00 +00:00
} else {
2023-09-18 14:39:10 +00:00
maxPrioritySubQuery := DB . Model ( & Ability { } ) . Select ( "MAX(priority)" ) . Where ( "group = ? and model = ? and enabled = 1" , group , model )
err = DB . Where ( "`group` = ? and model = ? and enabled = 1 and priority = (?)" , group , model , maxPrioritySubQuery ) . Order ( "RAND()" ) . Limit ( 1 ) . First ( & ability ) . Error
2023-06-07 15:26:00 +00:00
}
if err != nil {
return nil , err
}
channel := Channel { }
2023-06-29 03:27:34 +00:00
channel . Id = ability . ChannelId
2023-06-07 15:26:00 +00:00
err = DB . First ( & channel , "id = ?" , ability . ChannelId ) . Error
return & channel , err
}
func ( channel * Channel ) AddAbilities ( ) error {
models_ := strings . Split ( channel . Models , "," )
2023-06-14 04:14:08 +00:00
groups_ := strings . Split ( channel . Group , "," )
2023-06-07 15:26:00 +00:00
abilities := make ( [ ] Ability , 0 , len ( models_ ) )
for _ , model := range models_ {
2023-06-14 04:14:08 +00:00
for _ , group := range groups_ {
ability := Ability {
Group : group ,
Model : model ,
ChannelId : channel . Id ,
Enabled : channel . Status == common . ChannelStatusEnabled ,
2023-09-17 11:18:16 +00:00
Priority : channel . Priority ,
2023-06-14 04:14:08 +00:00
}
abilities = append ( abilities , ability )
2023-06-07 15:26:00 +00:00
}
}
return DB . Create ( & abilities ) . Error
}
func ( channel * Channel ) DeleteAbilities ( ) error {
return DB . Where ( "channel_id = ?" , channel . Id ) . Delete ( & Ability { } ) . Error
}
// UpdateAbilities updates abilities of this channel.
// Make sure the channel is completed before calling this function.
func ( channel * Channel ) UpdateAbilities ( ) error {
// A quick and dirty way to update abilities
// First delete all abilities of this channel
err := channel . DeleteAbilities ( )
if err != nil {
return err
}
// Then add new abilities
err = channel . AddAbilities ( )
if err != nil {
return err
}
return nil
}
func UpdateAbilityStatus ( channelId int , status bool ) error {
2023-06-08 01:26:54 +00:00
return DB . Model ( & Ability { } ) . Where ( "channel_id = ?" , channelId ) . Select ( "enabled" ) . Update ( "enabled" , status ) . Error
2023-06-07 15:26:00 +00:00
}