加强device仓库方法
This commit is contained in:
@@ -23,6 +23,8 @@ func GetAllModels() []interface{} {
|
|||||||
&SensorData{},
|
&SensorData{},
|
||||||
&DeviceCommandLog{},
|
&DeviceCommandLog{},
|
||||||
&PendingCollection{},
|
&PendingCollection{},
|
||||||
|
&AreaController{},
|
||||||
|
&DeviceTemplate{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,8 @@ type DeviceRepository interface {
|
|||||||
// ListAll 获取所有设备的列表
|
// ListAll 获取所有设备的列表
|
||||||
ListAll() ([]*models.Device, error)
|
ListAll() ([]*models.Device, error)
|
||||||
|
|
||||||
// ListByParentID 根据父级 ID 列出所有子设备。
|
// ListByAreaControllerID 根据区域主控 ID 列出所有子设备。
|
||||||
// 如果 parentID 为 nil,则列出所有顶层设备(如区域主控)。
|
ListByAreaControllerID(areaControllerID uint) ([]*models.Device, error)
|
||||||
ListByParentID(parentID *uint) ([]*models.Device, error)
|
|
||||||
|
|
||||||
// Update 更新一个已有的设备信息
|
// Update 更新一个已有的设备信息
|
||||||
Update(device *models.Device) error
|
Update(device *models.Device) error
|
||||||
@@ -33,11 +32,8 @@ type DeviceRepository interface {
|
|||||||
// Delete 根据主键 ID 删除一个设备
|
// Delete 根据主键 ID 删除一个设备
|
||||||
Delete(id uint) error
|
Delete(id uint) error
|
||||||
|
|
||||||
// FindByDevEui 根据 DevEui (存储在 properties JSONB 中的 lora_address) 查找设备 (新增)
|
// FindByAreaControllerAndPhysicalAddress 根据区域主控ID和物理地址(总线号、总线地址)查找设备
|
||||||
FindByDevEui(devEui string) (*models.Device, error)
|
FindByAreaControllerAndPhysicalAddress(areaControllerID uint, busNumber int, busAddress int) (*models.Device, error)
|
||||||
|
|
||||||
// FindByParentAndPhysicalAddress 根据父设备ID和物理地址(总线号、总线地址)查找设备
|
|
||||||
FindByParentAndPhysicalAddress(parentID uint, busNumber int32, busAddress int32) (*models.Device, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormDeviceRepository 是 DeviceRepository 的 GORM 实现
|
// gormDeviceRepository 是 DeviceRepository 的 GORM 实现
|
||||||
@@ -58,7 +54,7 @@ func (r *gormDeviceRepository) Create(device *models.Device) error {
|
|||||||
// FindByID 根据 ID 查找设备
|
// FindByID 根据 ID 查找设备
|
||||||
func (r *gormDeviceRepository) FindByID(id uint) (*models.Device, error) {
|
func (r *gormDeviceRepository) FindByID(id uint) (*models.Device, error) {
|
||||||
var device models.Device
|
var device models.Device
|
||||||
if err := r.db.First(&device, id).Error; err != nil {
|
if err := r.db.Preload("AreaController").Preload("DeviceTemplate").First(&device, id).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &device, nil
|
return &device, nil
|
||||||
@@ -79,24 +75,16 @@ func (r *gormDeviceRepository) FindByIDString(id string) (*models.Device, error)
|
|||||||
// ListAll 获取所有设备的列表
|
// ListAll 获取所有设备的列表
|
||||||
func (r *gormDeviceRepository) ListAll() ([]*models.Device, error) {
|
func (r *gormDeviceRepository) ListAll() ([]*models.Device, error) {
|
||||||
var devices []*models.Device
|
var devices []*models.Device
|
||||||
if err := r.db.Find(&devices).Error; err != nil {
|
if err := r.db.Preload("AreaController").Preload("DeviceTemplate").Find(&devices).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return devices, nil
|
return devices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListByParentID 根据父级 ID 列出所有子设备
|
// ListByAreaControllerID 根据区域主控 ID 列出所有子设备
|
||||||
func (r *gormDeviceRepository) ListByParentID(parentID *uint) ([]*models.Device, error) {
|
func (r *gormDeviceRepository) ListByAreaControllerID(areaControllerID uint) ([]*models.Device, error) {
|
||||||
var devices []*models.Device
|
var devices []*models.Device
|
||||||
var err error
|
err := r.db.Preload("AreaController").Preload("DeviceTemplate").Where("area_controller_id = ?", areaControllerID).Find(&devices).Error
|
||||||
|
|
||||||
// 根据 parentID 是否为 nil,构造不同的查询条件
|
|
||||||
if parentID == nil {
|
|
||||||
err = r.db.Where("parent_id IS NULL").Find(&devices).Error
|
|
||||||
} else {
|
|
||||||
err = r.db.Where("parent_id = ?", *parentID).Find(&devices).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -115,27 +103,17 @@ func (r *gormDeviceRepository) Delete(id uint) error {
|
|||||||
return r.db.Delete(&models.Device{}, id).Error
|
return r.db.Delete(&models.Device{}, id).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByDevEui 根据 DevEui (存储在 properties JSONB 中的 lora_address) 查找设备
|
// FindByAreaControllerAndPhysicalAddress 根据区域主控ID和物理地址(总线号、总线地址)查找设备
|
||||||
func (r *gormDeviceRepository) FindByDevEui(devEui string) (*models.Device, error) {
|
func (r *gormDeviceRepository) FindByAreaControllerAndPhysicalAddress(areaControllerID uint, busNumber int, busAddress int) (*models.Device, error) {
|
||||||
var device models.Device
|
var device models.Device
|
||||||
// 使用 GORM 的 JSONB 查询语法: properties->>'lora_address'
|
err := r.db.Preload("AreaController").Preload("DeviceTemplate").
|
||||||
if err := r.db.Where("properties->>'lora_address' = ?", devEui).First(&device).Error; err != nil {
|
Where("area_controller_id = ?", areaControllerID).
|
||||||
return nil, err // 如果找不到或发生其他错误,GORM 会返回错误
|
Where("properties->>'bus_number' = ?", strconv.Itoa(busNumber)).
|
||||||
}
|
Where("properties->>'bus_address' = ?", strconv.Itoa(busAddress)).
|
||||||
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
|
First(&device).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("根据父设备ID %d 和物理地址 (总线号: %d, 总线地址: %d) 查找设备失败: %w", parentID, busNumber, busAddress, err)
|
return nil, fmt.Errorf("根据区域主控ID %d 和物理地址 (总线号: %d, 总线地址: %d) 查找设备失败: %w", areaControllerID, busNumber, busAddress, err)
|
||||||
}
|
}
|
||||||
return &device, nil
|
return &device, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user