update_delete_user_logic
This commit is contained in:
parent
6246b9730f
commit
773fd5a9d4
@ -105,16 +105,37 @@ class UserService:
|
||||
|
||||
@staticmethod
|
||||
def delete_user(user_id):
|
||||
"""删除用户 (软删除,将状态设为-1)"""
|
||||
"""删除用户 (物理删除)"""
|
||||
user = User.query.get(user_id)
|
||||
if not user:
|
||||
return False, "用户不存在"
|
||||
|
||||
try:
|
||||
user.status = -1 # 软删除,设置状态为-1
|
||||
user.updated_at = datetime.now()
|
||||
# 检查是否有未归还的图书
|
||||
active_borrows_count = db.session.execute(
|
||||
"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} 本未归还的图书"
|
||||
|
||||
# 删除用户相关的通知记录
|
||||
db.session.execute(
|
||||
"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()
|
||||
return True, "用户已删除"
|
||||
return True, "用户已永久删除"
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False, f"删除失败: {str(e)}"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user