增加任务设备关联表
This commit is contained in:
		@@ -77,6 +77,9 @@ type Device struct {
 | 
			
		||||
	// Properties 用于存储特定类型设备的独有属性,采用JSON格式。
 | 
			
		||||
	// 建议在应用层为不同子类型的设备定义专用的属性结构体,以保证数据一致性。
 | 
			
		||||
	Properties datatypes.JSON `json:"properties"`
 | 
			
		||||
 | 
			
		||||
	// Tasks 是与此设备关联的任务列表,通过 DeviceTask 关联表实现多对多关系
 | 
			
		||||
	Tasks []Task `gorm:"many2many:device_tasks;" json:"tasks"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SelfCheck 对 Device 的关键字段和属性进行业务逻辑验证。
 | 
			
		||||
 
 | 
			
		||||
@@ -178,6 +178,9 @@ type Task struct {
 | 
			
		||||
	ExecutionOrder int            `gorm:"not null" json:"execution_order"` // 在计划中的执行顺序
 | 
			
		||||
	Type           TaskType       `gorm:"not null" json:"type"`            // 任务的类型,对应 task 包中的具体动作
 | 
			
		||||
	Parameters     datatypes.JSON `json:"parameters"`                      // 任务特定参数的JSON (例如: 设备ID, 值)
 | 
			
		||||
 | 
			
		||||
	// Devices 是与此任务关联的设备列表,通过 DeviceTask 关联表实现多对多关系
 | 
			
		||||
	Devices []Device `gorm:"many2many:device_tasks;" json:"devices"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TableName 自定义 GORM 使用的数据库表名
 | 
			
		||||
@@ -197,3 +200,18 @@ func (t Task) ParseParameters(v interface{}) error {
 | 
			
		||||
	}
 | 
			
		||||
	return json.Unmarshal(t.Parameters, v)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeviceTask 是设备和任务之间的关联模型,表示一个设备可以执行多个任务,一个任务可以被多个设备执行。
 | 
			
		||||
type DeviceTask struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	DeviceID uint `gorm:"not null;index"` // 设备ID
 | 
			
		||||
	TaskID   uint `gorm:"not null;index"` // 任务ID
 | 
			
		||||
 | 
			
		||||
	// 可选:如果需要存储关联的额外信息,可以在这里添加字段,例如:
 | 
			
		||||
	// Configuration datatypes.JSON `json:"configuration"` // 任务在特定设备上的配置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TableName 自定义 GORM 使用的数据库表名
 | 
			
		||||
func (DeviceTask) TableName() string {
 | 
			
		||||
	return "device_tasks"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user