diff --git a/internal/infra/database/postgres.go b/internal/infra/database/postgres.go index 87aa403..06ad895 100644 --- a/internal/infra/database/postgres.go +++ b/internal/infra/database/postgres.go @@ -156,8 +156,8 @@ func (ps *PostgresStorage) creatingHyperTable() error { }{ {models.SensorData{}, "time"}, {models.DeviceCommandLog{}, "sent_at"}, - {models.PlanExecutionLog{}, "started_at"}, - {models.TaskExecutionLog{}, "started_at"}, + {models.PlanExecutionLog{}, "created_at"}, + {models.TaskExecutionLog{}, "created_at"}, {models.PendingCollection{}, "created_at"}, {models.UserActionLog{}, "time"}, } diff --git a/internal/infra/models/execution.go b/internal/infra/models/execution.go index 392187a..d549cd7 100644 --- a/internal/infra/models/execution.go +++ b/internal/infra/models/execution.go @@ -25,8 +25,13 @@ const ( ) // PlanExecutionLog 记录整个计划的一次执行历史 + type PlanExecutionLog struct { - gorm.Model + ID uint `gorm:"primaryKey"` + CreatedAt time.Time `gorm:"primaryKey"` // 作为联合主键方便只查询热点数据 + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` + PlanID uint `gorm:"index"` Status ExecutionStatus StartedAt time.Time @@ -41,7 +46,11 @@ func (PlanExecutionLog) TableName() string { // TaskExecutionLog 记录单个任务的一次执行历史 type TaskExecutionLog struct { - gorm.Model + ID uint `gorm:"primaryKey"` + CreatedAt time.Time `gorm:"primaryKey"` // 作为联合主键方便只查询热点数据 + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` + PlanExecutionLogID uint `gorm:"index"` // 关联到某次计划执行 // TaskID 使用 int 类型以容纳特殊的负数ID,代表系统任务 @@ -100,7 +109,7 @@ type DeviceCommandLog struct { DeviceID uint `gorm:"not null;index" json:"device_id"` // SentAt 记录下行任务最初发送的时间。 - SentAt time.Time `gorm:"not null" json:"sent_at"` + SentAt time.Time `gorm:"primaryKey" json:"sent_at"` // AcknowledgedAt 记录设备确认收到下行消息的时间。 // 如果设备未确认,则为零值或 NULL。使用指针类型 *time.Time 允许 NULL 值。 @@ -136,7 +145,7 @@ type PendingCollection struct { FulfilledAt *time.Time // CreatedAt 是 GORM 的标准字段,记录请求创建时间。 - CreatedAt time.Time + CreatedAt time.Time `gorm:"primaryKey"` } // TableName 自定义 GORM 使用的数据库表名 @@ -175,7 +184,7 @@ type UserActionLog struct { Time time.Time `gorm:"primaryKey" json:"time"` // --- Who (谁) --- - UserID uint `gorm:"index" json:"user_id,omitempty"` + UserID uint `gorm:"primaryKey" json:"user_id,omitempty"` Username string `json:"username,omitempty"` // 操作发生时用户名的快照 // --- Where (何地) --- diff --git a/internal/infra/models/models.go b/internal/infra/models/models.go index 406be1f..8f21ca5 100644 --- a/internal/infra/models/models.go +++ b/internal/infra/models/models.go @@ -25,6 +25,7 @@ func GetAllModels() []interface{} { &PendingCollection{}, &AreaController{}, &DeviceTemplate{}, + &UserActionLog{}, } }