import json from common.db.base import execute, fetchone async def insert_run( analysis_run_id: str, hospital_id: str, owner_user_id: int, ) -> str: await execute( "INSERT INTO analysis_runs (analysis_run_id, hospital_id, owner_user_id) VALUES (%s, %s, %s)", (analysis_run_id, hospital_id, owner_user_id), ) return analysis_run_id async def select_run(analysis_run_id: str) -> dict | None: return await fetchone( "SELECT analysis_run_id, hospital_id, owner_user_id, status, created_at, updated_at" " FROM analysis_runs WHERE analysis_run_id = %s", (analysis_run_id,), ) async def select_run_status(analysis_run_id: str) -> str | None: row = await fetchone( "SELECT status FROM analysis_runs WHERE analysis_run_id = %s", (analysis_run_id,), ) return row["status"] if row else None async def update_run_status(analysis_run_id: str, status: str) -> None: await execute( "UPDATE analysis_runs SET status = %s WHERE analysis_run_id = %s", (status, analysis_run_id), ) async def update_run_report(analysis_run_id: str, data: dict) -> None: await execute( "UPDATE analysis_runs SET report_data = %s WHERE analysis_run_id = %s", (json.dumps(data, ensure_ascii=False), analysis_run_id), ) async def update_run_plan(analysis_run_id: str, data: dict) -> None: await execute( "UPDATE analysis_runs SET plan_data = %s WHERE analysis_run_id = %s", (json.dumps(data, ensure_ascii=False), analysis_run_id), ) async def select_run_with_clinic(analysis_run_id: str) -> dict | None: return await fetchone( "SELECT ar.report_data, ar.plan_data, ar.created_at," " h.hospital_name, h.hospital_name_en," " rs.url AS target_url" " FROM analysis_runs ar" " JOIN hospital_baseinfo h ON ar.hospital_id = h.hospital_id" " LEFT JOIN remote_source rs ON rs.hospital_id = h.hospital_id AND rs.source_type = 'mainpage'" " WHERE ar.analysis_run_id = %s", (analysis_run_id,), )