分页优化
This commit is contained in:
@@ -146,6 +146,61 @@ def list_applications(
|
||||
return query.order_by(JobApplication.applied_at.desc()).limit(limit).all()
|
||||
|
||||
|
||||
@router.get("/page", response_model=dict)
|
||||
def page_applications(
|
||||
page: int = Query(1, ge=1, description="页码(1 起)"),
|
||||
page_size: int = Query(20, ge=1, le=100, description="每页条数"),
|
||||
apply_type: Optional[str] = Query(None, description="投递结果:chat/deliver/skipped"),
|
||||
start: Optional[str] = Query(None, description="起始日期(北京时间,YYYY-MM-DD,含)"),
|
||||
end: Optional[str] = Query(None, description="结束日期(北京时间,YYYY-MM-DD,含)"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""投递记录分页筛选(北京时间范围 + 投递结果),时间倒序"""
|
||||
query = db.query(JobApplication).filter(JobApplication.user_id == current_user.id)
|
||||
|
||||
# 北京时间日期区间 → UTC 区间(北京 00:00 == UTC 前一日 16:00)
|
||||
try:
|
||||
if start:
|
||||
d = datetime.strptime(start, "%Y-%m-%d")
|
||||
query = query.filter(JobApplication.applied_at >= d - timedelta(hours=8))
|
||||
if end:
|
||||
d = datetime.strptime(end, "%Y-%m-%d") + timedelta(days=1)
|
||||
query = query.filter(JobApplication.applied_at < d - timedelta(hours=8))
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=422, detail="日期格式应为 YYYY-MM-DD")
|
||||
|
||||
if apply_type:
|
||||
query = query.filter(JobApplication.apply_type == apply_type)
|
||||
|
||||
# 筛选范围内统计(不受分页影响)
|
||||
total = query.count()
|
||||
applied = query.filter(JobApplication.apply_type.in_(["chat", "deliver"])).count()
|
||||
skipped = query.filter(JobApplication.apply_type == "skipped").count()
|
||||
|
||||
rows = query.order_by(JobApplication.applied_at.desc()) \
|
||||
.offset((page - 1) * page_size).limit(page_size).all()
|
||||
items = [{
|
||||
"job_title": r.job_title,
|
||||
"company": r.company,
|
||||
"salary": r.salary,
|
||||
"location": r.location,
|
||||
"apply_type": r.apply_type,
|
||||
"ai_score": r.ai_score,
|
||||
"ai_reason": r.ai_reason,
|
||||
"skip_reason": r.skip_reason,
|
||||
"greeting": r.greeting,
|
||||
# JD 全文截断,供客户端悬停摘要展示,控制分页响应体积
|
||||
"jd_text": (r.jd_text or "")[:500],
|
||||
"applied_at": (r.applied_at + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S"),
|
||||
} for r in rows]
|
||||
return {
|
||||
"items": items, "total": total, "applied": applied, "skipped": skipped,
|
||||
"page": page, "page_size": page_size,
|
||||
"pages": (total + page_size - 1) // page_size if page_size else 1,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/stats", response_model=StatsOut)
|
||||
def get_stats(
|
||||
current_user: User = Depends(get_current_user),
|
||||
|
||||
Reference in New Issue
Block a user