53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// OtaRepository 定义了与 OTA 升级任务相关的数据库操作接口。
|
|
type OtaRepository interface {
|
|
// FindTasksByStatusesAndCreationTime 根据状态列表和创建时间查找任务。
|
|
FindTasksByStatusesAndCreationTime(ctx context.Context, statuses []models.OTATaskStatus, createdBefore time.Time) ([]*models.OTATask, error)
|
|
// Update 更新单个 OTA 任务。
|
|
Update(ctx context.Context, task *models.OTATask) error
|
|
}
|
|
|
|
// gormOtaRepository 是 OtaRepository 的 GORM 实现
|
|
type gormOtaRepository struct {
|
|
ctx context.Context
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewGormOtaRepository 创建一个新的 OtaRepository GORM 实现实例
|
|
func NewGormOtaRepository(ctx context.Context, db *gorm.DB) OtaRepository {
|
|
return &gormOtaRepository{
|
|
ctx: ctx,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// FindTasksByStatusesAndCreationTime 实现了根据状态和创建时间查找任务的逻辑。
|
|
func (r *gormOtaRepository) FindTasksByStatusesAndCreationTime(ctx context.Context,
|
|
statuses []models.OTATaskStatus,
|
|
createdBefore time.Time,
|
|
) ([]*models.OTATask, error) {
|
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindTasksByStatusesAndCreationTime")
|
|
var tasks []*models.OTATask
|
|
err := r.db.WithContext(repoCtx).
|
|
Where("status IN ? AND created_at < ?", statuses, createdBefore).
|
|
Find(&tasks).Error
|
|
return tasks, err
|
|
}
|
|
|
|
// Update 实现了更新单个 OTA 任务的逻辑。
|
|
func (r *gormOtaRepository) Update(ctx context.Context, task *models.OTATask) error {
|
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "Update")
|
|
return r.db.WithContext(repoCtx).Save(task).Error
|
|
}
|