97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
![]() |
import json
|
|||
|
from flask import Blueprint, request, jsonify, g
|
|||
|
from ..models import Session, Report
|
|||
|
from src.main import Consultation
|
|||
|
from src.utils import read_basic_info_json
|
|||
|
from . import globals
|
|||
|
|
|||
|
from .. import db
|
|||
|
|
|||
|
report_routes = Blueprint('report', __name__)
|
|||
|
|
|||
|
|
|||
|
def validate_session_id(sessionId):
|
|||
|
"""验证sessionId是否提供且存在于Session表中"""
|
|||
|
if not sessionId:
|
|||
|
return jsonify({"error": "sessionId is required"}), 400
|
|||
|
session = Session.query.filter_by(id=sessionId).first()
|
|||
|
if not session:
|
|||
|
return jsonify({"error": "Session not found"}), 404
|
|||
|
return None
|
|||
|
|
|||
|
|
|||
|
@report_routes.route('/get-report/<int:sessionId>', methods=['GET'])
|
|||
|
def get_report(sessionId):
|
|||
|
validation_result = validate_session_id(sessionId)
|
|||
|
if validation_result:
|
|||
|
return validation_result
|
|||
|
reports = Report.query.filter_by(sessionId=sessionId).all()
|
|||
|
return jsonify(reports=[report.to_dict() for report in reports]), 200
|
|||
|
|
|||
|
|
|||
|
@report_routes.route('/add-report/<int:sessionId>', methods=['POST'])
|
|||
|
def add_report(sessionId):
|
|||
|
"""添加报告到数据库"""
|
|||
|
# 获取 sessionId 和上传的文件
|
|||
|
|
|||
|
if not globals.consultation: # 检查是否存在consultation对象
|
|||
|
return jsonify({"error": "No consultation object found"}), 404
|
|||
|
if not globals.consultation.save_result: # 检查是否存在save_result方法或属性(根据实际情况调整):
|
|||
|
return jsonify({"error": "No save_result method or property found"}), 404
|
|||
|
try:
|
|||
|
# 验证 sessionId 是否存在
|
|||
|
validation_result = validate_session_id(sessionId)
|
|||
|
if validation_result:
|
|||
|
return validation_result
|
|||
|
|
|||
|
report = globals.consultation.save_result(sessionId)
|
|||
|
|
|||
|
# 创建新的 Report 记录
|
|||
|
new_report = Report(
|
|||
|
sessionId=sessionId,
|
|||
|
report=report # 假设 report_data 是 Report 表中的一个字段
|
|||
|
)
|
|||
|
|
|||
|
# 添加到数据库
|
|||
|
db.session.add(new_report)
|
|||
|
db.session.commit()
|
|||
|
|
|||
|
format_report = globals.consultation.format_report(sessionId)
|
|||
|
print(format_report,type(format_report))
|
|||
|
|
|||
|
return jsonify({
|
|||
|
"message": "Report added successfully",
|
|||
|
"data": format_report
|
|||
|
}), 201
|
|||
|
except json.JSONDecodeError:
|
|||
|
return jsonify({"error": "Invalid JSON in report file"}), 400
|
|||
|
except Exception as e:
|
|||
|
db.session.rollback()
|
|||
|
return jsonify({"error": f"Failed to add report: {str(e)}"}), 500
|
|||
|
|
|||
|
|
|||
|
@report_routes.route('/upload-report/<int:sessionId>', methods=['POST'])
|
|||
|
def upload_json(sessionId):
|
|||
|
# 检查请求中是否包含文件
|
|||
|
print(sessionId)
|
|||
|
if 'report' not in request.files:
|
|||
|
return jsonify({"error": "No file part"}), 400
|
|||
|
|
|||
|
report = request.files['report']
|
|||
|
# 检查文件是否为空
|
|||
|
if report.filename == '':
|
|||
|
return jsonify({"error": "No selected file"}), 400
|
|||
|
|
|||
|
# 检查文件是否为 JSON 文件
|
|||
|
if report and report.filename.endswith('.json'):
|
|||
|
try:
|
|||
|
# 读取文件内容并解析为 JSON
|
|||
|
data = json.load(report)
|
|||
|
globals.consultation.init_session(sessionId, case_data=data)
|
|||
|
print(globals.consultation.session_map[sessionId].patient.basic_info)
|
|||
|
return jsonify({"message": "File uploaded successfully", "data": data}), 200
|
|||
|
except json.JSONDecodeError:
|
|||
|
return jsonify({"error": "Invalid JSON file"}), 400
|
|||
|
else:
|
|||
|
return jsonify({"error": "File must be a JSON file"}), 400
|