ListDeviceCommandLogs
This commit is contained in:
@@ -7,13 +7,22 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeviceCommandLogListOptions 定义了查询设备命令日志时的可选参数
|
||||
type DeviceCommandLogListOptions struct {
|
||||
DeviceID *uint
|
||||
ReceivedSuccess *bool
|
||||
StartTime *time.Time // 基于 sent_at 字段
|
||||
EndTime *time.Time // 基于 sent_at 字段
|
||||
OrderBy string // 例如 "sent_at asc"
|
||||
}
|
||||
|
||||
// DeviceCommandLogRepository 定义了设备下行命令历史记录的数据访问接口
|
||||
type DeviceCommandLogRepository interface {
|
||||
Create(record *models.DeviceCommandLog) error
|
||||
FindByMessageID(messageID string) (*models.DeviceCommandLog, error)
|
||||
// UpdateAcknowledgedAt 用于更新指定 MessageID 的下行命令记录的确认时间及接收成功状态。
|
||||
// AcknowledgedAt 和 ReceivedSuccess 字段会被更新。
|
||||
UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error
|
||||
// List 支持分页和过滤的列表查询
|
||||
List(opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error)
|
||||
}
|
||||
|
||||
// gormDeviceCommandLogRepository 是 DeviceCommandLogRepository 接口的 GORM 实现
|
||||
@@ -42,7 +51,6 @@ func (r *gormDeviceCommandLogRepository) FindByMessageID(messageID string) (*mod
|
||||
|
||||
// UpdateAcknowledgedAt 实现 DeviceCommandLogRepository 接口的 UpdateAcknowledgedAt 方法
|
||||
func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error {
|
||||
// 使用 Updates 方法更新指定字段
|
||||
return r.db.Model(&models.DeviceCommandLog{}).
|
||||
Where("message_id = ?", messageID).
|
||||
Updates(map[string]interface{}{
|
||||
@@ -50,3 +58,48 @@ func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string,
|
||||
"received_success": receivedSuccess,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// List 实现了分页和过滤查询设备命令日志的功能
|
||||
func (r *gormDeviceCommandLogRepository) List(opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error) {
|
||||
// --- 校验分页参数 ---
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.DeviceCommandLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.DeviceCommandLog{})
|
||||
|
||||
// --- 应用过滤条件 ---
|
||||
if opts.DeviceID != nil {
|
||||
query = query.Where("device_id = ?", *opts.DeviceID)
|
||||
}
|
||||
if opts.ReceivedSuccess != nil {
|
||||
query = query.Where("received_success = ?", *opts.ReceivedSuccess)
|
||||
}
|
||||
if opts.StartTime != nil {
|
||||
query = query.Where("sent_at >= ?", *opts.StartTime)
|
||||
}
|
||||
if opts.EndTime != nil {
|
||||
query = query.Where("sent_at <= ?", *opts.EndTime)
|
||||
}
|
||||
|
||||
// --- 计算总数 ---
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// --- 应用排序条件 ---
|
||||
orderBy := "sent_at 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user