删除设备模板时检查和删除区域主控时检查
This commit is contained in:
@@ -47,26 +47,11 @@ func (r *gormAreaControllerRepository) Update(ac *models.AreaController) error {
|
||||
}
|
||||
|
||||
// Delete 删除一个 AreaController 记录。
|
||||
// 在删除前会检查是否有设备关联到该主控,如果有,则不允许删除。
|
||||
func (r *gormAreaControllerRepository) Delete(id uint) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 检查是否有设备关联到这个区域主控
|
||||
var count int64
|
||||
if err := tx.Model(&models.Device{}).Where("area_controller_id = ?", id).Count(&count).Error; err != nil {
|
||||
return fmt.Errorf("检查关联设备失败: %w", err)
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return fmt.Errorf("无法删除区域主控,因为仍有 %d 个设备关联到它", count)
|
||||
}
|
||||
|
||||
// 如果没有关联设备,则执行删除操作
|
||||
if err := tx.Delete(&models.AreaController{}, id).Error; err != nil {
|
||||
return fmt.Errorf("删除区域主控失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err := r.db.Delete(&models.AreaController{}, id).Error; err != nil {
|
||||
return fmt.Errorf("删除区域主控失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindByID 通过 ID 查找一个 AreaController。
|
||||
|
||||
Reference in New Issue
Block a user