issue-18初步实现
This commit is contained in:
@@ -35,6 +35,9 @@ type DeviceRepository interface {
|
||||
|
||||
// FindByDevEui 根据 DevEui (存储在 properties JSONB 中的 lora_address) 查找设备 (新增)
|
||||
FindByDevEui(devEui string) (*models.Device, error)
|
||||
|
||||
// FindByParentAndPhysicalAddress 根据父设备ID和物理地址(总线号、总线地址)查找设备
|
||||
FindByParentAndPhysicalAddress(parentID uint, busNumber int32, busAddress int32) (*models.Device, error)
|
||||
}
|
||||
|
||||
// gormDeviceRepository 是 DeviceRepository 的 GORM 实现
|
||||
@@ -121,3 +124,18 @@ func (r *gormDeviceRepository) FindByDevEui(devEui string) (*models.Device, erro
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
// FindByParentAndPhysicalAddress 根据父设备ID和物理地址(总线号、总线地址)查找设备
|
||||
func (r *gormDeviceRepository) FindByParentAndPhysicalAddress(parentID uint, busNumber int32, busAddress int32) (*models.Device, error) {
|
||||
var device models.Device
|
||||
// PostgreSQL 使用 ->> 操作符来查询 JSONB 字段的文本值
|
||||
err := r.db.Where("parent_id = ?", parentID).
|
||||
Where("properties->>'bus_number' = ?", strconv.Itoa(int(busNumber))).
|
||||
Where("properties->>'bus_address' = ?", strconv.Itoa(int(busAddress))).
|
||||
First(&device).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("根据父设备ID %d 和物理地址 (总线号: %d, 总线地址: %d) 查找设备失败: %w", parentID, busNumber, busAddress, err)
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user