init
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""路由包"""
|
||||
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
投递记录路由:上报投递结果、查询历史、统计
|
||||
"""
|
||||
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,
|
||||
)
|
||||
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
客户端注册 / 登录状态 / 用户信息路由
|
||||
"""
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..auth import create_access_token, get_current_user
|
||||
from ..database import get_db
|
||||
from ..models import User
|
||||
from ..schemas import (
|
||||
RegisterRequest, RegisterResponse, UserInfo, LoginStatusUpdate, Message,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/auth", tags=["认证与用户"])
|
||||
|
||||
|
||||
@router.post("/register", response_model=RegisterResponse)
|
||||
def register(req: RegisterRequest, db: Session = Depends(get_db)):
|
||||
"""
|
||||
客户端注册/登录。
|
||||
以机器硬件指纹 client_code 作为唯一标识:
|
||||
- 已存在 → 直接返回 token(视为登录)
|
||||
- 不存在 → 创建新用户
|
||||
"""
|
||||
user = db.query(User).filter(User.client_code == req.client_code).first()
|
||||
is_new = user is None
|
||||
|
||||
if is_new:
|
||||
user = User(client_code=req.client_code, nickname=req.nickname)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
# 更新最后活跃时间
|
||||
user.last_seen_at = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
token = create_access_token(user.id, user.client_code)
|
||||
return RegisterResponse(
|
||||
user_id=user.id,
|
||||
token=token,
|
||||
client_code=user.client_code,
|
||||
is_new=is_new,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/me", response_model=UserInfo)
|
||||
def get_me(current_user: User = Depends(get_current_user)):
|
||||
"""获取当前用户信息"""
|
||||
return UserInfo(
|
||||
id=current_user.id,
|
||||
client_code=current_user.client_code,
|
||||
nickname=current_user.nickname,
|
||||
boss_account=current_user.boss_account,
|
||||
login_status=current_user.login_status,
|
||||
created_at=current_user.created_at,
|
||||
)
|
||||
|
||||
|
||||
@router.put("/login-status", response_model=Message)
|
||||
def update_login_status(
|
||||
req: LoginStatusUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""更新 Boss直聘 登录状态"""
|
||||
valid_status = {"not_login", "logging_in", "logged_in"}
|
||||
if req.login_status not in valid_status:
|
||||
raise HTTPException(status_code=400, detail=f"登录状态必须是 {valid_status} 之一")
|
||||
current_user.login_status = req.login_status
|
||||
if req.boss_account:
|
||||
current_user.boss_account = req.boss_account
|
||||
db.commit()
|
||||
return Message(msg=f"登录状态已更新为 {req.login_status}")
|
||||
@@ -0,0 +1,128 @@
|
||||
"""
|
||||
简历管理路由
|
||||
"""
|
||||
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, Resume
|
||||
from ..schemas import ResumeIn, ResumeOut, ResumeBatchIn, Message
|
||||
|
||||
router = APIRouter(prefix="/api/resumes", tags=["简历管理"])
|
||||
|
||||
|
||||
@router.get("", response_model=list[ResumeOut])
|
||||
def list_resumes(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""获取当前用户的所有简历"""
|
||||
resumes = db.query(Resume).filter(Resume.user_id == current_user.id).all()
|
||||
return resumes
|
||||
|
||||
|
||||
@router.get("/primary", response_model=ResumeOut)
|
||||
def get_primary_resume(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""获取当前用户的主简历"""
|
||||
resume = db.query(Resume).filter(
|
||||
Resume.user_id == current_user.id,
|
||||
Resume.is_primary == True,
|
||||
).first()
|
||||
if resume is None:
|
||||
raise HTTPException(status_code=404, detail="尚未设置主简历")
|
||||
return resume
|
||||
|
||||
|
||||
@router.post("/batch", response_model=list[ResumeOut])
|
||||
def upload_resumes_batch(
|
||||
req: ResumeBatchIn,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
批量上传抓取到的简历。
|
||||
以 boss_resume_id 去重:存在则更新,不存在则新增。
|
||||
第一份简历自动设为主简历(若当前没有主简历)。
|
||||
"""
|
||||
results = []
|
||||
has_primary = db.query(Resume).filter(
|
||||
Resume.user_id == current_user.id, Resume.is_primary == True
|
||||
).first() is not None
|
||||
|
||||
for i, item in enumerate(req.resumes):
|
||||
existing = None
|
||||
if item.boss_resume_id:
|
||||
existing = db.query(Resume).filter(
|
||||
Resume.user_id == current_user.id,
|
||||
Resume.boss_resume_id == item.boss_resume_id,
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
existing.title = item.title
|
||||
existing.position = item.position
|
||||
existing.content = item.content
|
||||
results.append(existing)
|
||||
else:
|
||||
resume = Resume(
|
||||
user_id=current_user.id,
|
||||
boss_resume_id=item.boss_resume_id,
|
||||
title=item.title,
|
||||
position=item.position,
|
||||
content=item.content,
|
||||
)
|
||||
# 若尚无主简历,将第一份设为主简历
|
||||
if not has_primary and i == 0:
|
||||
resume.is_primary = True
|
||||
has_primary = True
|
||||
db.add(resume)
|
||||
results.append(resume)
|
||||
|
||||
db.commit()
|
||||
for r in results:
|
||||
db.refresh(r)
|
||||
return results
|
||||
|
||||
|
||||
@router.post("/{resume_id}/set-primary", response_model=Message)
|
||||
def set_primary(
|
||||
resume_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""将指定简历设为主简历(同时取消其他主简历标记)"""
|
||||
target = db.query(Resume).filter(
|
||||
Resume.id == resume_id,
|
||||
Resume.user_id == current_user.id,
|
||||
).first()
|
||||
if target is None:
|
||||
raise HTTPException(status_code=404, detail="简历不存在")
|
||||
|
||||
# 取消当前主简历标记
|
||||
db.query(Resume).filter(
|
||||
Resume.user_id == current_user.id, Resume.is_primary == True
|
||||
).update({"is_primary": False})
|
||||
target.is_primary = True
|
||||
db.commit()
|
||||
return Message(msg=f"已将《{target.title}》设为主简历")
|
||||
|
||||
|
||||
@router.delete("/{resume_id}", response_model=Message)
|
||||
def delete_resume(
|
||||
resume_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""删除指定简历"""
|
||||
resume = db.query(Resume).filter(
|
||||
Resume.id == resume_id,
|
||||
Resume.user_id == current_user.id,
|
||||
).first()
|
||||
if resume is None:
|
||||
raise HTTPException(status_code=404, detail="简历不存在")
|
||||
db.delete(resume)
|
||||
db.commit()
|
||||
return Message(msg="简历已删除")
|
||||
@@ -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