Files
EdgeAPI/internal/db/models/server_group_dao.go

72 lines
1.4 KiB
Go
Raw Normal View History

2020-07-22 22:17:53 +08:00
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
ServerGroupStateEnabled = 1 // 已启用
ServerGroupStateDisabled = 0 // 已禁用
)
type ServerGroupDAO dbs.DAO
func NewServerGroupDAO() *ServerGroupDAO {
return dbs.NewDAO(&ServerGroupDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerGroups",
Model: new(ServerGroup),
PkName: "id",
},
}).(*ServerGroupDAO)
}
2020-10-13 20:05:13 +08:00
var SharedServerGroupDAO *ServerGroupDAO
func init() {
dbs.OnReady(func() {
SharedServerGroupDAO = NewServerGroupDAO()
})
}
2020-07-22 22:17:53 +08:00
// 启用条目
2020-10-13 20:05:13 +08:00
func (this *ServerGroupDAO) EnableServerGroup(id uint32) error {
_, err := this.Query().
2020-07-22 22:17:53 +08:00
Pk(id).
Set("state", ServerGroupStateEnabled).
Update()
2020-10-13 20:05:13 +08:00
return err
2020-07-22 22:17:53 +08:00
}
// 禁用条目
2020-10-13 20:05:13 +08:00
func (this *ServerGroupDAO) DisableServerGroup(id uint32) error {
_, err := this.Query().
2020-07-22 22:17:53 +08:00
Pk(id).
Set("state", ServerGroupStateDisabled).
Update()
2020-10-13 20:05:13 +08:00
return err
2020-07-22 22:17:53 +08:00
}
// 查找启用中的条目
func (this *ServerGroupDAO) FindEnabledServerGroup(id uint32) (*ServerGroup, error) {
result, err := this.Query().
Pk(id).
Attr("state", ServerGroupStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*ServerGroup), err
}
// 根据主键查找名称
func (this *ServerGroupDAO) FindServerGroupName(id uint32) (string, error) {
2020-10-13 20:05:13 +08:00
return this.Query().
2020-07-22 22:17:53 +08:00
Pk(id).
Result("name").
2020-10-13 20:05:13 +08:00
FindStringCol("")
2020-07-22 22:17:53 +08:00
}