修复更新计划时会用原计划ID创建新计划的问题

This commit is contained in:
2025-09-10 15:00:29 +08:00
parent e75b3ee148
commit 4a70c1e839

View File

@@ -79,6 +79,9 @@ func (f *feedPlanRepo) UpdateFeedingPlan(feedingPlan *model.FeedingPlan) error {
return err return err
} }
// 清空所有ID包括子计划和步骤的ID
f.clearAllIDs(feedingPlan)
// 再重新创建更新后的计划 // 再重新创建更新后的计划
if err := f.createFeedingPlanWithTx(tx, feedingPlan); err != nil { if err := f.createFeedingPlanWithTx(tx, feedingPlan); err != nil {
return err return err
@@ -178,3 +181,22 @@ func (f *feedPlanRepo) createFeedingPlanWithTx(tx *gorm.DB, feedingPlan *model.F
return nil return nil
} }
// clearAllIDs 清空计划及其子计划和步骤的所有ID
func (f *feedPlanRepo) clearAllIDs(plan *model.FeedingPlan) {
// 清空计划ID
plan.ID = 0
// 清空所有步骤的ID和关联的计划ID
for i := range plan.Steps {
plan.Steps[i].ID = 0
plan.Steps[i].PlanID = 0
}
// 清空所有子计划的ID和关联的父计划ID并递归清空子计划的ID
for i := range plan.SubPlans {
plan.SubPlans[i].ID = 0
plan.SubPlans[i].ParentID = nil
f.clearAllIDs(&plan.SubPlans[i])
}
}