bmad 分析师工作
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
@@ -44,12 +45,22 @@ type DeviceRepository interface {
|
||||
|
||||
// gormDeviceRepository 是 DeviceRepository 的 GORM 实现
|
||||
type gormDeviceRepository struct {
|
||||
db *gorm.DB
|
||||
db *gorm.DB
|
||||
deviceCommandLogRepo DeviceCommandLogRepository
|
||||
pendingCollectionRepo PendingCollectionRepository
|
||||
}
|
||||
|
||||
// NewGormDeviceRepository 创建一个新的 DeviceRepository GORM 实现实例
|
||||
func NewGormDeviceRepository(db *gorm.DB) DeviceRepository {
|
||||
return &gormDeviceRepository{db: db}
|
||||
func NewGormDeviceRepository(
|
||||
db *gorm.DB,
|
||||
deviceCommandLogRepo DeviceCommandLogRepository,
|
||||
pendingCollectionRepo PendingCollectionRepository,
|
||||
) DeviceRepository {
|
||||
return &gormDeviceRepository{
|
||||
db: db,
|
||||
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||
pendingCollectionRepo: pendingCollectionRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建一个新的设备记录
|
||||
@@ -129,6 +140,24 @@ func (r *gormDeviceRepository) Update(device *models.Device) error {
|
||||
// Delete 根据 ID 删除一个设备
|
||||
// GORM 使用软删除,记录不会从数据库中物理移除,而是设置 DeletedAt 字段。
|
||||
func (r *gormDeviceRepository) Delete(id uint) error {
|
||||
// 检查是否有相关的设备命令日志
|
||||
logs, err := r.deviceCommandLogRepo.FindByDeviceID(id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("查询设备 %d 的命令日志失败: %w", id, err)
|
||||
}
|
||||
if len(logs) > 0 {
|
||||
return errors.New("设备有相关的命令日志,不能删除")
|
||||
}
|
||||
|
||||
// 检查是否有相关的待处理采集请求
|
||||
pendingCollections, err := r.pendingCollectionRepo.FindByDeviceID(id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("查询设备 %d 的待处理采集请求失败: %w", id, err)
|
||||
}
|
||||
if len(pendingCollections) > 0 {
|
||||
return errors.New("设备有相关的待处理采集请求,不能删除")
|
||||
}
|
||||
|
||||
return r.db.Delete(&models.Device{}, id).Error
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user