271 lines
7.2 KiB
JavaScript
271 lines
7.2 KiB
JavaScript
|
|
/**
|
||
|
|
* 面试进度控制器
|
||
|
|
* 基于high.html的最新数据结构
|
||
|
|
*/
|
||
|
|
|
||
|
|
const db = require('../../config/database');
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取我的面试进度列表
|
||
|
|
* GET /api/transition-progress
|
||
|
|
* 需要登录
|
||
|
|
*/
|
||
|
|
exports.getMyProgress = async (req, res) => {
|
||
|
|
try {
|
||
|
|
const userId = req.user.id;
|
||
|
|
|
||
|
|
const [records] = await db.query(
|
||
|
|
`SELECT
|
||
|
|
id,
|
||
|
|
company,
|
||
|
|
job,
|
||
|
|
status,
|
||
|
|
status_type,
|
||
|
|
created_at,
|
||
|
|
updated_at
|
||
|
|
FROM transition_progress
|
||
|
|
WHERE user_id = ?
|
||
|
|
ORDER BY created_at DESC`,
|
||
|
|
[userId]
|
||
|
|
);
|
||
|
|
|
||
|
|
return res.json({
|
||
|
|
success: true,
|
||
|
|
data: records
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('获取面试进度失败:', error);
|
||
|
|
return res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: '获取面试进度失败',
|
||
|
|
error: error.message
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 创建面试进度记录
|
||
|
|
* POST /api/transition-progress
|
||
|
|
* 需要登录
|
||
|
|
*/
|
||
|
|
exports.create = async (req, res) => {
|
||
|
|
try {
|
||
|
|
const userId = req.user.id;
|
||
|
|
const {
|
||
|
|
company,
|
||
|
|
job,
|
||
|
|
status,
|
||
|
|
status_type
|
||
|
|
} = req.body;
|
||
|
|
|
||
|
|
// 验证必填字段
|
||
|
|
if (!company || !job || !status) {
|
||
|
|
return res.status(400).json({
|
||
|
|
success: false,
|
||
|
|
message: '缺少必填字段 (company, job, status)'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 验证status_type
|
||
|
|
if (status_type && !['offer', 'hr', 'ing', 'pending'].includes(status_type)) {
|
||
|
|
return res.status(400).json({
|
||
|
|
success: false,
|
||
|
|
message: 'status_type 必须是 offer, hr, ing 或 pending'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const [result] = await db.query(
|
||
|
|
`INSERT INTO transition_progress
|
||
|
|
(user_id, company, job, status, status_type)
|
||
|
|
VALUES (?, ?, ?, ?, ?)`,
|
||
|
|
[
|
||
|
|
userId,
|
||
|
|
company,
|
||
|
|
job,
|
||
|
|
status,
|
||
|
|
status_type || 'pending'
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return res.status(201).json({
|
||
|
|
success: true,
|
||
|
|
message: '面试进度记录创建成功',
|
||
|
|
data: { id: result.insertId }
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('创建面试进度失败:', error);
|
||
|
|
return res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: '创建面试进度失败',
|
||
|
|
error: error.message
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 更新面试进度
|
||
|
|
* PUT /api/transition-progress/:id
|
||
|
|
* 需要登录
|
||
|
|
*/
|
||
|
|
exports.update = async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { id } = req.params;
|
||
|
|
const userId = req.user.id;
|
||
|
|
const updateData = req.body;
|
||
|
|
|
||
|
|
// 检查记录是否存在且属于当前用户
|
||
|
|
const [existing] = await db.query(
|
||
|
|
'SELECT id FROM transition_progress WHERE id = ? AND user_id = ?',
|
||
|
|
[id, userId]
|
||
|
|
);
|
||
|
|
|
||
|
|
if (existing.length === 0) {
|
||
|
|
return res.status(404).json({
|
||
|
|
success: false,
|
||
|
|
message: '面试进度记录不存在或无权访问'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const updates = [];
|
||
|
|
const params = [];
|
||
|
|
|
||
|
|
const allowedFields = ['company', 'job', 'status', 'status_type'];
|
||
|
|
|
||
|
|
for (const field of allowedFields) {
|
||
|
|
if (updateData[field] !== undefined) {
|
||
|
|
// 验证status_type
|
||
|
|
if (field === 'status_type' && !['offer', 'hr', 'ing', 'pending'].includes(updateData[field])) {
|
||
|
|
return res.status(400).json({
|
||
|
|
success: false,
|
||
|
|
message: 'status_type 必须是 offer, hr, ing 或 pending'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
updates.push(`${field} = ?`);
|
||
|
|
params.push(updateData[field]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (updates.length === 0) {
|
||
|
|
return res.status(400).json({
|
||
|
|
success: false,
|
||
|
|
message: '没有要更新的字段'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
params.push(id);
|
||
|
|
await db.query(
|
||
|
|
`UPDATE transition_progress SET ${updates.join(', ')} WHERE id = ?`,
|
||
|
|
params
|
||
|
|
);
|
||
|
|
|
||
|
|
return res.json({
|
||
|
|
success: true,
|
||
|
|
message: '面试进度更新成功'
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('更新面试进度失败:', error);
|
||
|
|
return res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: '更新面试进度失败',
|
||
|
|
error: error.message
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 删除面试进度记录
|
||
|
|
* DELETE /api/transition-progress/:id
|
||
|
|
* 需要登录
|
||
|
|
*/
|
||
|
|
exports.delete = async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { id } = req.params;
|
||
|
|
const userId = req.user.id;
|
||
|
|
|
||
|
|
const [result] = await db.query(
|
||
|
|
'DELETE FROM transition_progress WHERE id = ? AND user_id = ?',
|
||
|
|
[id, userId]
|
||
|
|
);
|
||
|
|
|
||
|
|
if (result.affectedRows === 0) {
|
||
|
|
return res.status(404).json({
|
||
|
|
success: false,
|
||
|
|
message: '面试进度记录不存在或无权访问'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return res.json({
|
||
|
|
success: true,
|
||
|
|
message: '面试进度删除成功'
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('删除面试进度失败:', error);
|
||
|
|
return res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: '删除面试进度失败',
|
||
|
|
error: error.message
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有面试进度记录 (管理员)
|
||
|
|
* GET /api/transition-progress/all
|
||
|
|
* 需要管理员权限
|
||
|
|
*/
|
||
|
|
exports.getAll = async (req, res) => {
|
||
|
|
try {
|
||
|
|
const page = parseInt(req.query.page) || 1;
|
||
|
|
const limit = parseInt(req.query.limit) || 20;
|
||
|
|
const offset = (page - 1) * limit;
|
||
|
|
const statusType = req.query.status_type;
|
||
|
|
|
||
|
|
// 构建查询条件
|
||
|
|
let whereClause = '';
|
||
|
|
const params = [];
|
||
|
|
|
||
|
|
if (statusType && ['offer', 'hr', 'ing', 'pending'].includes(statusType)) {
|
||
|
|
whereClause = 'WHERE status_type = ?';
|
||
|
|
params.push(statusType);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 查询总数
|
||
|
|
const [countResult] = await db.query(
|
||
|
|
`SELECT COUNT(*) as total FROM transition_progress ${whereClause}`,
|
||
|
|
params
|
||
|
|
);
|
||
|
|
const total = countResult[0].total;
|
||
|
|
|
||
|
|
// 查询记录列表
|
||
|
|
const [records] = await db.query(
|
||
|
|
`SELECT
|
||
|
|
tp.*,
|
||
|
|
u.username
|
||
|
|
FROM transition_progress tp
|
||
|
|
LEFT JOIN users u ON tp.user_id = u.id
|
||
|
|
${whereClause}
|
||
|
|
ORDER BY tp.created_at DESC
|
||
|
|
LIMIT ? OFFSET ?`,
|
||
|
|
[...params, limit, offset]
|
||
|
|
);
|
||
|
|
|
||
|
|
return res.json({
|
||
|
|
success: true,
|
||
|
|
data: {
|
||
|
|
records,
|
||
|
|
total,
|
||
|
|
page,
|
||
|
|
limit,
|
||
|
|
totalPages: Math.ceil(total / limit)
|
||
|
|
}
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('获取所有面试进度失败:', error);
|
||
|
|
return res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: '获取所有面试进度失败',
|
||
|
|
error: error.message
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|