102 lines
5.0 KiB
Python
102 lines
5.0 KiB
Python
from datetime import datetime, timezone
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException
|
||
|
||
from app.middleware.auth import get_current_user
|
||
from app.models.checkup import CheckupPlan, CheckupItem
|
||
from app.models.user import User
|
||
|
||
router = APIRouter()
|
||
|
||
# 标准产检计划
|
||
STANDARD_CHECKUPS = [
|
||
CheckupItem(title="首次产检", week=6, items=["B超确认宫内孕", "血常规", "尿常规", "肝功能", "肾功能"], preparations=["空腹", "带身份证和医保卡"], notes="确认怀孕和预产期", importance="high"),
|
||
CheckupItem(title="NT检查", week=12, items=["NT超声筛查", "血清学筛查", "血压", "体重"], preparations=["不需要空腹", "需憋尿"], notes="筛查唐氏综合征风险", importance="high"),
|
||
CheckupItem(title="唐氏筛查", week=16, items=["唐氏筛查抽血", "血压", "体重", "宫高腹围"], preparations=["空腹抽血"], notes="评估胎儿染色体异常风险", importance="high"),
|
||
CheckupItem(title="大排畸检查", week=22, items=["四维彩超", "血压", "体重", "宫高腹围"], preparations=["不需要空腹", "提前预约"], notes="全面筛查胎儿结构畸形", importance="high"),
|
||
CheckupItem(title="糖耐量检查", week=24, items=["OGTT糖耐量试验", "血压", "体重"], preparations=["空腹8-14小时", "检查前3天正常饮食"], notes="筛查妊娠期糖尿病", importance="high"),
|
||
CheckupItem(title="常规产检", week=28, items=["血常规", "尿常规", "血压", "体重", "胎心监护"], preparations=["空腹"], notes="进入孕晚期,增加产检频率", importance="medium"),
|
||
CheckupItem(title="常规产检", week=30, items=["B超评估胎儿发育", "血压", "体重", "胎心监护"], preparations=[], notes="评估胎儿生长发育", importance="medium"),
|
||
CheckupItem(title="常规产检", week=32, items=["血常规", "尿常规", "血压", "体重", "胎心监护", "B超"], preparations=["空腹"], notes="观察胎位", importance="medium"),
|
||
CheckupItem(title="GBS筛查", week=36, items=["B族链球菌筛查", "血压", "体重", "胎心监护", "内检"], preparations=[], notes="预防新生儿感染", importance="high"),
|
||
CheckupItem(title="每周产检", week=37, items=["胎心监护", "血压", "体重", "宫高腹围"], preparations=[], notes="每周一次,准备待产", importance="medium"),
|
||
CheckupItem(title="每周产检", week=38, items=["胎心监护", "B超评估", "血压", "体重"], preparations=[], notes="评估分娩时机", importance="medium"),
|
||
CheckupItem(title="每周产检", week=39, items=["胎心监护", "血压", "体重", "宫颈评估"], preparations=[], notes="关注产兆", importance="high"),
|
||
CheckupItem(title="预产期产检", week=40, items=["胎心监护", "B超", "血压", "体重", "催产评估"], preparations=[], notes="超过预产期需密切监护", importance="high"),
|
||
]
|
||
|
||
|
||
@router.get("/plan")
|
||
async def get_checkup_plan(user: User = Depends(get_current_user)):
|
||
"""获取产检计划"""
|
||
plan = await CheckupPlan.find_one(CheckupPlan.user_id == user.id)
|
||
|
||
if not plan:
|
||
# Generate default plan
|
||
plan = CheckupPlan(user_id=user.id, checkups=STANDARD_CHECKUPS)
|
||
await plan.insert()
|
||
|
||
# Mark completed checkups based on current week
|
||
current_week = user.pregnancy_week or 0
|
||
checkups = []
|
||
for c in plan.checkups:
|
||
status = c.status
|
||
if c.week < current_week and status == "upcoming":
|
||
status = "missed"
|
||
checkups.append({
|
||
**c.model_dump(),
|
||
"status": status,
|
||
})
|
||
|
||
# Find next checkup
|
||
next_checkup = None
|
||
for c in checkups:
|
||
if c["status"] == "upcoming":
|
||
next_checkup = c
|
||
break
|
||
|
||
return {
|
||
"checkups": checkups,
|
||
"next_checkup": next_checkup,
|
||
"current_week": current_week,
|
||
}
|
||
|
||
|
||
@router.put("/plan/{week}/complete")
|
||
async def complete_checkup(week: int, user: User = Depends(get_current_user)):
|
||
"""标记产检已完成"""
|
||
plan = await CheckupPlan.find_one(CheckupPlan.user_id == user.id)
|
||
if not plan:
|
||
raise HTTPException(status_code=404, detail="产检计划不存在")
|
||
|
||
for checkup in plan.checkups:
|
||
if checkup.week == week:
|
||
checkup.status = "completed"
|
||
break
|
||
|
||
plan.updated_at = datetime.now(timezone.utc)
|
||
await plan.save()
|
||
return {"message": "已标记完成"}
|
||
|
||
|
||
async def link_report_to_checkup(user_id, report_id: str, pregnancy_week: int):
|
||
"""将报告自动关联到最近的产检(±2周窗口),并标记该产检为已完成"""
|
||
plan = await CheckupPlan.find_one(CheckupPlan.user_id == user_id)
|
||
if not plan:
|
||
return
|
||
|
||
best = None
|
||
best_diff = 999
|
||
for c in plan.checkups:
|
||
diff = abs(c.week - pregnancy_week)
|
||
if diff <= 2 and diff < best_diff:
|
||
best = c
|
||
best_diff = diff
|
||
|
||
if best:
|
||
if report_id not in best.report_ids:
|
||
best.report_ids.append(report_id)
|
||
best.status = "completed"
|
||
plan.updated_at = datetime.now(timezone.utc)
|
||
await plan.save()
|