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

124 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-07-22 22:17:53 +08:00
package models
import (
2020-10-28 18:21:15 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2020-07-22 22:17:53 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
2020-10-28 18:21:15 +08:00
"github.com/iwind/TeaGo/types"
2020-07-22 22:17:53 +08:00
)
const (
NodeGroupStateEnabled = 1 // 已启用
NodeGroupStateDisabled = 0 // 已禁用
)
type NodeGroupDAO dbs.DAO
func NewNodeGroupDAO() *NodeGroupDAO {
return dbs.NewDAO(&NodeGroupDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNodeGroups",
Model: new(NodeGroup),
PkName: "id",
},
}).(*NodeGroupDAO)
}
2020-10-13 20:05:13 +08:00
var SharedNodeGroupDAO *NodeGroupDAO
func init() {
dbs.OnReady(func() {
SharedNodeGroupDAO = NewNodeGroupDAO()
})
}
2020-07-22 22:17:53 +08:00
// 启用条目
func (this *NodeGroupDAO) EnableNodeGroup(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
2020-07-22 22:17:53 +08:00
Pk(id).
Set("state", NodeGroupStateEnabled).
Update()
}
// 禁用条目
func (this *NodeGroupDAO) DisableNodeGroup(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
2020-07-22 22:17:53 +08:00
Pk(id).
Set("state", NodeGroupStateDisabled).
Update()
}
// 查找启用中的条目
func (this *NodeGroupDAO) FindEnabledNodeGroup(tx *dbs.Tx, id int64) (*NodeGroup, error) {
result, err := this.Query(tx).
2020-07-22 22:17:53 +08:00
Pk(id).
Attr("state", NodeGroupStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NodeGroup), err
}
// 根据主键查找名称
func (this *NodeGroupDAO) FindNodeGroupName(tx *dbs.Tx, id int64) (string, error) {
name, err := this.Query(tx).
2020-07-22 22:17:53 +08:00
Pk(id).
Result("name").
FindCol("")
return name.(string), err
}
2020-10-28 18:21:15 +08:00
// 创建分组
func (this *NodeGroupDAO) CreateNodeGroup(tx *dbs.Tx, clusterId int64, name string) (int64, error) {
var op = NewNodeGroupOperator()
2020-10-28 18:21:15 +08:00
op.ClusterId = clusterId
op.Name = name
op.State = NodeGroupStateEnabled
err := this.Save(tx, op)
2020-10-28 18:21:15 +08:00
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 修改分组
func (this *NodeGroupDAO) UpdateNodeGroup(tx *dbs.Tx, groupId int64, name string) error {
2020-10-28 18:21:15 +08:00
if groupId <= 0 {
return errors.New("invalid groupId")
}
var op = NewNodeGroupOperator()
2020-10-28 18:21:15 +08:00
op.Id = groupId
op.Name = name
err := this.Save(tx, op)
2020-10-28 18:21:15 +08:00
return err
}
// 查询所有分组
func (this *NodeGroupDAO) FindAllEnabledGroupsWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NodeGroup, err error) {
_, err = this.Query(tx).
2020-10-28 18:21:15 +08:00
State(NodeGroupStateEnabled).
Attr("clusterId", clusterId).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// 保存排序
func (this *NodeGroupDAO) UpdateGroupOrders(tx *dbs.Tx, groupIds []int64) error {
2020-10-28 18:21:15 +08:00
for index, groupId := range groupIds {
_, err := this.Query(tx).
2020-10-28 18:21:15 +08:00
Pk(groupId).
Set("order", len(groupIds)-index).
Update()
if err != nil {
return err
}
}
return nil
}