From 7d671048b9a41265985188e4e4320193c403de08 Mon Sep 17 00:00:00 2001 From: root <13910913995@163.com> Date: Wed, 20 Aug 2025 17:38:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86session=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E7=9A=84=E6=9F=A5=E8=AF=A2=E5=92=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/database.py | 26 ++++++++++++++++++++++++++ backend/main.py | 28 +++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/backend/database.py b/backend/database.py index 6a9b5ddb..7bab2471 100644 --- a/backend/database.py +++ b/backend/database.py @@ -954,6 +954,32 @@ class DatabaseManager: logger.error(f'获取会话总数失败: {e}') return 0 + def delete_detection_session(self, session_id: str): + """删除检测会话及其相关的检测数据""" + conn = self.get_connection() + cursor = conn.cursor() + + try: + # 验证会话是否存在 + cursor.execute('SELECT COUNT(*) FROM detection_sessions WHERE id = ?', (session_id,)) + if cursor.fetchone()[0] == 0: + raise ValueError(f'检测会话不存在: {session_id}') + + # 先删除相关的检测数据 + cursor.execute('DELETE FROM detection_data WHERE session_id = ?', (session_id,)) + deleted_data_count = cursor.rowcount + + # 再删除检测会话 + cursor.execute('DELETE FROM detection_sessions WHERE id = ?', (session_id,)) + + conn.commit() + logger.info(f'删除检测会话: {session_id}, 同时删除了 {deleted_data_count} 条检测数据') + + except Exception as e: + conn.rollback() + logger.error(f'删除检测会话失败: {e}') + raise + def get_session_data(self, session_id: str) -> Optional[Dict]: """获取会话详细数据""" conn = self.get_connection() diff --git a/backend/main.py b/backend/main.py index bb9ffe5e..7fcbf172 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1212,11 +1212,20 @@ class AppServer: try: page = int(flask_request.args.get('page', 1)) size = int(flask_request.args.get('size', 10)) - patient_id = flask_request.args.get('patient_id') + patient_id = flask_request.args.get('patient_id') sessions = self.db_manager.get_detection_sessions(page, size, patient_id) total = self.db_manager.get_sessions_count(patient_id) + # 为每个会话补充最新的检测数据 + for session in sessions: + session_id = session.get('id') + if session_id: + latest_data = self.db_manager.get_latest_detection_data(session_id, 5) + session['latest_detection_data'] = latest_data + else: + session['latest_detection_data'] = [] + return jsonify({ 'success': True, 'data': { @@ -1300,6 +1309,23 @@ class AppServer: self.logger.error(f'删除检测数据失败: {e}') return jsonify({'success': False, 'error': str(e)}), 500 + @self.app.route('/api/detection/sessions/', methods=['DELETE']) + def delete_detection_session(session_id): + """删除检测会话及其相关的检测数据""" + try: + self.db_manager.delete_detection_session(session_id) + + return jsonify({ + 'success': True, + 'message': '检测会话删除成功' + }) + + except ValueError as e: + return jsonify({'success': False, 'error': str(e)}), 404 + except Exception as e: + self.logger.error(f'删除检测会话失败: {e}') + return jsonify({'success': False, 'error': str(e)}), 500 + # ==================== 错误处理 ==================== @self.app.errorhandler(404)