diff --git a/services/tp_scenario_strategy_service.go b/services/tp_scenario_strategy_service.go index 24b1805a6..417d9adbd 100644 --- a/services/tp_scenario_strategy_service.go +++ b/services/tp_scenario_strategy_service.go @@ -5,6 +5,7 @@ import ( "ThingsPanel-Go/models" "ThingsPanel-Go/utils" valid "ThingsPanel-Go/validate" + "encoding/json" "errors" "time" @@ -80,6 +81,7 @@ func (*TpScenarioStrategyService) AddTpScenarioStrategy(tp_scenario_strategy val return tp_scenario_strategy, result.Error } for _, scenarioAction := range tp_scenario_strategy.AddTpScenarioActions { + scenarioAction.Id = utils.GetUuid() scenarioAction.ScenarioStrategyId = tp_scenario_strategy.Id // DeviceId外键可以为null,需要用map处理 @@ -87,6 +89,27 @@ func (*TpScenarioStrategyService) AddTpScenarioStrategy(tp_scenario_strategy val if scenarioAction.DeviceId == "" { delete(scenarioActionMap, "DeviceId") } + // 告警 + if scenarioAction.ActionType == "3" { + // 创建策略 + delete(scenarioActionMap, "Instruct") + scenarioAction.WarningStrategy.Id = utils.GetUuid() + result := tx.Model(&models.TpWarningStrategy{}).Create(scenarioAction.WarningStrategy) + if result.Error != nil { + tx.Rollback() + logs.Error(result.Error.Error()) + return tp_scenario_strategy, result.Error + } + warning_strategy := make(map[string]interface{}) + warning_strategy["warning_strategy_id"] = scenarioAction.WarningStrategy.Id + jsonData, err := json.Marshal(warning_strategy) + if err != nil { + return tp_scenario_strategy, err + } + scenarioActionMap["instruct"] = jsonData + } + + delete(scenarioActionMap, "WarningStrategy") result := tx.Model(&models.TpScenarioAction{}).Create(scenarioActionMap) if result.Error != nil { tx.Rollback() diff --git a/validate/tp_scenario_action_validate.go b/validate/tp_scenario_action_validate.go index 1c5081ebf..5899f339c 100644 --- a/validate/tp_scenario_action_validate.go +++ b/validate/tp_scenario_action_validate.go @@ -13,13 +13,14 @@ type EditTpScenarioActionValidate struct { } type AddTpScenarioActionValidate struct { - Id string `json:"id" gorm:"primaryKey" valid:"MaxSize(36)"` - ScenarioStrategyId string `json:"scenario_strategy_id,omitempty" valid:"MaxSize(36)"` - ActionType string `json:"action_type,omitempty" valid:"MaxSize(2)"` - DeviceId string `json:"device_id,omitempty" valid:"MaxSize(36)"` - DeviceModel string `json:"device_model,omitempty" valid:"MaxSize(2)"` // 模型类型1-设定属性 2-调动服务 - Instruct string `json:"instruct,omitempty" valid:"MaxSize(999)"` // 指令 - Remark string `json:"remark,omitempty" valid:"MaxSize(255)"` + Id string `json:"id" gorm:"primaryKey" valid:"MaxSize(36)"` + ScenarioStrategyId string `json:"scenario_strategy_id,omitempty" valid:"MaxSize(36)"` + ActionType string `json:"action_type,omitempty" valid:"MaxSize(2)"` + DeviceId string `json:"device_id,omitempty" valid:"MaxSize(36)"` + DeviceModel string `json:"device_model,omitempty" valid:"MaxSize(2)"` // 模型类型1-设定属性 2-调动服务 + Instruct string `json:"instruct,omitempty" valid:"MaxSize(999)"` // 指令 + Remark string `json:"remark,omitempty" valid:"MaxSize(255)"` + WarningStrategy AddTpWarningStrategyValidate `json:"warning_strategy,omitempty"` } type TpScenarioActionPaginationValidate struct {