增加了session会话的查询和删除功能

This commit is contained in:
root 2025-08-20 17:38:38 +08:00
parent c6e88c5c7a
commit 7d671048b9
2 changed files with 53 additions and 1 deletions

View File

@ -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()

View File

@ -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/<session_id>', 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)