const express = require('express'); const router = express.Router(); const { pool } = require('../config/db'); // 获取所有面试 router.get('/', async (req, res) => { try { const [interviews] = await pool.query(` SELECT id, title, description, questions, duration, created_at AS createdAt FROM interviews ORDER BY created_at DESC `); // 解析questions字段 interviews.forEach(interview => { interview.questions = JSON.parse(interview.questions); }); res.json(interviews); } catch (error) { console.error('Get interviews error:', error); res.status(500).json({ error: 'Server error' }); } }); // 创建新面试 router.post('/', async (req, res) => { try { const { title, description, questions, duration } = req.body; const id = require('crypto').randomUUID(); await pool.query( `INSERT INTO interviews ( id, title, description, questions, duration, created_at ) VALUES (?, ?, ?, ?, ?, ?)`, [ id, title, description, JSON.stringify(questions || []), duration, new Date() ] ); const [newInterview] = await pool.query( 'SELECT * FROM interviews WHERE id = ?', [id] ); res.status(201).json(formatInterview(newInterview[0])); } catch (error) { console.error('Create interview error:', error); res.status(500).json({ error: 'Server error' }); } }); // 安排候选人面试 router.post('/schedule', async (req, res) => { try { const { candidateId, interviewId, interviewTime, interviewerId, notes } = req.body; const id = require('crypto').randomUUID(); await pool.query( `INSERT INTO candidate_interviews ( id, candidate_id, interview_id, interview_time, interviewer_id, notes, result ) VALUES (?, ?, ?, ?, ?, ?, 'scheduled')`, [ id, candidateId, interviewId, new Date(interviewTime), interviewerId, notes ] ); // 更新候选人状态 await pool.query( `UPDATE candidates SET status = 'interviewed', reviewed_at = ? WHERE id = ?`, [new Date(), candidateId] ); // 记录状态变更历史 await pool.query( `INSERT INTO candidate_status_history ( id, candidate_id, old_status, new_status, changed_by, change_time ) VALUES (?, ?, ?, ?, ?, ?)`, [ require('crypto').randomUUID(), candidateId, 'passed', 'interviewed', req.user?.id || 'system', new Date() ] ); const [scheduledInterview] = await pool.query( `SELECT ci.*, i.title AS interview_title FROM candidate_interviews ci JOIN interviews i ON ci.interview_id = i.id WHERE ci.id = ?`, [id] ); res.status(201).json(formatCandidateInterview(scheduledInterview[0])); } catch (error) { console.error('Schedule interview error:', error); res.status(500).json({ error: 'Server error' }); } }); // 更新面试结果 router.put('/:id/result', async (req, res) => { try { const { id } = req.params; const { result, notes } = req.body; await pool.query( `UPDATE candidate_interviews SET result = ?, notes = ? WHERE id = ?`, [result, notes, id] ); const [updatedInterview] = await pool.query( `SELECT ci.*, i.title AS interview_title FROM candidate_interviews ci JOIN interviews i ON ci.interview_id = i.id WHERE ci.id = ?`, [id] ); res.json(formatCandidateInterview(updatedInterview[0])); } catch (error) { console.error('Update interview result error:', error); res.status(500).json({ error: 'Server error' }); } }); // 格式化面试数据 function formatInterview(interview) { return { ...interview, questions: JSON.parse(interview.questions), createdAt: interview.created_at }; } // 格式化候选人面试数据 function formatCandidateInterview(candidateInterview) { return { ...candidateInterview, interviewTime: candidateInterview.interview_time, interviewerId: candidateInterview.interviewer_id, interviewTitle: candidateInterview.interview_title }; } module.exports = router;