This commit is contained in:
2025-11-16 21:44:50 +08:00
parent 148fa1f2bb
commit aa94b47773
6 changed files with 31 additions and 19 deletions

View File

@@ -76,7 +76,7 @@ func (r *gormDeviceRepository) Create(ctx context.Context, device *models.Device
func (r *gormDeviceRepository) FindByID(ctx context.Context, id uint32) (*models.Device, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByID")
var device models.Device
if err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").First(&device, id).Error; err != nil {
if err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").First(&device, id).Error; err != nil {
return nil, err
}
return &device, nil
@@ -111,7 +111,7 @@ func (r *gormDeviceRepository) FindByIDString(ctx context.Context, id string) (*
func (r *gormDeviceRepository) ListAll(ctx context.Context) ([]*models.Device, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAll")
var devices []*models.Device
if err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").Find(&devices).Error; err != nil {
if err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").Find(&devices).Error; err != nil {
return nil, err
}
return devices, nil
@@ -121,7 +121,7 @@ func (r *gormDeviceRepository) ListAll(ctx context.Context) ([]*models.Device, e
func (r *gormDeviceRepository) ListAllSensors(ctx context.Context) ([]*models.Device, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAllSensors")
var sensors []*models.Device
err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").
err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").
Joins("JOIN device_templates ON device_templates.id = devices.device_template_id").
Where("device_templates.category = ?", models.CategorySensor).
Find(&sensors).Error
@@ -135,7 +135,7 @@ func (r *gormDeviceRepository) ListAllSensors(ctx context.Context) ([]*models.De
func (r *gormDeviceRepository) ListByAreaControllerID(ctx context.Context, areaControllerID uint32) ([]*models.Device, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListByAreaControllerID")
var devices []*models.Device
err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").Where("area_controller_id = ?", areaControllerID).Find(&devices).Error
err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").Where("area_controller_id = ?", areaControllerID).Find(&devices).Error
if err != nil {
return nil, err
}
@@ -171,7 +171,7 @@ func (r *gormDeviceRepository) Delete(ctx context.Context, id uint32) error {
func (r *gormDeviceRepository) FindByAreaControllerAndPhysicalAddress(ctx context.Context, areaControllerID uint32, busNumber int, busAddress int) (*models.Device, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByAreaControllerAndPhysicalAddress")
var device models.Device
err := r.db.WithContext(repoCtx).Preload("AreaController").Preload("DeviceTemplate").
err := r.db.WithContext(repoCtx).Preload("AreaControllers").Preload("DeviceTemplates").
Where("area_controller_id = ?", areaControllerID).
Where("properties->>'bus_number' = ?", strconv.Itoa(busNumber)).
Where("properties->>'bus_address' = ?", strconv.Itoa(busAddress)).