From 62a7fdb3ce6e7161f7202ffec64c11edfb1719d8 Mon Sep 17 00:00:00 2001 From: WangSL Date: Sun, 26 May 2024 23:53:58 +0800 Subject: [PATCH] bugfix check_out.py --- api/check_out.py | 73 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 7 deletions(-) diff --git a/api/check_out.py b/api/check_out.py index 857b656..d06b468 100644 --- a/api/check_out.py +++ b/api/check_out.py @@ -6,18 +6,19 @@ """ from typing import Union -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends from pydantic import BaseModel -from sqlalchemy import text, select +from sqlalchemy import text from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session from starlette import status from auth import get_current_active_user, roles_required from auth_schema import User -from config import RoomStatus, ClientStatus, BabyNurseWorkStatus, ClientTag +from config import RoomStatus, BabyNurseWorkStatus, ClientTag from database import get_db from datetime import datetime + standby = BabyNurseWorkStatus.standby.value router = APIRouter() @@ -25,12 +26,17 @@ free = RoomStatus.Free.value -class CheckOutRecv(BaseModel): +class TerminateRecv(BaseModel): room_number: str recently_used: Union[str, None] double_check_password: str +class CheckOutRecv(BaseModel): + room_number: str + recently_used: Union[str, None] + + class CheckOutResp(BaseModel): status: str details: str @@ -71,7 +77,59 @@ def update_room_and_client(db, check_out_recv): ) try: - db.execute(update_client_sql, {"status": f'{client_status}-{ClientTag.terminate.value}', "room_number": check_out_recv.room_number}) + db.execute(update_client_sql, {"status": f'{client_status}-{ClientTag.terminate.value}', + "room_number": check_out_recv.room_number}) + db.execute(update_room_sql, + {"room_number": check_out_recv.room_number, "recently_used": datetime.now().strftime('%Y-%m-%d'), + "status": free}) + db.execute(update_baby_nurse_work_status_sql, {"room_number": check_out_recv.room_number, "standby": standby}) + db.commit() + except ValueError as ve: + # 如果没有找到对应的client_id,可以在这里处理异常 + db.rollback() + raise ve + except SQLAlchemyError as e: + db.rollback() + raise Exception(str(e)) + + +def client_check_out(db, check_out_recv): + # 把房间状态改为空闲,房间客户改为空,最近使用改为今天 + update_room_sql = text( + """ + UPDATE room SET status = :status, client_id = NULL, recently_used = :recently_used + WHERE room_number = :room_number + """ + ) + + get_client_status_sql = text( + """ + SELECT status FROM client WHERE id = (SELECT client_id FROM room WHERE room_number = :room_number) + """ + ) + client_status = db.execute(get_client_status_sql, {"room_number": check_out_recv.room_number}) + client_status = client_status.mappings().first() + client_status = client_status["status"].split("-")[0] + # 把客户状态改为已离店,客户房间改为空 + update_client_sql = text( + """ + UPDATE client + SET status = :status, + room = NULL + WHERE id = (SELECT client_id FROM room WHERE room_number = :room_number) + """ + ) + + # 把宝妈护工状态改为待命 + update_baby_nurse_work_status_sql = text( + """ + UPDATE baby_nurse SET work_status = :standby WHERE baby_nurse_id = (SELECT assigned_baby_nurse FROM client WHERE room = :room_number) + """ + ) + + try: + db.execute(update_client_sql, {"status": f'{client_status}-{ClientTag.checked_out.value}', + "room_number": check_out_recv.room_number}) db.execute(update_room_sql, {"room_number": check_out_recv.room_number, "recently_used": datetime.now().strftime('%Y-%m-%d'), "status": free}) @@ -88,7 +146,7 @@ def update_room_and_client(db, check_out_recv): @router.post("/terminate") @roles_required("admin") -async def terminate_service(check_out_recv: CheckOutRecv, current_user: User = Depends(get_current_active_user), +async def terminate_service(check_out_recv: TerminateRecv, current_user: User = Depends(get_current_active_user), db: Session = Depends(get_db)): if current_user.double_check_password != check_out_recv.double_check_password: return CheckOutResp(status=status.HTTP_401_UNAUTHORIZED, details="密码错误") @@ -99,9 +157,10 @@ async def terminate_service(check_out_recv: CheckOutRecv, current_user: User = D except Exception as e: return CheckOutResp(status=status.HTTP_500_INTERNAL_SERVER_ERROR, details=str(e)) + @router.post("/check_out") @roles_required("admin") -async def terminate_service(check_out_recv: CheckOutRecv, db: Session = Depends(get_db)): +async def check_out_service(check_out_recv: CheckOutRecv, db: Session = Depends(get_db)): try: update_room_and_client(db, check_out_recv) return CheckOutResp(status=status.HTTP_200_OK, details="退房成功")