ListPlanExecutionLogs
This commit is contained in:
@@ -8,9 +8,18 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PlanExecutionLogListOptions 定义了查询计划执行日志时的可选参数
|
||||
type PlanExecutionLogListOptions struct {
|
||||
PlanID *uint
|
||||
Status *models.ExecutionStatus
|
||||
StartTime *time.Time // 基于 created_at 字段
|
||||
EndTime *time.Time // 基于 created_at 字段
|
||||
OrderBy string // 例如 "created_at asc"
|
||||
}
|
||||
|
||||
// ExecutionLogRepository 定义了与执行日志交互的接口。
|
||||
// 这为服务层提供了一个清晰的契约,并允许在测试中轻松地进行模拟。
|
||||
type ExecutionLogRepository interface {
|
||||
// --- Existing methods ---
|
||||
UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error
|
||||
UpdateTaskExecutionLogStatus(logID uint, status models.ExecutionStatus) error
|
||||
CreateTaskExecutionLog(log *models.TaskExecutionLog) error
|
||||
@@ -48,6 +57,9 @@ type ExecutionLogRepository interface {
|
||||
|
||||
// CancelIncompleteTasksByPlanLogID 取消一个计划执行中的所有未完成任务
|
||||
CancelIncompleteTasksByPlanLogID(planLogID uint, reason string) error
|
||||
|
||||
// --- New method ---
|
||||
ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error)
|
||||
}
|
||||
|
||||
// gormExecutionLogRepository 是使用 GORM 的具体实现。
|
||||
@@ -56,18 +68,57 @@ type gormExecutionLogRepository struct {
|
||||
}
|
||||
|
||||
// NewGormExecutionLogRepository 创建一个新的执行日志仓库。
|
||||
// 它接收一个 GORM DB 实例作为依赖。
|
||||
func NewGormExecutionLogRepository(db *gorm.DB) ExecutionLogRepository {
|
||||
return &gormExecutionLogRepository{db: db}
|
||||
}
|
||||
|
||||
// ListPlanExecutionLogs 实现了分页和过滤查询计划执行日志的功能
|
||||
func (r *gormExecutionLogRepository) ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.PlanExecutionLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PlanExecutionLog{})
|
||||
|
||||
if opts.PlanID != nil {
|
||||
query = query.Where("plan_id = ?", *opts.PlanID)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// --- Existing method implementations ---
|
||||
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {
|
||||
if len(logIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.Model(&models.TaskExecutionLog{}).
|
||||
Where("id IN ?", logIDs).
|
||||
Update("status", status).Error
|
||||
return r.db.Model(&models.TaskExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
|
||||
}
|
||||
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatus(logID uint, status models.ExecutionStatus) error {
|
||||
|
||||
Reference in New Issue
Block a user