From f44a94b451a717e5fa3ca1ebb520bd748765f01b Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Mon, 10 Nov 2025 17:50:05 +0800 Subject: [PATCH] GetDeviceThresholdAlarm GetAreaThresholdAlarm --- internal/app/dto/alarm_dto.go | 20 ++++++ .../app/service/threshold_alarm_service.go | 68 +++++++++++++++++++ internal/infra/repository/plan_repository.go | 18 +++-- 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/internal/app/dto/alarm_dto.go b/internal/app/dto/alarm_dto.go index 6538184..2f097c6 100644 --- a/internal/app/dto/alarm_dto.go +++ b/internal/app/dto/alarm_dto.go @@ -113,3 +113,23 @@ type UpdateAreaThresholdAlarmDTO struct { Operator models.Operator `json:"operator" binding:"required"` // 新的操作符 Level models.SeverityLevel `json:"level,omitempty"` // 新的告警等级,可选 } + +// AreaThresholdAlarmDTO 用于表示一个区域阈值告警任务的详细信息 +type AreaThresholdAlarmDTO struct { + ID int `json:"id"` + AreaControllerID uint `json:"area_controller_id"` + SensorType models.SensorType `json:"sensor_type"` + Thresholds float64 `json:"thresholds"` + Operator models.Operator `json:"operator"` + Level models.SeverityLevel `json:"level"` +} + +// DeviceThresholdAlarmDTO 用于表示一个设备阈值告警任务的详细信息 +type DeviceThresholdAlarmDTO struct { + ID int `json:"id"` + DeviceID uint `json:"device_id"` + SensorType models.SensorType `json:"sensor_type"` + Thresholds float64 `json:"thresholds"` + Operator models.Operator `json:"operator"` + Level models.SeverityLevel `json:"level"` +} diff --git a/internal/app/service/threshold_alarm_service.go b/internal/app/service/threshold_alarm_service.go index 7f44e1b..82d65ed 100644 --- a/internal/app/service/threshold_alarm_service.go +++ b/internal/app/service/threshold_alarm_service.go @@ -25,14 +25,19 @@ type ThresholdAlarmService interface { ListActiveAlarms(ctx context.Context, req *dto.ListActiveAlarmRequest) (*dto.ListActiveAlarmResponse, error) // ListHistoricalAlarms 批量查询历史告警。 ListHistoricalAlarms(ctx context.Context, req *dto.ListHistoricalAlarmRequest) (*dto.ListHistoricalAlarmResponse, error) + // CreateDeviceThresholdAlarm 创建一个设备阈值告警。 CreateDeviceThresholdAlarm(ctx context.Context, req *dto.CreateDeviceThresholdAlarmDTO) error // UpdateDeviceThresholdAlarm 更新一个设备阈值告警。 UpdateDeviceThresholdAlarm(ctx context.Context, taskID int, req *dto.UpdateDeviceThresholdAlarmDTO) error + // GetDeviceThresholdAlarm 根据ID获取一个设备阈值告警任务。 + GetDeviceThresholdAlarm(ctx context.Context, taskID int) (*dto.DeviceThresholdAlarmDTO, error) // CreateAreaThresholdAlarm 创建一个区域阈值告警。 CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error // UpdateAreaThresholdAlarm 更新一个区域阈值告警。 UpdateAreaThresholdAlarm(ctx context.Context, taskID int, req *dto.UpdateAreaThresholdAlarmDTO) error + // GetAreaThresholdAlarm 根据ID获取一个区域阈值告警任务。 + GetAreaThresholdAlarm(ctx context.Context, taskID int) (*dto.AreaThresholdAlarmDTO, error) } // thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。 @@ -254,6 +259,38 @@ func (s *thresholdAlarmService) UpdateDeviceThresholdAlarm(ctx context.Context, return err } +// GetDeviceThresholdAlarm 实现了根据ID获取一个设备阈值告警任务的逻辑。 +func (s *thresholdAlarmService) GetDeviceThresholdAlarm(ctx context.Context, taskID int) (*dto.DeviceThresholdAlarmDTO, error) { + serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetDeviceThresholdAlarm") + + // 1. 使用 planRepo 查询任务 + t, err := s.planRepo.FindTaskByID(serviceCtx, taskID) + if err != nil { + return nil, err // 如果未找到或发生其他错误,直接返回 + } + + // 2. 验证任务类型是否正确 + if t.Type != models.TaskTypeDeviceThresholdCheck { + return nil, fmt.Errorf("任务 %d 不是一个设备阈值检查任务", taskID) + } + var params task.DeviceThresholdCheckParams + err = t.ParseParameters(¶ms) + if err != nil { + return nil, fmt.Errorf("任务 %d: 解析参数失败: %w", taskID, err) + } + + resp := &dto.DeviceThresholdAlarmDTO{ + ID: t.ID, + DeviceID: params.DeviceID, + SensorType: params.SensorType, + Thresholds: params.Thresholds, + Operator: params.Operator, + Level: params.Level, + } + + return resp, nil +} + // CreateAreaThresholdAlarm 实现了创建一个区域阈值告警的逻辑。 func (s *thresholdAlarmService) CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error { serviceCtx, logger := logs.Trace(ctx, s.ctx, "CreateAreaThresholdAlarm") @@ -374,3 +411,34 @@ func (s *thresholdAlarmService) UpdateAreaThresholdAlarm(ctx context.Context, ta _, err = s.planService.UpdatePlan(serviceCtx, plan, models.PlanTypeSystem) return err } + +// GetAreaThresholdAlarm 实现了根据ID获取一个区域阈值告警任务的逻辑。 +func (s *thresholdAlarmService) GetAreaThresholdAlarm(ctx context.Context, taskID int) (*dto.AreaThresholdAlarmDTO, error) { + serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetAreaThresholdAlarm") + + // 1. 使用 planRepo 查询任务 + t, err := s.planRepo.FindTaskByID(serviceCtx, taskID) + if err != nil { + return nil, err // 如果未找到或发生其他错误,直接返回 + } + + // 2. 验证任务类型是否正确 + if t.Type != models.TaskTypeAreaCollectorThresholdCheck { + return nil, fmt.Errorf("任务 %d 不是一个区域阈值检查任务", taskID) + } + var params task.AreaThresholdCheckParams + err = t.ParseParameters(¶ms) + if err != nil { + return nil, fmt.Errorf("任务 %d: 解析参数失败: %w", taskID, err) + } + + resp := &dto.AreaThresholdAlarmDTO{ + ID: t.ID, + AreaControllerID: params.AreaControllerID, + SensorType: params.SensorType, + Thresholds: params.Thresholds, + Operator: params.Operator, + Level: params.Level, + } + return resp, nil +} diff --git a/internal/infra/repository/plan_repository.go b/internal/infra/repository/plan_repository.go index 9418db1..e623fc0 100644 --- a/internal/infra/repository/plan_repository.go +++ b/internal/infra/repository/plan_repository.go @@ -69,25 +69,22 @@ type PlanRepository interface { FlattenPlanTasks(ctx context.Context, planID uint) ([]models.Task, error) // DeleteTask 根据ID删除任务 DeleteTask(ctx context.Context, id int) error + // FindTaskByID 根据ID获取任务的基本信息 + FindTaskByID(ctx context.Context, id int) (*models.Task, error) // FindPlanAnalysisTaskByParamsPlanID 根据Parameters中的ParamsPlanID字段值查找TaskPlanAnalysis类型的Task FindPlanAnalysisTaskByParamsPlanID(ctx context.Context, paramsPlanID uint) (*models.Task, error) // FindRunnablePlans 获取所有应执行的计划 FindRunnablePlans(ctx context.Context) ([]*models.Plan, error) // FindInactivePlans 获取所有已禁用或已停止的计划 FindInactivePlans(ctx context.Context) ([]*models.Plan, error) - // FindPlanAnalysisTaskByPlanID 根据 PlanID 找到其关联的 'plan_analysis' 任务 FindPlanAnalysisTaskByPlanID(ctx context.Context, planID uint) (*models.Task, error) - // CreatePlanAnalysisTask 创建一个 plan_analysis 类型的任务并返回它 CreatePlanAnalysisTask(ctx context.Context, plan *models.Plan) (*models.Task, error) - // FindPlansWithPendingTasks 查找所有正在执行的计划 FindPlansWithPendingTasks(ctx context.Context) ([]*models.Plan, error) - // StopPlanTransactionally 停止一个计划的执行,包括更新状态、移除待执行任务和更新执行日志 StopPlanTransactionally(ctx context.Context, planID uint) error - // UpdatePlanStateAfterExecution 更新计划执行后的状态(计数和状态) UpdatePlanStateAfterExecution(ctx context.Context, planID uint, newCount uint, newStatus models.PlanStatus) error } @@ -870,3 +867,14 @@ func (r *gormPlanRepository) UpdateExecuteCount(ctx context.Context, id uint, co } return nil } + +// FindTaskByID 根据ID获取任务的基本信息 +func (r *gormPlanRepository) FindTaskByID(ctx context.Context, id int) (*models.Task, error) { + repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTaskByID") + var task models.Task + result := r.db.WithContext(repoCtx).First(&task, id) + if result.Error != nil { + return nil, result.Error + } + return &task, nil +}