From 03788341333aff870faa47dd23b0140067facacf Mon Sep 17 00:00:00 2001 From: superlishunqin <852326703@qq.com> Date: Fri, 16 May 2025 23:57:11 +0800 Subject: [PATCH] update_delete_user_logic --- app/services/user_service.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/app/services/user_service.py b/app/services/user_service.py index 3426dfc..f067423 100644 --- a/app/services/user_service.py +++ b/app/services/user_service.py @@ -3,7 +3,7 @@ from app.models.user import User, Role, db from sqlalchemy import or_ from datetime import datetime - +from sqlalchemy import text class UserService: @staticmethod @@ -111,27 +111,21 @@ class UserService: return False, "用户不存在" try: - # 检查是否有未归还的图书 + # 检查是否有未归还的图书 - 使用SQLAlchemy的text函数 active_borrows_count = db.session.execute( - "SELECT COUNT(*) FROM borrow_records WHERE user_id = :user_id AND return_date IS NULL", + text("SELECT COUNT(*) FROM borrow_records WHERE user_id = :user_id AND return_date IS NULL"), {"user_id": user_id} ).scalar() if active_borrows_count > 0: return False, f"无法删除:该用户还有 {active_borrows_count} 本未归还的图书" - # 删除用户相关的通知记录 + # 删除用户相关的通知记录 - 使用SQLAlchemy的text函数 db.session.execute( - "DELETE FROM notifications WHERE user_id = :user_id OR sender_id = :user_id", + text("DELETE FROM notifications WHERE user_id = :user_id OR sender_id = :user_id"), {"user_id": user_id} ) - # 删除用户相关的日志记录(可选,取决于是否需要保留审计记录) - # db.session.execute( - # "DELETE FROM logs WHERE user_id = :user_id", - # {"user_id": user_id} - # ) - # 物理删除用户 db.session.delete(user) db.session.commit()