init
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
"""
|
||||
投递会话路由:开始 / 结束投递、上报统计
|
||||
"""
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..database import get_db
|
||||
from ..models import User, ApplySession
|
||||
from ..schemas import SessionStartIn, SessionOut, SessionStatsIn, Message
|
||||
|
||||
router = APIRouter(prefix="/api/sessions", tags=["投递会话"])
|
||||
|
||||
|
||||
@router.post("/start", response_model=SessionOut)
|
||||
def start_session(
|
||||
req: SessionStartIn,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""开始一次投递会话"""
|
||||
# 若已有进行中的会话,先结束它
|
||||
running = db.query(ApplySession).filter(
|
||||
ApplySession.user_id == current_user.id,
|
||||
ApplySession.status == "running",
|
||||
).first()
|
||||
if running:
|
||||
running.status = "stopped"
|
||||
running.ended_at = datetime.utcnow()
|
||||
|
||||
session = ApplySession(
|
||||
user_id=current_user.id,
|
||||
status="running",
|
||||
keywords=req.keywords,
|
||||
city_code=req.city_code,
|
||||
salary_min=req.salary_min,
|
||||
salary_max=req.salary_max,
|
||||
)
|
||||
db.add(session)
|
||||
db.commit()
|
||||
db.refresh(session)
|
||||
return session
|
||||
|
||||
|
||||
@router.get("/current", response_model=SessionOut)
|
||||
def get_current_session(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""获取当前进行中的会话"""
|
||||
session = db.query(ApplySession).filter(
|
||||
ApplySession.user_id == current_user.id,
|
||||
ApplySession.status == "running",
|
||||
).order_by(ApplySession.started_at.desc()).first()
|
||||
if session is None:
|
||||
raise HTTPException(status_code=404, detail="当前没有进行中的会话")
|
||||
return session
|
||||
|
||||
|
||||
@router.post("/{session_id}/stop", response_model=SessionOut)
|
||||
def stop_session(
|
||||
session_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""结束指定会话"""
|
||||
session = db.query(ApplySession).filter(
|
||||
ApplySession.id == session_id,
|
||||
ApplySession.user_id == current_user.id,
|
||||
).first()
|
||||
if session is None:
|
||||
raise HTTPException(status_code=404, detail="会话不存在")
|
||||
session.status = "stopped"
|
||||
session.ended_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(session)
|
||||
return session
|
||||
|
||||
|
||||
@router.post("/{session_id}/stats", response_model=SessionOut)
|
||||
def update_session_stats(
|
||||
session_id: int,
|
||||
req: SessionStatsIn,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""增量上报投递/跳过数量"""
|
||||
session = db.query(ApplySession).filter(
|
||||
ApplySession.id == session_id,
|
||||
ApplySession.user_id == current_user.id,
|
||||
).first()
|
||||
if session is None:
|
||||
raise HTTPException(status_code=404, detail="会话不存在")
|
||||
session.applied_count += req.applied_delta
|
||||
session.skipped_count += req.skipped_delta
|
||||
db.commit()
|
||||
db.refresh(session)
|
||||
return session
|
||||
|
||||
|
||||
@router.get("/history", response_model=list[SessionOut])
|
||||
def list_sessions(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""获取历史会话列表"""
|
||||
return db.query(ApplySession).filter(
|
||||
ApplySession.user_id == current_user.id
|
||||
).order_by(ApplySession.started_at.desc()).limit(50).all()
|
||||
Reference in New Issue
Block a user