增加两个批量查询接口

This commit is contained in:
2025-11-16 16:30:26 +08:00
parent bf747e22ce
commit bf1600b385
10 changed files with 1041 additions and 11 deletions

View File

@@ -36,6 +36,8 @@ type ThresholdAlarmService interface {
DeleteDeviceThresholdAlarm(ctx context.Context, taskID int, req *dto.DeleteDeviceThresholdAlarmDTO) error
// DeleteDeviceThresholdAlarmByDeviceID 实现了根据设备ID删除一个设备下所有设备阈值告警的逻辑。
DeleteDeviceThresholdAlarmByDeviceID(ctx context.Context, deviceID uint32) error
// ListDeviceThresholdAlarms 批量查询设备阈值告警配置。
ListDeviceThresholdAlarms(ctx context.Context, req *dto.ListDeviceThresholdAlarmRequest) (*dto.ListDeviceThresholdAlarmResponse, error)
// CreateAreaThresholdAlarm 创建一个区域阈值告警。
CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error
@@ -47,6 +49,8 @@ type ThresholdAlarmService interface {
DeleteAreaThresholdAlarm(ctx context.Context, taskID int) error
// DeleteAreaThresholdAlarmByAreaControllerID 实现了根据区域ID删除一个区域下所有区域阈值告警的逻辑。
DeleteAreaThresholdAlarmByAreaControllerID(ctx context.Context, areaControllerID uint32) error
// ListAreaThresholdAlarms 批量查询区域阈值告警配置。
ListAreaThresholdAlarms(ctx context.Context, req *dto.ListAreaThresholdAlarmRequest) (*dto.ListAreaThresholdAlarmResponse, error)
}
// thresholdAlarmService 是 ThresholdAlarmService 接口的具体实现。
@@ -444,6 +448,110 @@ func (s *thresholdAlarmService) DeleteDeviceThresholdAlarmByDeviceID(ctx context
}
// ListDeviceThresholdAlarms 实现了批量查询设备阈值告警配置的逻辑。
func (s *thresholdAlarmService) ListDeviceThresholdAlarms(ctx context.Context, req *dto.ListDeviceThresholdAlarmRequest) (*dto.ListDeviceThresholdAlarmResponse, error) {
serviceCtx, logger := logs.Trace(ctx, s.ctx, "ListDeviceThresholdAlarms")
// 1. 准备调用 planRepo.ListTasks 的选项
taskType := models.TaskTypeDeviceThresholdCheck
opts := repository.TaskListOptions{
Type: &taskType,
DeviceID: req.DeviceID,
SensorType: req.SensorType,
Level: req.Level,
OrderBy: req.OrderBy,
}
// 2. 调用底层的 ListTasks 方法
tasks, total, err := s.planRepo.ListTasks(serviceCtx, opts, req.Page, req.PageSize)
if err != nil {
return nil, fmt.Errorf("查询设备阈值告警任务失败: %w", err)
}
// 3. 将查询到的 models.Task 列表转换为 dto.DeviceThresholdAlarmDTO 列表
alarmDTOs := make([]dto.DeviceThresholdAlarmDTO, 0, len(tasks))
for _, t := range tasks {
var params task.DeviceThresholdCheckParams
if err := t.ParseParameters(&params); err != nil {
logger.Warnf("解析任务 %d 的参数失败: %v已在列表中跳过", t.ID, err)
continue
}
alarmDTOs = append(alarmDTOs, dto.DeviceThresholdAlarmDTO{
ID: t.ID,
DeviceID: params.DeviceID,
SensorType: params.SensorType,
Thresholds: params.Thresholds,
Operator: params.Operator,
Level: params.Level,
})
}
// 4. 构建并返回响应
response := &dto.ListDeviceThresholdAlarmResponse{
List: alarmDTOs,
Pagination: dto.PaginationDTO{
Total: total,
Page: req.Page,
PageSize: req.PageSize,
},
}
return response, nil
}
// ListAreaThresholdAlarms 实现了批量查询区域阈值告警配置的逻辑。
func (s *thresholdAlarmService) ListAreaThresholdAlarms(ctx context.Context, req *dto.ListAreaThresholdAlarmRequest) (*dto.ListAreaThresholdAlarmResponse, error) {
serviceCtx, logger := logs.Trace(ctx, s.ctx, "ListAreaThresholdAlarms")
// 1. 准备调用 planRepo.ListTasks 的选项
taskType := models.TaskTypeAreaCollectorThresholdCheck
opts := repository.TaskListOptions{
Type: &taskType,
AreaControllerID: req.AreaControllerID,
SensorType: req.SensorType,
Level: req.Level,
OrderBy: req.OrderBy,
}
// 2. 调用底层的 ListTasks 方法
tasks, total, err := s.planRepo.ListTasks(serviceCtx, opts, req.Page, req.PageSize)
if err != nil {
return nil, fmt.Errorf("查询区域阈值告警任务失败: %w", err)
}
// 3. 将查询到的 models.Task 列表转换为 dto.AreaThresholdAlarmDTO 列表
alarmDTOs := make([]dto.AreaThresholdAlarmDTO, 0, len(tasks))
for _, t := range tasks {
var params task.AreaThresholdCheckParams
if err := t.ParseParameters(&params); err != nil {
logger.Warnf("解析区域任务 %d 的参数失败: %v已在列表中跳过", t.ID, err)
continue
}
alarmDTOs = append(alarmDTOs, dto.AreaThresholdAlarmDTO{
ID: t.ID,
AreaControllerID: params.AreaControllerID,
SensorType: params.SensorType,
Thresholds: params.Thresholds,
Operator: params.Operator,
Level: params.Level,
})
}
// 4. 构建并返回响应
response := &dto.ListAreaThresholdAlarmResponse{
List: alarmDTOs,
Pagination: dto.PaginationDTO{
Total: total,
Page: req.Page,
PageSize: req.PageSize,
},
}
return response, nil
}
// CreateAreaThresholdAlarm 实现了创建一个区域阈值告警的逻辑。
func (s *thresholdAlarmService) CreateAreaThresholdAlarm(ctx context.Context, req *dto.CreateAreaThresholdAlarmDTO) error {
serviceCtx, logger := logs.Trace(ctx, s.ctx, "CreateAreaThresholdAlarm")