CreateDeviceThresholdAlarm

UpdateDeviceThresholdAlarm
CreateAreaThresholdAlarm
UpdateAreaThresholdAlarm
This commit is contained in:
2025-11-10 17:35:22 +08:00
parent d4e8aba1fd
commit f2b0c2987f
10 changed files with 375 additions and 38 deletions

View File

@@ -11,11 +11,13 @@ import (
"gorm.io/gorm"
)
type PlanName string
const (
// PlanNamePeriodicSystemHealthCheck 是周期性系统健康检查计划的名称
PlanNamePeriodicSystemHealthCheck = "周期性系统健康检查"
PlanNamePeriodicSystemHealthCheck PlanName = "周期性系统健康检查"
// PlanNameAlarmNotification 是告警通知发送计划的名称
PlanNameAlarmNotification = "告警通知发送"
PlanNameAlarmNotification PlanName = "告警通知发送"
)
// PlanExecutionType 定义了计划的执行类型
@@ -38,11 +40,13 @@ const (
type TaskType string
const (
TaskPlanAnalysis TaskType = "计划分析" // 解析Plan的Task列表并添加到待执行队列的特殊任务
TaskTypeWaiting TaskType = "等待" // 等待任务
TaskTypeReleaseFeedWeight TaskType = "下料" // 下料口释放指定重量任务
TaskTypeFullCollection TaskType = "全量采集" // 新增的全量采集任务
TaskTypeAlarmNotification TaskType = "告警通知" // 告警通知任务
TaskPlanAnalysis TaskType = "计划分析" // 解析Plan的Task列表并添加到待执行队列的特殊任务
TaskTypeWaiting TaskType = "等待" // 等待任务
TaskTypeReleaseFeedWeight TaskType = "下料" // 下料口释放指定重量任务
TaskTypeFullCollection TaskType = "全量采集" // 新增的全量采集任务
TaskTypeAlarmNotification TaskType = "告警通知" // 告警通知任务
TaskTypeDeviceThresholdCheck TaskType = "设备阈值检查" // 设备阈值检查任务
TaskTypeAreaCollectorThresholdCheck TaskType = "区域阈值检查" // 区域阈值检查任务
)
// -- Task Parameters --
@@ -72,7 +76,7 @@ const (
type Plan struct {
gorm.Model
Name string `gorm:"not null" json:"name"`
Name PlanName `gorm:"not null" json:"name"`
Description string `json:"description"`
PlanType PlanType `gorm:"not null;index" json:"plan_type"` // 任务类型, 包括系统任务和用户自定义任务
ExecutionType PlanExecutionType `gorm:"not null;index" json:"execution_type"`

View File

@@ -49,6 +49,8 @@ type PlanRepository interface {
GetPlanByID(ctx context.Context, id uint) (*models.Plan, error)
// GetPlansByIDs 根据ID列表获取计划不包含子计划和任务详情
GetPlansByIDs(ctx context.Context, ids []uint) ([]models.Plan, error)
// GetSystemPlanByName 根据计划名称获取系统计划,包含子计划和任务详情
GetSystemPlanByName(ctx context.Context, planName models.PlanName) (*models.Plan, error)
// CreatePlan 创建一个新的计划
CreatePlan(ctx context.Context, plan *models.Plan) error
// CreatePlanTx 在指定事务中创建一个新的计划
@@ -164,6 +166,23 @@ func (r *gormPlanRepository) GetPlansByIDs(ctx context.Context, ids []uint) ([]m
return plans, nil
}
// GetSystemPlanByName 根据计划名称获取系统计划,包含子计划和任务详情, 系统任务不该有重名情况, 所以可以这么查询
func (r *gormPlanRepository) GetSystemPlanByName(ctx context.Context, planName models.PlanName) (*models.Plan, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetSystemPlanByName")
var plan models.Plan
// 首先只查询计划的基本信息获取其ID
err := r.db.WithContext(repoCtx).Select("id").Where("name = ? AND plan_type = ?", planName, models.PlanTypeSystem).First(&plan).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil // 未找到系统计划不是错误
}
if err != nil {
return nil, fmt.Errorf("查询系统计划 '%s' 失败: %w", planName, err)
}
// 如果找到了计划ID则复用 GetPlanByID 来获取完整的计划详情
return r.GetPlanByID(repoCtx, plan.ID)
}
// GetPlanByID 根据ID获取计划包含子计划和任务详情
func (r *gormPlanRepository) GetPlanByID(ctx context.Context, id uint) (*models.Plan, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPlanByID")