From 4a70c1e839af85fcf60a4a7b1a6f74145fac71c7 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Wed, 10 Sep 2025 15:00:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9B=B4=E6=96=B0=E8=AE=A1?= =?UTF-8?q?=E5=88=92=E6=97=B6=E4=BC=9A=E7=94=A8=E5=8E=9F=E8=AE=A1=E5=88=92?= =?UTF-8?q?ID=E5=88=9B=E5=BB=BA=E6=96=B0=E8=AE=A1=E5=88=92=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/storage/repository/feed.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/storage/repository/feed.go b/internal/storage/repository/feed.go index eae3559..32f95b4 100644 --- a/internal/storage/repository/feed.go +++ b/internal/storage/repository/feed.go @@ -79,6 +79,9 @@ func (f *feedPlanRepo) UpdateFeedingPlan(feedingPlan *model.FeedingPlan) error { return err } + // 清空所有ID,包括子计划和步骤的ID + f.clearAllIDs(feedingPlan) + // 再重新创建更新后的计划 if err := f.createFeedingPlanWithTx(tx, feedingPlan); err != nil { return err @@ -178,3 +181,22 @@ func (f *feedPlanRepo) createFeedingPlanWithTx(tx *gorm.DB, feedingPlan *model.F 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]) + } +}