Files
pig-farm-controller/internal/model/feeding.go
2025-09-09 20:42:19 +08:00

175 lines
5.5 KiB
Go

package model
import (
"time"
"gorm.io/gorm"
)
// FeedingPlanType 喂料计划类型枚举
type FeedingPlanType string
const (
// FeedingPlanTypeManual 手动触发
FeedingPlanTypeManual FeedingPlanType = "manual"
// FeedingPlanTypeAutoOnce 自动触发一次
FeedingPlanTypeAutoOnce FeedingPlanType = "auto_once"
// FeedingPlanTypeAutoScheduled 定时自动触发
FeedingPlanTypeAutoScheduled FeedingPlanType = "auto_scheduled"
)
// FeedingPlan 喂料计划主表
type FeedingPlan struct {
// ID 计划ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// Name 计划名称
Name string `gorm:"not null;column:name" json:"name"`
// Description 计划描述
Description string `gorm:"column:description" json:"description"`
// Type 计划类型(手动触发/自动触发一次/定时自动触发)
Type FeedingPlanType `gorm:"not null;column:type" json:"type"`
// Enabled 是否启用
Enabled bool `gorm:"not null;default:true;column:enabled" json:"enabled"`
// ScheduleCron 定时任务表达式, 当类型为 FeedingPlanTypeAutoOnce 时只会触发第一次
ScheduleCron *string `gorm:"column:schedule_cron" json:"schedule_cron,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
// Steps 计划步骤列表
Steps []FeedingPlanStep `gorm:"foreignKey:PlanID" json:"-"`
}
// TableName 指定FeedingPlan模型对应的数据库表名
func (FeedingPlan) TableName() string {
return "feeding_plans"
}
// FeedingPlanStep 喂料计划步骤表,表示计划中的每个设备动作
type FeedingPlanStep struct {
// ID 步骤ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// PlanID 关联的计划ID
PlanID uint `gorm:"not null;column:plan_id;index" json:"plan_id"`
// StepOrder 步骤顺序
StepOrder int `gorm:"not null;column:step_order" json:"step_order"`
// DeviceID 关联的设备ID
DeviceID uint `gorm:"not null;column:device_id;index" json:"device_id"`
// TargetValue 目标值(达到该值后停止工作切换到下一个设备)
TargetValue float64 `gorm:"not null;column:target_value" json:"target_value"`
// Action 动作(如:打开设备)
Action string `gorm:"not null;column:action" json:"action"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}
// TableName 指定FeedingPlanStep模型对应的数据库表名
func (FeedingPlanStep) TableName() string {
return "feeding_plan_steps"
}
// FeedingExecution 喂料执行记录表
type FeedingExecution struct {
// ID 执行记录ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// PlanID 关联的计划ID
PlanID uint `gorm:"not null;column:plan_id;index" json:"plan_id"`
// TriggerType 触发类型(手动/自动)
TriggerType FeedingPlanType `gorm:"not null;column:trigger_type" json:"trigger_type"`
// Status 执行状态(进行中/已完成/已取消/失败)
Status string `gorm:"not null;column:status" json:"status"`
// StartedAt 开始执行时间
StartedAt *time.Time `gorm:"column:started_at" json:"started_at,omitempty"`
// FinishedAt 完成时间
FinishedAt *time.Time `gorm:"column:finished_at" json:"finished_at,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
// Steps 执行步骤详情
Steps []FeedingExecutionStep `gorm:"foreignKey:ExecutionID" json:"-"`
}
// TableName 指定FeedingExecution模型对应的数据库表名
func (FeedingExecution) TableName() string {
return "feeding_executions"
}
// FeedingExecutionStep 喂料执行步骤详情表
type FeedingExecutionStep struct {
// ID 执行步骤ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// ExecutionID 关联的执行记录ID
ExecutionID uint `gorm:"not null;column:execution_id;index" json:"execution_id"`
// StepID 关联的计划步骤ID
StepID uint `gorm:"not null;column:step_id;index" json:"step_id"`
// DeviceID 关联的设备ID
DeviceID uint `gorm:"not null;column:device_id;index" json:"device_id"`
// TargetValue 目标值
TargetValue float64 `gorm:"not null;column:target_value" json:"target_value"`
// ActualValue 实际值
ActualValue *float64 `gorm:"column:actual_value" json:"actual_value,omitempty"`
// Status 步骤状态(待执行/执行中/已完成/失败)
Status string `gorm:"not null;column:status" json:"status"`
// StartedAt 开始执行时间
StartedAt *time.Time `gorm:"column:started_at" json:"started_at,omitempty"`
// FinishedAt 完成时间
FinishedAt *time.Time `gorm:"column:finished_at" json:"finished_at,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}
// TableName 指定FeedingExecutionStep模型对应的数据库表名
func (FeedingExecutionStep) TableName() string {
return "feeding_execution_steps"
}