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

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

@@ -170,14 +170,8 @@ func (s *deviceService) DeleteDevice(ctx context.Context, id uint) error {
return err // 如果未找到,会返回 gorm.ErrRecordNotFound
}
// TODO 这个应该用事务处理
err = s.thresholdAlarmService.DeleteDeviceThresholdAlarmByDeviceID(serviceCtx, id)
if err != nil {
return fmt.Errorf("删除设备阈值告警失败: %w", err)
}
// 在删除前检查设备是否被任务使用
inUse, err := s.deviceRepo.IsDeviceInUse(serviceCtx, id)
inUse, err := s.deviceRepo.IsDeviceInUse(serviceCtx, id, []models.TaskType{models.TaskTypeDeviceThresholdCheck})
if err != nil {
// 如果检查过程中发生数据库错误,则返回错误
return fmt.Errorf("检查设备使用情况失败: %w", err)
@@ -187,6 +181,12 @@ func (s *deviceService) DeleteDevice(ctx context.Context, id uint) error {
return ErrDeviceInUse
}
// TODO 这个应该用事务处理
err = s.thresholdAlarmService.DeleteDeviceThresholdAlarmByDeviceID(serviceCtx, id)
if err != nil {
return fmt.Errorf("删除设备阈值告警失败: %w", err)
}
// 只有在未被使用时,才执行删除操作
return s.deviceRepo.Delete(serviceCtx, id)
}
@@ -296,14 +296,8 @@ func (s *deviceService) DeleteAreaController(ctx context.Context, id uint) error
return err // 如果未找到gorm会返回 ErrRecordNotFound
}
// TODO 这个应该用事务处理
err = s.thresholdAlarmService.DeleteAreaThresholdAlarmByAreaControllerID(serviceCtx, id)
if err != nil {
return fmt.Errorf("删除区域阈值告警失败: %w", err)
}
// 2. 检查是否被使用(业务逻辑)
inUse, err := s.deviceRepo.IsAreaControllerInUse(serviceCtx, id)
inUse, err := s.areaControllerRepo.IsAreaControllerUsedByTasks(serviceCtx, id, []models.TaskType{models.TaskTypeAreaCollectorThresholdCheck})
if err != nil {
return err // 返回数据库检查错误
}
@@ -311,6 +305,12 @@ func (s *deviceService) DeleteAreaController(ctx context.Context, id uint) error
return ErrAreaControllerInUse // 返回业务错误
}
// TODO 这个应该用事务处理
err = s.thresholdAlarmService.DeleteAreaThresholdAlarmByAreaControllerID(serviceCtx, id)
if err != nil {
return fmt.Errorf("删除区域阈值告警失败: %w", err)
}
// 3. 执行删除
return s.areaControllerRepo.Delete(serviceCtx, id)
}