120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
"""
|
|
投递记录路由:上报投递结果、查询历史、统计
|
|
"""
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import func
|
|
|
|
from ..auth import get_current_user
|
|
from ..database import get_db
|
|
from ..models import User, JobApplication
|
|
from ..schemas import ApplicationIn, ApplicationOut, Message, StatsOut
|
|
|
|
router = APIRouter(prefix="/api/jobs", tags=["投递记录"])
|
|
|
|
|
|
@router.post("/report", response_model=ApplicationOut)
|
|
def report_application(
|
|
req: ApplicationIn,
|
|
session_id: Optional[int] = Query(None, description="所属会话 ID"),
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
客户端上报一条投递/跳过记录。
|
|
同一用户 + 同一职位 ID 去重,已存在则更新。
|
|
"""
|
|
existing = db.query(JobApplication).filter(
|
|
JobApplication.user_id == current_user.id,
|
|
JobApplication.job_id == req.job_id,
|
|
).first()
|
|
|
|
if existing:
|
|
existing.apply_type = req.apply_type
|
|
existing.ai_score = req.ai_score
|
|
existing.ai_reason = req.ai_reason
|
|
existing.skip_reason = req.skip_reason
|
|
if session_id:
|
|
existing.session_id = session_id
|
|
record = existing
|
|
else:
|
|
record = JobApplication(
|
|
user_id=current_user.id,
|
|
session_id=session_id,
|
|
job_id=req.job_id,
|
|
job_title=req.job_title,
|
|
company=req.company,
|
|
salary=req.salary,
|
|
location=req.location,
|
|
apply_type=req.apply_type,
|
|
ai_score=req.ai_score,
|
|
ai_reason=req.ai_reason,
|
|
skip_reason=req.skip_reason,
|
|
)
|
|
db.add(record)
|
|
|
|
db.commit()
|
|
db.refresh(record)
|
|
return record
|
|
|
|
|
|
@router.get("/check/{job_id}", response_model=dict)
|
|
def check_applied(
|
|
job_id: str,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""检查某职位是否已投递过(用于客户端去重)"""
|
|
record = db.query(JobApplication).filter(
|
|
JobApplication.user_id == current_user.id,
|
|
JobApplication.job_id == job_id,
|
|
JobApplication.apply_type.in_(["chat", "deliver"]),
|
|
).first()
|
|
return {"job_id": job_id, "applied": record is not None}
|
|
|
|
|
|
@router.get("/history", response_model=list[ApplicationOut])
|
|
def list_applications(
|
|
apply_type: Optional[str] = Query(None, description="过滤投递类型"),
|
|
limit: int = Query(50, le=500),
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""获取投递历史(按时间倒序)"""
|
|
query = db.query(JobApplication).filter(JobApplication.user_id == current_user.id)
|
|
if apply_type:
|
|
query = query.filter(JobApplication.apply_type == apply_type)
|
|
return query.order_by(JobApplication.applied_at.desc()).limit(limit).all()
|
|
|
|
|
|
@router.get("/stats", response_model=StatsOut)
|
|
def get_stats(
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""获取投递统计"""
|
|
base = db.query(JobApplication).filter(JobApplication.user_id == current_user.id)
|
|
|
|
def count(**filters):
|
|
q = base
|
|
for k, v in filters.items():
|
|
q = q.filter(getattr(JobApplication, k) == v)
|
|
return q.count()
|
|
|
|
today_str = datetime.utcnow().strftime("%Y-%m-%d")
|
|
total_applied = base.filter(JobApplication.apply_type.in_(["chat", "deliver"])).count()
|
|
today_applied = base.filter(
|
|
JobApplication.apply_type.in_(["chat", "deliver"]),
|
|
func.date(JobApplication.applied_at) == today_str,
|
|
).count()
|
|
|
|
return StatsOut(
|
|
total_applied=total_applied,
|
|
total_chat=count(apply_type="chat"),
|
|
total_deliver=count(apply_type="deliver"),
|
|
total_skipped=count(apply_type="skipped"),
|
|
today_applied=today_applied,
|
|
)
|