记录信号强度数据
This commit is contained in:
@@ -32,6 +32,9 @@ type DeviceRepository interface {
|
||||
|
||||
// Delete 根据主键 ID 删除一个设备
|
||||
Delete(id uint) error
|
||||
|
||||
// FindByDevEui 根据 DevEui (存储在 properties JSONB 中的 lora_address) 查找设备 (新增)
|
||||
FindByDevEui(devEui string) (*models.Device, error)
|
||||
}
|
||||
|
||||
// gormDeviceRepository 是 DeviceRepository 的 GORM 实现
|
||||
@@ -108,3 +111,13 @@ func (r *gormDeviceRepository) Update(device *models.Device) error {
|
||||
func (r *gormDeviceRepository) Delete(id uint) error {
|
||||
return r.db.Delete(&models.Device{}, id).Error
|
||||
}
|
||||
|
||||
// FindByDevEui 根据 DevEui (存储在 properties JSONB 中的 lora_address) 查找设备
|
||||
func (r *gormDeviceRepository) FindByDevEui(devEui string) (*models.Device, error) {
|
||||
var device models.Device
|
||||
// 使用 GORM 的 JSONB 查询语法: properties->>'lora_address'
|
||||
if err := r.db.Where("properties->>'lora_address' = ?", devEui).First(&device).Error; err != nil {
|
||||
return nil, err // 如果找不到或发生其他错误,GORM 会返回错误
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
27
internal/infra/repository/sensor_data_repository.go
Normal file
27
internal/infra/repository/sensor_data_repository.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SensorDataRepository 定义了与传感器数据相关的数据库操作接口。
|
||||
type SensorDataRepository interface {
|
||||
Create(sensorData *models.SensorData) error
|
||||
}
|
||||
|
||||
// gormSensorDataRepository 是 SensorDataRepository 的 GORM 实现。
|
||||
type gormSensorDataRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormSensorDataRepository 创建一个新的 SensorDataRepository GORM 实现实例。
|
||||
// 它直接接收一个 *gorm.DB 实例作为依赖,完全遵循项目中的既定模式。
|
||||
func NewGormSensorDataRepository(db *gorm.DB) SensorDataRepository {
|
||||
return &gormSensorDataRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 将一条新的传感器数据记录插入数据库。
|
||||
func (r *gormSensorDataRepository) Create(sensorData *models.SensorData) error {
|
||||
return r.db.Create(sensorData).Error
|
||||
}
|
||||
Reference in New Issue
Block a user