修改变量名, 规避order关键字

This commit is contained in:
2025-09-13 15:30:10 +08:00
parent 16bc3b08bc
commit cacb9a9b1d
2 changed files with 16 additions and 15 deletions

View File

@@ -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 使用的数据库表名

View File

@@ -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)
}