diff --git a/backend/ent/schema/mixins/soft_delete.go b/backend/ent/schema/mixins/soft_delete.go index 03a3899d..9f98a422 100644 --- a/backend/ent/schema/mixins/soft_delete.go +++ b/backend/ent/schema/mixins/soft_delete.go @@ -112,9 +112,6 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook { // 类型断言,获取 mutation 的扩展接口 mx, ok := m.(interface { SetOp(ent.Op) - Client() interface { - Mutate(context.Context, ent.Mutation) (ent.Value, error) - } SetDeletedAt(time.Time) WhereP(...func(*sql.Selector)) }) @@ -127,7 +124,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook { mx.SetOp(ent.OpUpdate) // 设置删除时间为当前时间 mx.SetDeletedAt(time.Now()) - return mx.Client().Mutate(ctx, m) + return next.Mutate(ctx, m) }) }, } diff --git a/backend/internal/repository/user_repo.go b/backend/internal/repository/user_repo.go index c81225ff..3ffb00f3 100644 --- a/backend/internal/repository/user_repo.go +++ b/backend/internal/repository/user_repo.go @@ -169,8 +169,14 @@ func (r *userRepository) Update(ctx context.Context, userIn *service.User) error } func (r *userRepository) Delete(ctx context.Context, id int64) error { - _, err := r.client.User.Delete().Where(dbuser.IDEQ(id)).Exec(ctx) - return err + affected, err := r.client.User.Delete().Where(dbuser.IDEQ(id)).Exec(ctx) + if err != nil { + return translatePersistenceError(err, service.ErrUserNotFound, nil) + } + if affected == 0 { + return service.ErrUserNotFound + } + return nil } func (r *userRepository) List(ctx context.Context, params pagination.PaginationParams) ([]service.User, *pagination.PaginationResult, error) { diff --git a/backend/internal/service/admin_service.go b/backend/internal/service/admin_service.go index 9ffd342d..4be09810 100644 --- a/backend/internal/service/admin_service.go +++ b/backend/internal/service/admin_service.go @@ -366,7 +366,11 @@ func (s *adminServiceImpl) DeleteUser(ctx context.Context, id int64) error { if user.Role == "admin" { return errors.New("cannot delete admin user") } - return s.userRepo.Delete(ctx, id) + if err := s.userRepo.Delete(ctx, id); err != nil { + log.Printf("delete user failed: user_id=%d err=%v", id, err) + return err + } + return nil } func (s *adminServiceImpl) UpdateUserBalance(ctx context.Context, userID int64, balance float64, operation string, notes string) (*User, error) {