修改变量名, 规避order关键字
This commit is contained in:
@@ -58,10 +58,10 @@ func (Plan) TableName() string {
|
|||||||
type SubPlan struct {
|
type SubPlan struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
ParentPlanID uint `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID
|
ParentPlanID uint `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID
|
||||||
ChildPlanID uint `gorm:"not null;index" json:"child_plan_id"` // 子计划的ID (它本身也是一个 Plan)
|
ChildPlanID uint `gorm:"not null;index" json:"child_plan_id"` // 子计划的ID (它本身也是一个 Plan)
|
||||||
Order int `gorm:"not null" json:"order"` // 在父计划中的执行顺序
|
ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在父计划中的执行顺序
|
||||||
ChildPlan *Plan `gorm:"-" json:"child_plan"` // 完整子计划数据,仅内存中
|
ChildPlan *Plan `gorm:"-" json:"child_plan"` // 完整子计划数据,仅内存中
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName 自定义 GORM 使用的数据库表名
|
// TableName 自定义 GORM 使用的数据库表名
|
||||||
@@ -73,12 +73,12 @@ func (SubPlan) TableName() string {
|
|||||||
type Task struct {
|
type Task struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
PlanID uint `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID
|
PlanID uint `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID
|
||||||
Name string `gorm:"not null" json:"name"`
|
Name string `gorm:"not null" json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Order int `gorm:"not null" json:"order"` // 在计划中的执行顺序
|
ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在计划中的执行顺序
|
||||||
Type TaskType `gorm:"not null" json:"type"` // 任务的类型,对应 task 包中的具体动作
|
Type TaskType `gorm:"not null" json:"type"` // 任务的类型,对应 task 包中的具体动作
|
||||||
Parameters datatypes.JSON `json:"parameters"` // 任务特定参数的JSON (例如: 设备ID, 值)
|
Parameters datatypes.JSON `json:"parameters"` // 任务特定参数的JSON (例如: 设备ID, 值)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName 自定义 GORM 使用的数据库表名
|
// TableName 自定义 GORM 使用的数据库表名
|
||||||
|
|||||||
@@ -67,15 +67,13 @@ func (r *gormPlanRepository) GetPlanByID(id uint) (*models.Plan, error) {
|
|||||||
case models.PlanContentTypeSubPlans:
|
case models.PlanContentTypeSubPlans:
|
||||||
// 加载子计划引用
|
// 加载子计划引用
|
||||||
var subPlans []models.SubPlan
|
var subPlans []models.SubPlan
|
||||||
result = r.db.Where("parent_plan_id = ?", plan.ID).Order("order").Find(&subPlans)
|
result = r.db.Where("parent_plan_id = ?", plan.ID).Order("execution_order").Find(&subPlans)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// 递归加载每个子计划的完整信息
|
// 递归加载每个子计划的完整信息
|
||||||
for i := range subPlans {
|
for i := range subPlans {
|
||||||
// 递归调用确保子计划数据可以被获取
|
|
||||||
// 注意:SubPlan结构体中没有ChildPlan字段,所以这里只是递归获取确保数据完整性
|
|
||||||
childPlan, err := r.GetPlanByID(subPlans[i].ChildPlanID)
|
childPlan, err := r.GetPlanByID(subPlans[i].ChildPlanID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -87,8 +85,11 @@ func (r *gormPlanRepository) GetPlanByID(id uint) (*models.Plan, error) {
|
|||||||
case models.PlanContentTypeTasks:
|
case models.PlanContentTypeTasks:
|
||||||
// 加载任务
|
// 加载任务
|
||||||
result = r.db.Preload("Tasks", func(taskDB *gorm.DB) *gorm.DB {
|
result = r.db.Preload("Tasks", func(taskDB *gorm.DB) *gorm.DB {
|
||||||
return taskDB.Order("order")
|
return taskDB.Order("execution_order")
|
||||||
}).Find(&plan)
|
}).First(&plan, id)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("未知的计划内容类型: %v; 计划ID: %v", plan.ContentType, plan.ID)
|
return nil, fmt.Errorf("未知的计划内容类型: %v; 计划ID: %v", plan.ContentType, plan.ID)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user