ListPendingCollections
This commit is contained in:
@@ -7,6 +7,15 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PendingCollectionListOptions 定义了查询待采集请求时的可选参数
|
||||
type PendingCollectionListOptions struct {
|
||||
DeviceID *uint
|
||||
Status *models.PendingCollectionStatus
|
||||
StartTime *time.Time // 基于 created_at 字段
|
||||
EndTime *time.Time // 基于 created_at 字段
|
||||
OrderBy string // 例如 "created_at asc"
|
||||
}
|
||||
|
||||
// PendingCollectionRepository 定义了与待采集请求相关的数据库操作接口。
|
||||
type PendingCollectionRepository interface {
|
||||
// Create 创建一个新的待采集请求。
|
||||
@@ -20,6 +29,9 @@ type PendingCollectionRepository interface {
|
||||
|
||||
// MarkAllPendingAsTimedOut 将所有“待处理”请求更新为“已超时”。
|
||||
MarkAllPendingAsTimedOut() (int64, error)
|
||||
|
||||
// List 支持分页和过滤的列表查询
|
||||
List(opts PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error)
|
||||
}
|
||||
|
||||
// gormPendingCollectionRepository 是 PendingCollectionRepository 的 GORM 实现。
|
||||
@@ -65,3 +77,43 @@ func (r *gormPendingCollectionRepository) MarkAllPendingAsTimedOut() (int64, err
|
||||
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
// List 实现了分页和过滤查询待采集请求的功能
|
||||
func (r *gormPendingCollectionRepository) List(opts PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.PendingCollection
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PendingCollection{})
|
||||
|
||||
if opts.DeviceID != nil {
|
||||
query = query.Where("device_id = ?", *opts.DeviceID)
|
||||
}
|
||||
if opts.Status != nil {
|
||||
query = query.Where("status = ?", *opts.Status)
|
||||
}
|
||||
if opts.StartTime != nil {
|
||||
query = query.Where("created_at >= ?", *opts.StartTime)
|
||||
}
|
||||
if opts.EndTime != nil {
|
||||
query = query.Where("created_at <= ?", *opts.EndTime)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderBy := "created_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