Files
zyt/app/scripts/render_prescription_editor_visual.py
T
2026-08-14 14:37:30 +08:00

205 lines
7.1 KiB
Python

"""Render deterministic prescription-editor drawer references."""
from __future__ import annotations
import os
from pathlib import Path
from types import SimpleNamespace
from typing import Any
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
from PySide6.QtGui import QFont, QFontDatabase
from PySide6.QtWidgets import QApplication, QWidget
from doctor_workstation.core.permissions import PermissionSet
from doctor_workstation.ui import apply_theme
from doctor_workstation.ui.dialogs.prescription import (
PrescriptionDetailDialog,
PrescriptionEditorDialog,
)
from doctor_workstation.ui.dialogs.prescription_ai import PrescriptionAiReportDialog
class VisualRepository:
def list_medicines(self, **_filters: Any) -> dict[str, Any]:
return {"lists": [], "count": 0}
def list_prescription_template_ai_reports(self, template_id: int) -> dict[str, Any]:
report = {
"summary": "益气健脾为主,兼顾养阴安神,适合脾气不足、气阴两虚方向的复核参考。",
"possible_symptoms": ["乏力气短", "食少便溏", "睡眠不安"],
"main_indications": "气阴两虚、脾气不足所见的倦怠与纳差。",
"efficacy": ["益气健脾", "养阴生津", "宁心安神"],
"suitable_people": ["辨证属气阴两虚者", "需由医师结合四诊确认"],
"compatibility_analysis": "黄芪、党参、白术与茯苓协同补气健脾,麦冬、五味子兼顾养阴敛津,酸枣仁与远志用于宁心安神。",
"cautions": ["仍需核对过敏史与现用药", "症状变化时及时复诊"],
"disclaimer": "仅供专业人员辅助审方,不替代辨证、诊断和处方审核。",
}
return {
"prescription_id": template_id,
"can_refresh": True,
"can_edit": True,
"reports": [
{
"report_id": 21,
"model_key": "qwen",
"model_label": "千问",
"model_name": "qwen3.6-35b",
"generated_at": "2026-08-13 10:32:00",
"report": report,
"is_stale": False,
"is_edited": True,
},
{
"report_id": 22,
"model_key": "openai",
"model_label": "OpenAI",
"model_name": "gpt-5.6-sol",
"generated_at": "2026-08-13 10:32:00",
"report": report,
"is_stale": False,
"is_edited": False,
},
],
}
def _seed() -> dict[str, Any]:
return {
"diagnosis_id": 501,
"id": 2501,
"appointment_id": 2501,
"prescription_no": "RX202608130021",
"patient_name": "林晓岚",
"phone": "13800138000",
"gender": 0,
"age": 34,
"visit_no": "1K00002501",
"prescription_date": "2026-08-11",
"tongue": "舌淡红、苔薄白",
"pulse": "面色少华",
"pulse_condition": "脉细",
"clinical_diagnosis": "气阴两虚,脾气不足",
"prescription_type": "浓缩水丸",
"dosage_amount": 3,
"dosage_unit": "g",
"dosage_bag_count": 2,
"dose_count": 7,
"dose_unit": "剂",
"usage_days": 7,
"times_per_day": 2,
"usage_instruction": "早晚各一次,温水送服",
"usage_time": "饭后",
"usage_way": "温水送服",
"dietary_taboo": ["生冷食物", "浓茶"],
"usage_notes": "服药期间规律记录空腹及餐后血糖。",
"aux_usage": {
"dosage_amount": 2,
"dosage_bag_count": 1,
"times_per_day": 1,
"usage_days": 7,
"prescription_name": "宁心辅方",
},
"doctor_name": "陈医生",
"audit_status": 1,
"create_time": "2026-08-13 10:26:00",
"herbs": [
{"medicine_id": 11, "name": "黄芪", "dosage": 15, "formula_type": "主方"},
{"medicine_id": 12, "name": "党参", "dosage": 12, "formula_type": "主方"},
{"medicine_id": 13, "name": "白术", "dosage": 10, "formula_type": "主方"},
{"medicine_id": 14, "name": "茯苓", "dosage": 12, "formula_type": "主方"},
{"medicine_id": 15, "name": "麦冬", "dosage": 9, "formula_type": "主方"},
{"medicine_id": 16, "name": "五味子", "dosage": 6, "formula_type": "主方"},
{"medicine_id": 17, "name": "酸枣仁", "dosage": 12, "formula_type": "辅方"},
{"medicine_id": 18, "name": "远志", "dosage": 6, "formula_type": "辅方"},
],
}
def _make_editor(host: QWidget) -> PrescriptionEditorDialog:
editor = PrescriptionEditorDialog(
VisualRepository(),
_seed(),
mode="add",
current_user=SimpleNamespace(id=1, name="陈医生"),
parent=host,
)
return editor
def _settle(app: QApplication, rounds: int = 8) -> None:
for _ in range(rounds):
app.processEvents()
def render() -> list[Path]:
app = QApplication.instance() or QApplication([])
apply_theme(app)
font_path = Path("C:/Windows/Fonts/msyh.ttc")
if font_path.is_file():
font_id = QFontDatabase.addApplicationFont(str(font_path))
families = QFontDatabase.applicationFontFamilies(font_id)
if families:
app.setFont(QFont(families[0], 9))
output = Path(__file__).resolve().parents[1] / "artifacts" / "subwindow_exact"
output.mkdir(parents=True, exist_ok=True)
rendered: list[Path] = []
host = QWidget()
host.resize(1440, 900)
host.show()
editor = _make_editor(host)
editor.show()
_settle(app)
editor.body_scroll.verticalScrollBar().setValue(0)
_settle(app, 4)
path = output / "prescription_editor_860x900.png"
if not editor.grab().save(str(path), "PNG"):
raise RuntimeError(f"failed to save {path}")
rendered.append(path)
editor.close()
host.close()
detail = PrescriptionDetailDialog(
_seed(),
can_open_diagnosis=True,
can_open_orders=True,
)
detail.show()
_settle(app, 10)
path = output / "prescription_detail_920x780.png"
if not detail.grab().save(str(path), "PNG"):
raise RuntimeError(f"failed to save {path}")
rendered.append(path)
detail.close()
_settle(app, 2)
ai_dialog = PrescriptionAiReportDialog(
VisualRepository(),
PermissionSet(["*", "tcm.prescriptionLibrary/editAiReport"]),
)
ai_dialog.open_for(
{
"id": 2501,
"prescription_name": "益气养阴安神方",
"formula_type": "主方",
"herbs": _seed()["herbs"],
}
)
ai_dialog.show()
_settle(app, 16)
path = output / "prescription_ai_report_920x760.png"
if not ai_dialog.grab().save(str(path), "PNG"):
raise RuntimeError(f"failed to save {path}")
rendered.append(path)
ai_dialog.close()
_settle(app, 2)
return rendered
if __name__ == "__main__":
for rendered_path in render():
print(rendered_path)