From cacb9a9b1d2e56d855e150b9b97cf0433e70a16f Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 13 Sep 2025 15:30:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F=E5=90=8D,?= =?UTF-8?q?=20=E8=A7=84=E9=81=BForder=E5=85=B3=E9=94=AE=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/infra/models/plan.go | 20 ++++++++++---------- internal/infra/repository/plan_repository.go | 11 ++++++----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/infra/models/plan.go b/internal/infra/models/plan.go index 0b5445d..0190122 100644 --- a/internal/infra/models/plan.go +++ b/internal/infra/models/plan.go @@ -58,10 +58,10 @@ func (Plan) TableName() string { type SubPlan struct { gorm.Model - ParentPlanID uint `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID - ChildPlanID uint `gorm:"not null;index" json:"child_plan_id"` // 子计划的ID (它本身也是一个 Plan) - Order int `gorm:"not null" json:"order"` // 在父计划中的执行顺序 - ChildPlan *Plan `gorm:"-" json:"child_plan"` // 完整子计划数据,仅内存中 + ParentPlanID uint `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID + ChildPlanID uint `gorm:"not null;index" json:"child_plan_id"` // 子计划的ID (它本身也是一个 Plan) + ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在父计划中的执行顺序 + ChildPlan *Plan `gorm:"-" json:"child_plan"` // 完整子计划数据,仅内存中 } // TableName 自定义 GORM 使用的数据库表名 @@ -73,12 +73,12 @@ func (SubPlan) TableName() string { type Task struct { gorm.Model - PlanID uint `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID - Name string `gorm:"not null" json:"name"` - Description string `json:"description"` - Order int `gorm:"not null" json:"order"` // 在计划中的执行顺序 - Type TaskType `gorm:"not null" json:"type"` // 任务的类型,对应 task 包中的具体动作 - Parameters datatypes.JSON `json:"parameters"` // 任务特定参数的JSON (例如: 设备ID, 值) + PlanID uint `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID + Name string `gorm:"not null" json:"name"` + Description string `json:"description"` + ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在计划中的执行顺序 + Type TaskType `gorm:"not null" json:"type"` // 任务的类型,对应 task 包中的具体动作 + Parameters datatypes.JSON `json:"parameters"` // 任务特定参数的JSON (例如: 设备ID, 值) } // TableName 自定义 GORM 使用的数据库表名 diff --git a/internal/infra/repository/plan_repository.go b/internal/infra/repository/plan_repository.go index 83f7107..5c882af 100644 --- a/internal/infra/repository/plan_repository.go +++ b/internal/infra/repository/plan_repository.go @@ -67,15 +67,13 @@ func (r *gormPlanRepository) GetPlanByID(id uint) (*models.Plan, error) { case models.PlanContentTypeSubPlans: // 加载子计划引用 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 { return nil, result.Error } // 递归加载每个子计划的完整信息 for i := range subPlans { - // 递归调用确保子计划数据可以被获取 - // 注意:SubPlan结构体中没有ChildPlan字段,所以这里只是递归获取确保数据完整性 childPlan, err := r.GetPlanByID(subPlans[i].ChildPlanID) if err != nil { return nil, err @@ -87,8 +85,11 @@ func (r *gormPlanRepository) GetPlanByID(id uint) (*models.Plan, error) { case models.PlanContentTypeTasks: // 加载任务 result = r.db.Preload("Tasks", func(taskDB *gorm.DB) *gorm.DB { - return taskDB.Order("order") - }).Find(&plan) + return taskDB.Order("execution_order") + }).First(&plan, id) + if result.Error != nil { + return nil, result.Error + } default: return nil, fmt.Errorf("未知的计划内容类型: %v; 计划ID: %v", plan.ContentType, plan.ID) }