Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
150b315a
Commit
150b315a
authored
Jan 18, 2026
by
yangjianbo
Browse files
fix(软删除): 修复软删除钩子的客户端调用逻辑,确保正确处理变更
parent
fb839ae6
Changes
1
Hide whitespace changes
Inline
Side-by-side
backend/ent/schema/mixins/soft_delete.go
View file @
150b315a
...
@@ -5,6 +5,7 @@ package mixins
...
@@ -5,6 +5,7 @@ package mixins
import
(
import
(
"context"
"context"
"fmt"
"fmt"
"reflect"
"time"
"time"
"entgo.io/ent"
"entgo.io/ent"
...
@@ -122,7 +123,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
...
@@ -122,7 +123,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
mx
.
SetOp
(
ent
.
OpUpdate
)
mx
.
SetOp
(
ent
.
OpUpdate
)
// 设置删除时间为当前时间
// 设置删除时间为当前时间
mx
.
SetDeletedAt
(
time
.
Now
())
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
...
@@ -135,3 +136,36 @@ func (d SoftDeleteMixin) applyPredicate(w interface{ WhereP(...func(*sql.Selecto
sql
.
FieldIsNull
(
d
.
Fields
()[
0
]
.
Descriptor
()
.
Name
),
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment