79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
from common.db.base import execute, fetchone
|
|
|
|
|
|
async def select_hospital(hospital_id: str) -> dict | None:
|
|
return await fetchone(
|
|
"SELECT hospital_id, owner_user_id, hospital_name, hospital_name_en,"
|
|
" brn, road_address, site_address, status, created_at, updated_at"
|
|
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
|
(hospital_id,),
|
|
)
|
|
|
|
|
|
async def update_hospital_status(hospital_id: str, status: str) -> None:
|
|
await execute(
|
|
"UPDATE hospital_baseinfo SET status = %s WHERE hospital_id = %s",
|
|
(status, hospital_id),
|
|
)
|
|
|
|
|
|
async def _insert_hospital_history(hospital_id: str, analysis_run_id: str | None) -> None:
|
|
row = await fetchone(
|
|
"SELECT owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, status"
|
|
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
|
(hospital_id,),
|
|
)
|
|
if not row:
|
|
return
|
|
await execute(
|
|
"INSERT INTO hospital_history"
|
|
" (hospital_id, owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, status, analysis_run_id)"
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
|
(
|
|
hospital_id,
|
|
row["owner_user_id"],
|
|
row["hospital_name"],
|
|
row["hospital_name_en"],
|
|
row["brn"],
|
|
row["road_address"],
|
|
row["site_address"],
|
|
row["status"],
|
|
analysis_run_id,
|
|
),
|
|
)
|
|
|
|
|
|
async def insert_hospital(
|
|
hospital_id: str,
|
|
name: str,
|
|
name_en: str | None = None,
|
|
road_address: str | None = None,
|
|
site_address: str | None = None,
|
|
owner_user_id: int = 0,
|
|
brn: str = "",
|
|
) -> dict:
|
|
await execute(
|
|
"INSERT INTO hospital_baseinfo"
|
|
" (hospital_id, hospital_name, hospital_name_en, road_address, site_address, status, owner_user_id, brn)"
|
|
" VALUES (%s, %s, %s, %s, %s, 'done', %s, %s)",
|
|
(hospital_id, name, name_en, road_address, site_address, owner_user_id, brn),
|
|
)
|
|
await _insert_hospital_history(hospital_id, analysis_run_id=None)
|
|
return await fetchone(
|
|
"SELECT created_at FROM hospital_baseinfo WHERE hospital_id = %s",
|
|
(hospital_id,),
|
|
)
|
|
|
|
|
|
async def update_hospital(hospital_id: str, data: dict, analysis_run_id: str | None = None) -> None:
|
|
await execute(
|
|
"UPDATE hospital_baseinfo"
|
|
" SET status = 'done',"
|
|
" hospital_name = COALESCE(%s, hospital_name),"
|
|
" hospital_name_en = COALESCE(%s, hospital_name_en),"
|
|
" road_address = COALESCE(%s, road_address)"
|
|
" WHERE hospital_id = %s",
|
|
(data.get("clinicName"), data.get("clinicNameEn"), data.get("address"), hospital_id),
|
|
)
|
|
await _insert_hospital_history(hospital_id, analysis_run_id)
|