设备和区域主控删除时清除对应区域阈值告警或设备阈值告警任务

This commit is contained in:
2025-11-10 21:14:36 +08:00
parent 9dc47ec7ad
commit 30880f8c30
6 changed files with 123 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ package repository
import (
"context"
"fmt"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
@@ -18,6 +19,8 @@ type AreaControllerRepository interface {
ListAll(ctx context.Context) ([]*models.AreaController, error)
Update(ctx context.Context, ac *models.AreaController) error
Delete(ctx context.Context, id uint) error
// IsAreaControllerUsedByTasks 检查区域主控是否被特定任务类型使用,可以忽略指定任务类型
IsAreaControllerUsedByTasks(ctx context.Context, areaControllerID uint, ignoredTaskTypes []models.TaskType) (bool, error)
}
// gormAreaControllerRepository 是 AreaControllerRepository 的 GORM 实现。
@@ -84,3 +87,66 @@ func (r *gormAreaControllerRepository) FindByNetworkID(ctx context.Context, netw
}
return &areaController, nil
}
// IsAreaControllerUsedByTasks 检查区域主控是否被特定任务类型使用,可以忽略指定任务类型
func (r *gormAreaControllerRepository) IsAreaControllerUsedByTasks(ctx context.Context, areaControllerID uint, ignoredTaskTypes []models.TaskType) (bool, error) {
repoCtx, logger := logs.Trace(ctx, r.ctx, "IsAreaControllerUsedByTasks")
// 将 ignoredTaskTypes 转换为 map以便高效查找
ignoredMap := make(map[models.TaskType]struct{})
for _, tt := range ignoredTaskTypes {
ignoredMap[tt] = struct{}{}
}
areaControllerIDStr := strconv.FormatUint(uint64(areaControllerID), 10)
// 定义所有可能与 AreaControllerID 相关的任务类型列表
// 方便未来扩展,如果新增任务类型与区域主控关联,只需在此处添加
relevantTaskTypes := []models.TaskType{
models.TaskTypeAreaCollectorThresholdCheck,
// TODO: 如果未来有其他任务类型通过 parameters 关联 AreaControllerID请在此处添加
// 例如: models.TaskTypeAnotherAreaControllerTask,
}
for _, taskType := range relevantTaskTypes {
// 如果当前任务类型在忽略列表中,则跳过检查
if _, ok := ignoredMap[taskType]; ok {
continue
}
var taskCount int64
var query *gorm.DB
// 根据任务类型构建不同的查询条件
switch taskType {
case models.TaskTypeAreaCollectorThresholdCheck:
// TaskTypeAreaCollectorThresholdCheck 任务的 AreaControllerID 存储在 parameters->>'AreaControllerID'
query = r.db.WithContext(repoCtx).
Model(&models.Task{}).
Where("type = ?", models.TaskTypeAreaCollectorThresholdCheck).
Where("parameters->>'AreaControllerID' = ?", areaControllerIDStr)
// TODO: 如果未来有其他任务类型通过不同的 parameters 字段关联 AreaControllerID请在此处添加 case
// case models.TaskTypeAnotherAreaControllerTask:
// query = r.db.WithContext(repoCtx).
// Model(&models.Task{}).
// Where("type = ?", models.TaskTypeAnotherAreaControllerTask).
// Where("parameters->>'AnotherFieldForAreaControllerID' = ?", areaControllerIDStr)
default:
// 对于未明确处理的 relevantTaskTypes可以记录警告或直接跳过
logger.Warnf(fmt.Sprintf("IsAreaControllerUsedByTasks: 未处理的区域主控相关任务类型: %s", taskType))
continue
}
if query != nil {
err := query.Count(&taskCount).Error
if err != nil {
return false, fmt.Errorf("查询区域主控任务使用情况失败 (任务类型: %s): %w", taskType, err)
}
if taskCount > 0 {
return true, nil // 发现有未被忽略的任务正在使用此区域主控
}
}
}
return false, nil // 没有发现任何未被忽略的任务正在使用此区域主控
}