117 lines
4.9 KiB
Go
117 lines
4.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ExecutionLogRepository 定义了与执行日志交互的接口。
|
|
// 这为服务层提供了一个清晰的契约,并允许在测试中轻松地进行模拟。
|
|
type ExecutionLogRepository interface {
|
|
UpdateLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error
|
|
UpdateLogStatus(logID uint, status models.ExecutionStatus) error
|
|
CreateTaskExecutionLog(log *models.TaskExecutionLog) error
|
|
CreatePlanExecutionLog(log *models.PlanExecutionLog) error
|
|
UpdatePlanExecutionLog(log *models.PlanExecutionLog) error
|
|
CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error
|
|
UpdateTaskExecutionLog(log *models.TaskExecutionLog) error
|
|
FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error)
|
|
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
|
|
UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error
|
|
|
|
// UpdatePlanExecutionLogsStatusByIDs 批量更新计划执行日志的状态
|
|
UpdatePlanExecutionLogsStatusByIDs(logIDs []uint, status models.ExecutionStatus) error
|
|
|
|
// FindIncompletePlanExecutionLogs 查找所有未完成的计划执行日志
|
|
FindIncompletePlanExecutionLogs() ([]models.PlanExecutionLog, error)
|
|
}
|
|
|
|
// gormExecutionLogRepository 是使用 GORM 的具体实现。
|
|
type gormExecutionLogRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewGormExecutionLogRepository 创建一个新的执行日志仓库。
|
|
// 它接收一个 GORM DB 实例作为依赖。
|
|
func NewGormExecutionLogRepository(db *gorm.DB) ExecutionLogRepository {
|
|
return &gormExecutionLogRepository{db: db}
|
|
}
|
|
|
|
func (r *gormExecutionLogRepository) UpdateLogStatusByIDs(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
|
|
}
|
|
|
|
func (r *gormExecutionLogRepository) UpdateLogStatus(logID uint, status models.ExecutionStatus) error {
|
|
return r.db.Model(&models.TaskExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
|
}
|
|
|
|
func (r *gormExecutionLogRepository) CreateTaskExecutionLog(log *models.TaskExecutionLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
// CreatePlanExecutionLog 为一次计划执行创建一条新的日志条目。
|
|
func (r *gormExecutionLogRepository) CreatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
// UpdatePlanExecutionLog 使用 Updates 方法更新一个计划执行日志。
|
|
// GORM 的 Updates 传入 struct 时,只会更新非零值字段。
|
|
// 在这里,我们期望传入的对象一定包含一个有效的 ID。
|
|
func (r *gormExecutionLogRepository) UpdatePlanExecutionLog(log *models.PlanExecutionLog) error {
|
|
return r.db.Updates(log).Error
|
|
}
|
|
|
|
// CreateTaskExecutionLogsInBatch 在一次数据库调用中创建多个任务执行日志条目。
|
|
// 这是“预写日志”步骤的关键。
|
|
func (r *gormExecutionLogRepository) CreateTaskExecutionLogsInBatch(logs []*models.TaskExecutionLog) error {
|
|
if len(logs) == 0 {
|
|
return nil
|
|
}
|
|
// GORM 的 Create 传入一个切片指针会执行批量插入。
|
|
return r.db.Create(&logs).Error
|
|
}
|
|
|
|
// UpdateTaskExecutionLog 使用 Updates 方法更新一个任务执行日志。
|
|
// GORM 的 Updates 传入 struct 时,只会更新非零值字段。
|
|
// 这种方式代码更直观,上层服务可以直接修改模型对象后进行保存。
|
|
func (r *gormExecutionLogRepository) UpdateTaskExecutionLog(log *models.TaskExecutionLog) error {
|
|
return r.db.Updates(log).Error
|
|
}
|
|
|
|
// FindTaskExecutionLogByID 根据 ID 查找单个任务执行日志。
|
|
// 它会预加载关联的 Task 信息。
|
|
func (r *gormExecutionLogRepository) FindTaskExecutionLogByID(id uint) (*models.TaskExecutionLog, error) {
|
|
var log models.TaskExecutionLog
|
|
// 使用 Preload("Task") 来确保关联的任务信息被一并加载
|
|
err := r.db.Preload("Task").First(&log, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &log, nil
|
|
}
|
|
|
|
// UpdatePlanExecutionLogStatus 更新计划执行日志的状态
|
|
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogStatus(logID uint, status models.ExecutionStatus) error {
|
|
return r.db.Model(&models.PlanExecutionLog{}).Where("id = ?", logID).Update("status", status).Error
|
|
}
|
|
|
|
// UpdatePlanExecutionLogsStatusByIDs 批量更新计划执行日志的状态
|
|
func (r *gormExecutionLogRepository) UpdatePlanExecutionLogsStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {
|
|
if len(logIDs) == 0 {
|
|
return nil
|
|
}
|
|
return r.db.Model(&models.PlanExecutionLog{}).Where("id IN ?", logIDs).Update("status", status).Error
|
|
}
|
|
|
|
// FindIncompletePlanExecutionLogs 查找所有未完成的计划执行日志
|
|
func (r *gormExecutionLogRepository) FindIncompletePlanExecutionLogs() ([]models.PlanExecutionLog, error) {
|
|
var logs []models.PlanExecutionLog
|
|
err := r.db.Where("status = ?", models.ExecutionStatusStarted).Find(&logs).Error
|
|
return logs, err
|
|
}
|