Files
pig-farm-controller/internal/infra/repository/sensor_data_repository.go

103 lines
3.6 KiB
Go
Raw Normal View History

2025-09-24 21:53:18 +08:00
package repository
import (
2025-11-05 23:00:07 +08:00
"context"
2025-09-25 11:17:13 +08:00
"time"
2025-11-05 23:00:07 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
2025-09-24 21:53:18 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
2025-11-05 23:00:07 +08:00
2025-09-24 21:53:18 +08:00
"gorm.io/gorm"
)
2025-10-18 15:31:05 +08:00
// SensorDataListOptions 定义了查询传感器数据列表时的可选参数
type SensorDataListOptions struct {
2025-11-10 22:23:31 +08:00
DeviceID *uint32
2025-10-18 15:31:05 +08:00
SensorType *models.SensorType
StartTime *time.Time
EndTime *time.Time
OrderBy string // 例如 "time DESC"
}
2025-09-24 21:53:18 +08:00
// SensorDataRepository 定义了与传感器数据相关的数据库操作接口。
type SensorDataRepository interface {
2025-11-05 23:00:07 +08:00
Create(ctx context.Context, sensorData *models.SensorData) error
2025-11-10 22:23:31 +08:00
GetLatestSensorDataByDeviceIDAndSensorType(ctx context.Context, deviceID uint32, sensorType models.SensorType) (*models.SensorData, error)
2025-10-18 15:31:05 +08:00
// List 支持分页和过滤的列表查询
2025-11-05 23:00:07 +08:00
List(ctx context.Context, opts SensorDataListOptions, page, pageSize int) ([]models.SensorData, int64, error)
2025-09-24 21:53:18 +08:00
}
// gormSensorDataRepository 是 SensorDataRepository 的 GORM 实现。
type gormSensorDataRepository struct {
2025-11-05 23:00:07 +08:00
ctx context.Context
db *gorm.DB
2025-09-24 21:53:18 +08:00
}
// NewGormSensorDataRepository 创建一个新的 SensorDataRepository GORM 实现实例。
2025-11-05 23:00:07 +08:00
func NewGormSensorDataRepository(ctx context.Context, db *gorm.DB) SensorDataRepository {
return &gormSensorDataRepository{ctx: ctx, db: db}
2025-09-24 21:53:18 +08:00
}
// Create 将一条新的传感器数据记录插入数据库。
2025-11-05 23:00:07 +08:00
func (r *gormSensorDataRepository) Create(ctx context.Context, sensorData *models.SensorData) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
return r.db.WithContext(repoCtx).Create(sensorData).Error
2025-09-24 21:53:18 +08:00
}
2025-09-25 11:17:13 +08:00
// GetLatestSensorDataByDeviceIDAndSensorType 根据设备ID和传感器类型查询最新的传感器数据。
2025-11-10 22:23:31 +08:00
func (r *gormSensorDataRepository) GetLatestSensorDataByDeviceIDAndSensorType(ctx context.Context, deviceID uint32, sensorType models.SensorType) (*models.SensorData, error) {
2025-11-05 23:00:07 +08:00
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetLatestSensorDataByDeviceIDAndSensorType")
2025-09-25 11:17:13 +08:00
var sensorData models.SensorData
// 增加一个时间范围来缩小查询范围, 从而加快查找速度, 当使用时序数据库时时间范围可以让数据库忽略时间靠前的分片
2025-11-05 23:00:07 +08:00
err := r.db.WithContext(repoCtx).Where("device_id = ? AND sensor_type = ? AND time >=?", deviceID, sensorType, time.Now().Add(-24*time.Hour)).
2025-09-25 11:17:13 +08:00
Order("time DESC").
First(&sensorData).Error
return &sensorData, err
}
2025-10-18 15:31:05 +08:00
// List 实现了分页和过滤查询传感器数据的功能
2025-11-05 23:00:07 +08:00
func (r *gormSensorDataRepository) List(ctx context.Context, opts SensorDataListOptions, page, pageSize int) ([]models.SensorData, int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "List")
2025-10-18 15:31:05 +08:00
// --- 校验分页参数 ---
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
}
var results []models.SensorData
var total int64
2025-11-05 23:00:07 +08:00
query := r.db.WithContext(repoCtx).Model(&models.SensorData{})
2025-10-18 15:31:05 +08:00
// --- 应用过滤条件 ---
if opts.DeviceID != nil {
query = query.Where("device_id = ?", *opts.DeviceID)
}
if opts.SensorType != nil {
query = query.Where("sensor_type = ?", *opts.SensorType)
}
if opts.StartTime != nil {
query = query.Where("time >= ?", *opts.StartTime)
}
if opts.EndTime != nil {
query = query.Where("time <= ?", *opts.EndTime)
}
// --- 计算总数 ---
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
// --- 应用排序条件 ---
orderBy := "time DESC" // 默认排序
if opts.OrderBy != "" {
orderBy = opts.OrderBy
}
query = query.Order(orderBy)
// --- 分页 ---
offset := (page - 1) * pageSize
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
return results, total, err
}