# 数据库配置指南 ## 1. 数据库设计文档 ### 核心表结构 | 表名 | 说明 | 主要字段 | |------|------|----------| | `candidates` | 候选人主表 | 基本信息、职业信息、联系方式、状态 | | `resumes` | 简历内容表 | 原始内容、解析内容、附件、版本 | | `jobs` | 职位信息表 | 职位名称、部门、薪资、要求 | | `evaluation_schemas` | 评价方案表 | 维度配置、权重、提示词模板 | | `evaluations` | 评价记录表 | 评分结果、AI分析、推荐意见 | | `notifications` | 通知记录表 | 通知渠道、状态、发送时间 | ### ER 关系图 ``` ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ candidates │────<│ resumes │ │ jobs │ │ (候选人) │ │ (简历) │ │ (职位) │ └──────┬──────┘ └─────────────┘ └──────┬──────┘ │ │ │ ┌─────────────┐ │ └────────>│ evaluations │<─────────────┘ │ (评价记录) │ └──────┬──────┘ │ ┌──────┴──────┐ │ notifications│ │ (通知记录) │ └─────────────┘ ``` ## 2. 数据库配置方法 ### 方法一:SQLite (开发测试) 无需额外配置,默认使用 SQLite: ```bash # 在项目根目录创建 .env 文件 echo "DB_URL=sqlite:///./hr_agent.db" > .env ``` ### 方法二:MySQL (生产环境) #### 2.1 创建数据库 ```sql CREATE DATABASE hr_agent CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'hr_agent'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON hr_agent.* TO 'hr_agent'@'%'; FLUSH PRIVILEGES; ``` #### 2.2 配置连接 ```bash # .env 文件 echo "DB_URL=mysql+pymysql://hr_agent:your_password@localhost:3306/hr_agent" > .env ``` #### 2.3 安装依赖 ```bash uv add pymysql cryptography ``` ### 方法三:PostgreSQL #### 3.1 创建数据库 ```sql CREATE DATABASE hr_agent ENCODING 'UTF8'; CREATE USER hr_agent WITH PASSWORD 'your_password'; GRANT ALL PRIVILEGES ON DATABASE hr_agent TO hr_agent; ``` #### 3.2 配置连接 ```bash # .env 文件 echo "DB_URL=postgresql+asyncpg://hr_agent:your_password@localhost:5432/hr_agent" > .env ``` #### 3.3 安装依赖 ```bash uv add asyncpg ``` ## 3. 初始化数据库 ### 3.1 执行 SQL 脚本 ```bash # MySQL mysql -u hr_agent -p hr_agent < migrations/001_init_schema.sql # SQLite sqlite3 hr_agent.db < migrations/001_init_schema.sql # PostgreSQL psql -U hr_agent -d hr_agent -f migrations/001_init_schema.sql ``` ### 3.2 验证初始化 ```sql -- 查看表结构 SHOW TABLES; -- 查看默认评价方案 SELECT * FROM evaluation_schemas; ``` ## 4. 环境变量配置 在项目根目录创建 `.env` 文件: ```env # 数据库配置 DB_URL=sqlite:///./hr_agent.db DB_ECHO=false # LLM 配置 LLM_PROVIDER=mock LLM_API_KEY= LLM_MODEL=gpt-4 # 爬虫配置 CRAWLER_BOSS_WT_TOKEN=your_boss_token # 通知配置 (可选) NOTIFY_WECHAT_WORK_WEBHOOK= NOTIFY_DINGTALK_WEBHOOK= ``` ## 5. 常用查询示例 ### 查询候选人列表 ```sql SELECT c.id, c.name, c.source, c.current_company, c.current_position, c.status, e.overall_score, e.recommendation FROM candidates c LEFT JOIN evaluations e ON c.id = e.candidate_id WHERE c.status = 'NEW' ORDER BY c.created_at DESC; ``` ### 查询高分候选人 ```sql SELECT c.name, c.current_company, e.overall_score, e.summary FROM candidates c JOIN evaluations e ON c.id = e.candidate_id WHERE e.overall_score >= 80 AND e.recommendation IN ('strong_recommend', 'recommend') ORDER BY e.overall_score DESC; ``` ### 统计各渠道候选人数量 ```sql SELECT source, COUNT(*) as total, SUM(CASE WHEN status = 'ANALYZED' THEN 1 ELSE 0 END) as analyzed FROM candidates GROUP BY source; ```