|
@@ -1,6 +1,7 @@
|
|
|
package model
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "encoding/json"
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm"
|
|
|
"one-api/common"
|
|
"one-api/common"
|
|
|
)
|
|
)
|
|
@@ -29,6 +30,31 @@ type Channel struct {
|
|
|
StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
|
|
StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
|
|
|
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
|
|
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
|
|
|
AutoBan *int `json:"auto_ban" gorm:"default:1"`
|
|
AutoBan *int `json:"auto_ban" gorm:"default:1"`
|
|
|
|
|
+ OtherInfo string `json:"other_info"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (channel *Channel) GetOtherInfo() map[string]interface{} {
|
|
|
|
|
+ var otherInfo map[string]interface{}
|
|
|
|
|
+ if channel.OtherInfo != "" {
|
|
|
|
|
+ err := json.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ common.SysError("failed to unmarshal other info: " + err.Error())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return otherInfo
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
|
|
|
|
|
+ otherInfoBytes, err := json.Marshal(otherInfo)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ common.SysError("failed to marshal other info: " + err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ channel.OtherInfo = string(otherInfoBytes)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (channel *Channel) Save() error {
|
|
|
|
|
+ return DB.Save(channel).Error
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
|
|
func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
|
|
@@ -213,15 +239,30 @@ func (channel *Channel) Delete() error {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func UpdateChannelStatusById(id int, status int) {
|
|
|
|
|
|
|
+func UpdateChannelStatusById(id int, status int, reason string) {
|
|
|
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
|
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
common.SysError("failed to update ability status: " + err.Error())
|
|
common.SysError("failed to update ability status: " + err.Error())
|
|
|
}
|
|
}
|
|
|
- err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
|
|
|
|
|
|
+ channel, err := GetChannelById(id, true)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- common.SysError("failed to update channel status: " + err.Error())
|
|
|
|
|
|
|
+ // find channel by id error, directly update status
|
|
|
|
|
+ err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ common.SysError("failed to update channel status: " + err.Error())
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // find channel by id success, update status and other info
|
|
|
|
|
+ info := channel.GetOtherInfo()
|
|
|
|
|
+ info["status_reason"] = reason
|
|
|
|
|
+ channel.SetOtherInfo(info)
|
|
|
|
|
+ channel.Status = status
|
|
|
|
|
+ err = channel.Save()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ common.SysError("failed to update channel status: " + err.Error())
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func UpdateChannelUsedQuota(id int, quota int) {
|
|
func UpdateChannelUsedQuota(id int, quota int) {
|