252 lines
6.6 KiB
JavaScript
252 lines
6.6 KiB
JavaScript
|
|
/**
|
|||
|
|
* 高薪岗位控制器
|
|||
|
|
* 基于high.html的最新数据结构
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
const db = require('../../config/database');
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取岗位列表
|
|||
|
|
* GET /api/high-salary-jobs
|
|||
|
|
* 支持分页、筛选、搜索
|
|||
|
|
*/
|
|||
|
|
exports.getList = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const page = parseInt(req.query.page) || 1;
|
|||
|
|
const limit = parseInt(req.query.limit) || 20;
|
|||
|
|
const offset = (page - 1) * limit;
|
|||
|
|
const location = req.query.location;
|
|||
|
|
const keyword = req.query.keyword;
|
|||
|
|
|
|||
|
|
// 构建查询条件
|
|||
|
|
let whereClause = 'WHERE is_active = 1';
|
|||
|
|
const params = [];
|
|||
|
|
|
|||
|
|
if (location) {
|
|||
|
|
whereClause += ' AND location = ?';
|
|||
|
|
params.push(location);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (keyword) {
|
|||
|
|
whereClause += ' AND (title LIKE ? OR company LIKE ?)';
|
|||
|
|
params.push(`%${keyword}%`, `%${keyword}%`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询总数
|
|||
|
|
const [countResult] = await db.query(
|
|||
|
|
`SELECT COUNT(*) as total FROM high_salary_jobs ${whereClause}`,
|
|||
|
|
params
|
|||
|
|
);
|
|||
|
|
const total = countResult[0].total;
|
|||
|
|
|
|||
|
|
// 查询岗位列表
|
|||
|
|
const [jobs] = await db.query(
|
|||
|
|
`SELECT * FROM high_salary_jobs ${whereClause} ORDER BY display_order ASC, id DESC LIMIT ? OFFSET ?`,
|
|||
|
|
[...params, limit, offset]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
return res.json({
|
|||
|
|
success: true,
|
|||
|
|
data: {
|
|||
|
|
jobs,
|
|||
|
|
total,
|
|||
|
|
page,
|
|||
|
|
limit,
|
|||
|
|
totalPages: Math.ceil(total / limit)
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取岗位列表失败:', error);
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '获取岗位列表失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取岗位详情
|
|||
|
|
* GET /api/high-salary-jobs/:id
|
|||
|
|
*/
|
|||
|
|
exports.getById = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { id } = req.params;
|
|||
|
|
|
|||
|
|
const [jobs] = await db.query(
|
|||
|
|
'SELECT * FROM high_salary_jobs WHERE id = ? AND is_active = 1',
|
|||
|
|
[id]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
if (jobs.length === 0) {
|
|||
|
|
return res.status(404).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '岗位不存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return res.json({
|
|||
|
|
success: true,
|
|||
|
|
data: jobs[0]
|
|||
|
|
});
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取岗位详情失败:', error);
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '获取岗位详情失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建岗位(管理员)
|
|||
|
|
* POST /api/high-salary-jobs
|
|||
|
|
*/
|
|||
|
|
exports.create = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const {
|
|||
|
|
title,
|
|||
|
|
salary,
|
|||
|
|
company,
|
|||
|
|
location,
|
|||
|
|
quota,
|
|||
|
|
tags,
|
|||
|
|
requirements,
|
|||
|
|
description,
|
|||
|
|
icon,
|
|||
|
|
display_order
|
|||
|
|
} = req.body;
|
|||
|
|
|
|||
|
|
// 验证必填字段
|
|||
|
|
if (!title || !salary || !company || !location || quota === undefined) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '缺少必填字段'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const [result] = await db.query(
|
|||
|
|
`INSERT INTO high_salary_jobs
|
|||
|
|
(title, salary, company, location, quota, tags, requirements, description, icon, display_order)
|
|||
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|||
|
|
[
|
|||
|
|
title,
|
|||
|
|
salary,
|
|||
|
|
company,
|
|||
|
|
location,
|
|||
|
|
quota,
|
|||
|
|
JSON.stringify(tags || []),
|
|||
|
|
requirements,
|
|||
|
|
description,
|
|||
|
|
icon || 'fa-microchip',
|
|||
|
|
display_order || 0
|
|||
|
|
]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
res.status(201).json({
|
|||
|
|
success: true,
|
|||
|
|
message: '岗位创建成功',
|
|||
|
|
data: { id: result.insertId }
|
|||
|
|
});
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('创建岗位失败:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '创建岗位失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 更新岗位(管理员)
|
|||
|
|
* PUT /api/high-salary-jobs/:id
|
|||
|
|
*/
|
|||
|
|
exports.update = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { id } = req.params;
|
|||
|
|
const updateData = req.body;
|
|||
|
|
|
|||
|
|
// 检查岗位是否存在
|
|||
|
|
const [existing] = await db.query('SELECT id FROM high_salary_jobs WHERE id = ?', [id]);
|
|||
|
|
if (existing.length === 0) {
|
|||
|
|
return res.status(404).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '岗位不存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const updates = [];
|
|||
|
|
const params = [];
|
|||
|
|
|
|||
|
|
const allowedFields = ['title', 'salary', 'company', 'location', 'quota', 'tags', 'requirements', 'description', 'icon', 'display_order', 'is_active'];
|
|||
|
|
|
|||
|
|
for (const field of allowedFields) {
|
|||
|
|
if (updateData[field] !== undefined) {
|
|||
|
|
updates.push(`${field} = ?`);
|
|||
|
|
params.push(field === 'tags' ? JSON.stringify(updateData[field]) : updateData[field]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (updates.length === 0) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '没有要更新的字段'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
params.push(id);
|
|||
|
|
await db.query(
|
|||
|
|
`UPDATE high_salary_jobs SET ${updates.join(', ')} WHERE id = ?`,
|
|||
|
|
params
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
success: true,
|
|||
|
|
message: '岗位更新成功'
|
|||
|
|
});
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('更新岗位失败:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '更新岗位失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 删除岗位(软删除)
|
|||
|
|
* DELETE /api/high-salary-jobs/:id
|
|||
|
|
*/
|
|||
|
|
exports.delete = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { id } = req.params;
|
|||
|
|
|
|||
|
|
const [result] = await db.query(
|
|||
|
|
'UPDATE high_salary_jobs SET is_active = 0 WHERE id = ?',
|
|||
|
|
[id]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
if (result.affectedRows === 0) {
|
|||
|
|
return res.status(404).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '岗位不存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
success: true,
|
|||
|
|
message: '岗位删除成功'
|
|||
|
|
});
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('删除岗位失败:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '删除岗位失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|