Files
waf-platform/EdgeAPI/internal/db/models/node_action_dao_plus.go
2026-02-04 20:27:13 +08:00

176 lines
4.5 KiB
Go

// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build plus
package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
// CreateAction 添加动作
func (this *NodeActionDAO) CreateAction(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64, condsConfig *nodeconfigs.NodeActionCondsConfig, actionConfig *nodeconfigs.NodeActionConfig, duration *shared.TimeDuration) (int64, error) {
var op = NewNodeActionOperator()
op.Role = role
op.NodeId = nodeId
condsJSON, err := json.Marshal(condsConfig)
if err != nil {
return 0, err
}
op.Conds = condsJSON
actionJSON, err := json.Marshal(actionConfig)
if err != nil {
return 0, err
}
op.Action = actionJSON
if duration == nil || duration.Count <= 0 {
duration = &shared.TimeDuration{
Count: 1,
Unit: shared.TimeDurationUnitHour,
}
}
durationJSON, err := json.Marshal(duration)
if err != nil {
return 0, err
}
op.Duration = durationJSON
op.IsOn = true
op.State = NodeActionStateEnabled
return this.SaveInt64(tx, op)
}
// DuplicateNodeAction 复制动作
func (this *NodeActionDAO) DuplicateNodeAction(tx *dbs.Tx, action *NodeAction, role nodeconfigs.NodeRole, newNodeId int64) error {
if action == nil {
return errors.New("'action' should not be nil")
}
var op = NewNodeActionOperator()
op.Role = role
op.NodeId = newNodeId
op.IsOn = action.IsOn
if len(action.Conds) > 0 {
op.Conds = action.Conds
}
if len(action.Action) > 0 {
op.Action = action.Action
}
if len(action.Duration) > 0 {
op.Duration = action.Duration
}
op.Order = action.Order
op.State = action.State
return this.Save(tx, op)
}
// UpdateAction 修改动作
func (this *NodeActionDAO) UpdateAction(tx *dbs.Tx, actionId int64, condsConfig *nodeconfigs.NodeActionCondsConfig, actionConfig *nodeconfigs.NodeActionConfig, duration *shared.TimeDuration, isOn bool) error {
if actionId <= 0 {
return errors.New("invalid actionId")
}
var op = NewNodeActionOperator()
op.Id = actionId
condsJSON, err := json.Marshal(condsConfig)
if err != nil {
return err
}
op.Conds = condsJSON
actionJSON, err := json.Marshal(actionConfig)
if err != nil {
return err
}
op.Action = actionJSON
if duration == nil || duration.Count <= 0 {
duration = &shared.TimeDuration{
Count: 1,
Unit: shared.TimeDurationUnitHour,
}
}
durationJSON, err := json.Marshal(duration)
if err != nil {
return err
}
op.Duration = durationJSON
op.IsOn = isOn
return this.Save(tx, op)
}
// DisableAllNodeActions 禁用单个节点所有动作
func (this *NodeActionDAO) DisableAllNodeActions(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
return this.Query(tx).
Attr("role", role).
Attr("nodeId", nodeId).
State(NodeStateEnabled).
Set("state", NodeStateDisabled).
UpdateQuickly()
}
// FindAllNodeActions 列出节点所有动作
func (this *NodeActionDAO) FindAllNodeActions(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) (result []*NodeAction, err error) {
_, err = this.Query(tx).
State(NodeActionStateEnabled).
Attr("role", role).
Attr("nodeId", nodeId).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// FindAllAvailableNodeActionsWithParam 根据参数查询节点相关动作
// TODO 可能需要增加缓存以便于不用每次都查询数据库
func (this *NodeActionDAO) FindAllAvailableNodeActionsWithParam(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64, param nodeconfigs.NodeActionParam) (result []*NodeAction, err error) {
_, err = this.Query(tx).
State(NodeActionStateEnabled).
Attr("role", role).
Attr("nodeId", nodeId).
Attr("isOn", true).
Where("JSON_CONTAINS(conds, :paramQuery, '$.conds')").
Param("paramQuery", maps.Map{
"param": param,
}.AsJSON()).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// CountNodeActions 计算节点动作数量
func (this *NodeActionDAO) CountNodeActions(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) (int64, error) {
return this.Query(tx).
Attr("nodeId", nodeId).
Attr("role", role).
State(NodeActionStateEnabled).
Count()
}
// UpdateNodeActionOrders 设置动作顺序
func (this *NodeActionDAO) UpdateNodeActionOrders(tx *dbs.Tx, actionIds []int64) error {
var maxOrder = len(actionIds) + 1
for index, actionId := range actionIds {
err := this.Query(tx).
Pk(actionId).
Set("order", maxOrder-index).
UpdateQuickly()
if err != nil {
return err
}
}
return nil
}