117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
"""
|
|
投递会话路由:开始 / 结束投递、上报统计
|
|
"""
|
|
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 ..online import require_online_user
|
|
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(require_online_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""开始一次投递会话(前置校验:RPA 必须在线)"""
|
|
# 若已有进行中的会话,先结束它
|
|
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=req.city,
|
|
salary_min=req.salary_min,
|
|
salary_max=req.salary_max,
|
|
prompt=req.prompt,
|
|
)
|
|
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),
|
|
):
|
|
"""
|
|
结束指定会话。
|
|
特例:start 需在线前置校验,但 stop 是安全清理操作,故意豁免在线拦截——
|
|
否则离线前遗留的 running 僵尸会话将永远无法关闭。
|
|
"""
|
|
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(require_online_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""增量上报投递/跳过数量(前置校验:RPA 必须在线)"""
|
|
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()
|