""" SQLAlchemy ORM 模型定义 所有业务表均带 user_id 外键,实现用户数据隔离 """ from datetime import datetime from sqlalchemy import ( Column, Integer, BigInteger, String, Text, Boolean, DateTime, Float, ForeignKey, Index, UniqueConstraint, ) from sqlalchemy.orm import relationship from .database import Base def utcnow() -> datetime: return datetime.utcnow() class User(Base): """ 系统用户 以用户名(username)+ 密码哈希作为身份,与机器/指纹浏览器解耦 """ __tablename__ = "users" id = Column(Integer, primary_key=True, autoincrement=True) # 登录用户名(唯一) username = Column(String(64), unique=True, nullable=False, index=True) # 密码加盐哈希(PBKDF2,格式:盐$摘要),不存明文 password_hash = Column(String(255), nullable=False) # 客户端显示昵称(可选) nickname = Column(String(64), nullable=True) # 最近一次登录的客户端设备指纹(仅作审计/展示,非身份标识) client_code = Column(String(128), nullable=True) # 账号是否启用 is_active = Column(Boolean, default=True, nullable=False) # Boss直聘 账号标识(登录成功后回填,用于区分同一 Boss 账号) boss_account = Column(String(128), nullable=True) # 登录状态:not_login / logging_in / logged_in login_status = Column(String(20), default="not_login", nullable=False) created_at = Column(DateTime, default=utcnow, nullable=False) last_seen_at = Column(DateTime, default=utcnow, onupdate=utcnow) # 关联 resumes = relationship("Resume", back_populates="user", cascade="all, delete-orphan") applications = relationship("JobApplication", back_populates="user", cascade="all, delete-orphan") sessions = relationship("ApplySession", back_populates="user", cascade="all, delete-orphan") class Resume(Base): """ 简历 一个用户可有多份简历,其中一份标记为主简历 """ __tablename__ = "resumes" id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) # Boss直聘 简历 ID boss_resume_id = Column(String(64), nullable=True) # 简历标题 title = Column(String(128), nullable=False) # 求职意向岗位 position = Column(String(128), nullable=True) # 简历全文(结构化或纯文本) content = Column(Text, nullable=True) # 是否为主简历 is_primary = Column(Boolean, default=False, nullable=False) created_at = Column(DateTime, default=utcnow, nullable=False) updated_at = Column(DateTime, default=utcnow, onupdate=utcnow) user = relationship("User", back_populates="resumes") class ApplySession(Base): """ 投递会话 记录一次"开始投递 → 结束投递"的完整过程 """ __tablename__ = "apply_sessions" id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) # 会话状态:running / stopped / finished status = Column(String(20), default="running", nullable=False) # 搜索关键词(逗号分隔) keywords = Column(String(512), nullable=True) # 城市代码 city_code = Column(String(20), nullable=True) # 薪资范围 salary_min = Column(Integer, nullable=True) salary_max = Column(Integer, nullable=True) # 本次会话投递数量统计 applied_count = Column(Integer, default=0) skipped_count = Column(Integer, default=0) started_at = Column(DateTime, default=utcnow, nullable=False) ended_at = Column(DateTime, nullable=True) user = relationship("User", back_populates="sessions") applications = relationship("JobApplication", back_populates="session", cascade="all, delete-orphan") class JobApplication(Base): """ 投递记录 """ __tablename__ = "job_applications" __table_args__ = ( # 同一用户不重复投递同一职位 UniqueConstraint("user_id", "job_id", name="uq_user_job"), Index("idx_user_session", "user_id", "session_id"), ) id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False) session_id = Column(Integer, ForeignKey("apply_sessions.id", ondelete="SET NULL"), nullable=True) # Boss直聘 职位 ID job_id = Column(String(64), nullable=False, index=True) job_title = Column(String(128), nullable=True) company = Column(String(128), nullable=True) salary = Column(String(32), nullable=True) location = Column(String(64), nullable=True) # 投递方式:chat=立即沟通 / deliver=投递简历 / skipped=跳过 apply_type = Column(String(20), nullable=False) # AI 匹配评分(-1 表示未评分) ai_score = Column(Integer, default=-1) ai_reason = Column(String(255), nullable=True) # 跳过原因 skip_reason = Column(String(255), nullable=True) applied_at = Column(DateTime, default=utcnow, nullable=False) user = relationship("User", back_populates="applications") session = relationship("ApplySession", back_populates="applications")