From 150b315a7b237b91372508fe4414847324fbbc26 Mon Sep 17 00:00:00 2001 From: yangjianbo Date: Sun, 18 Jan 2026 16:46:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=BD=AF=E5=88=A0=E9=99=A4):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=BD=AF=E5=88=A0=E9=99=A4=E9=92=A9=E5=AD=90=E7=9A=84?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=B0=83=E7=94=A8=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/ent/schema/mixins/soft_delete.go | 36 +++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/backend/ent/schema/mixins/soft_delete.go b/backend/ent/schema/mixins/soft_delete.go index 00ef77a6..749d0ffb 100644 --- a/backend/ent/schema/mixins/soft_delete.go +++ b/backend/ent/schema/mixins/soft_delete.go @@ -5,6 +5,7 @@ package mixins import ( "context" "fmt" + "reflect" "time" "entgo.io/ent" @@ -122,7 +123,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook { mx.SetOp(ent.OpUpdate) // 设置删除时间为当前时间 mx.SetDeletedAt(time.Now()) - return next.Mutate(ctx, m) + return mutateWithClient(ctx, m, next) }) }, } @@ -135,3 +136,36 @@ func (d SoftDeleteMixin) applyPredicate(w interface{ WhereP(...func(*sql.Selecto sql.FieldIsNull(d.Fields()[0].Descriptor().Name), ) } + +func mutateWithClient(ctx context.Context, m ent.Mutation, fallback ent.Mutator) (ent.Value, error) { + clientMethod := reflect.ValueOf(m).MethodByName("Client") + if !clientMethod.IsValid() || clientMethod.Type().NumIn() != 0 || clientMethod.Type().NumOut() != 1 { + return nil, fmt.Errorf("soft delete: mutation client method not found for %T", m) + } + client := clientMethod.Call(nil)[0] + mutateMethod := client.MethodByName("Mutate") + if !mutateMethod.IsValid() { + return nil, fmt.Errorf("soft delete: mutation client missing Mutate for %T", m) + } + if mutateMethod.Type().NumIn() != 2 || mutateMethod.Type().NumOut() != 2 { + return nil, fmt.Errorf("soft delete: mutation client signature mismatch for %T", m) + } + + results := mutateMethod.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(m)}) + value := results[0].Interface() + var err error + if !results[1].IsNil() { + err = results[1].Interface().(error) + } + if err != nil { + return nil, err + } + if value == nil { + return nil, fmt.Errorf("soft delete: mutation client returned nil for %T", m) + } + v, ok := value.(ent.Value) + if !ok { + return nil, fmt.Errorf("soft delete: unexpected value type %T for %T", value, m) + } + return v, nil +}