done
This commit is contained in:
163
routes/employees.js
Normal file
163
routes/employees.js
Normal file
@@ -0,0 +1,163 @@
|
||||
// routes/employees.js —— 员工管理 CRUD
|
||||
const { pool } = require('../db')
|
||||
|
||||
function pagination(query) {
|
||||
const page = Math.max(Number(query.page) || 1, 1)
|
||||
const pageSize = Math.min(Math.max(Number(query.pageSize) || 10, 1), 100)
|
||||
const offset = (page - 1) * pageSize
|
||||
return { page, pageSize, offset }
|
||||
}
|
||||
|
||||
// GET /api/employees —— 列表
|
||||
async function list(req, res) {
|
||||
try {
|
||||
const { page, pageSize, offset } = pagination(req.query)
|
||||
const { name, department, status } = req.query
|
||||
|
||||
let where = 'WHERE 1=1'
|
||||
const params = []
|
||||
|
||||
if (name) {
|
||||
where += ' AND name LIKE ?'
|
||||
params.push(`%${name}%`)
|
||||
}
|
||||
if (department) {
|
||||
where += ' AND department LIKE ?'
|
||||
params.push(`%${department}%`)
|
||||
}
|
||||
if (status !== undefined && status !== '') {
|
||||
where += ' AND status = ?'
|
||||
params.push(Number(status))
|
||||
}
|
||||
|
||||
const [[{ total }]] = await pool.query(
|
||||
`SELECT COUNT(*) AS total FROM employees ${where}`,
|
||||
params
|
||||
)
|
||||
|
||||
const [rows] = await pool.query(
|
||||
`SELECT * FROM employees ${where} ORDER BY id DESC LIMIT ? OFFSET ?`,
|
||||
[...params, pageSize, offset]
|
||||
)
|
||||
|
||||
res.json({
|
||||
code: 0,
|
||||
message: 'ok',
|
||||
data: {
|
||||
list: rows,
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil(total / pageSize),
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[employees list] error:', e)
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/employees/:id —— 详情
|
||||
async function detail(req, res) {
|
||||
try {
|
||||
const [rows] = await pool.query('SELECT * FROM employees WHERE id = ?', [req.params.id])
|
||||
if (rows.length === 0) {
|
||||
return res.status(404).json({ code: 404, message: '员工不存在' })
|
||||
}
|
||||
res.json({ code: 0, message: 'ok', data: rows[0] })
|
||||
} catch (e) {
|
||||
console.error('[employees detail] error:', e)
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/employees —— 新增
|
||||
async function create(req, res) {
|
||||
const {
|
||||
name, gender, age, education, department,
|
||||
entry_date, position, salary, phone, email, status, remark,
|
||||
} = req.body || {}
|
||||
|
||||
if (!name) {
|
||||
return res.status(400).json({ code: 400, message: '员工姓名必填' })
|
||||
}
|
||||
|
||||
try {
|
||||
const [result] = await pool.query(
|
||||
`INSERT INTO employees (name, gender, age, education, department, entry_date, position, salary, phone, email, status, remark)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[name,
|
||||
gender || null,
|
||||
age !== undefined ? age : null,
|
||||
education || null,
|
||||
department || null,
|
||||
entry_date || null,
|
||||
position || null,
|
||||
salary !== undefined ? salary : null,
|
||||
phone || null,
|
||||
email || null,
|
||||
status !== undefined ? status : 1,
|
||||
remark || null]
|
||||
)
|
||||
const [rows] = await pool.query('SELECT * FROM employees WHERE id = ?', [result.insertId])
|
||||
res.json({ code: 0, message: 'ok', data: rows[0] })
|
||||
} catch (e) {
|
||||
console.error('[employees create] error:', e)
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
// PUT /api/employees/:id —— 更新
|
||||
async function update(req, res) {
|
||||
const { id } = req.params
|
||||
const fields = [
|
||||
'name', 'gender', 'age', 'education', 'department',
|
||||
'entry_date', 'position', 'salary', 'phone', 'email', 'status', 'remark',
|
||||
]
|
||||
|
||||
try {
|
||||
const [existing] = await pool.query('SELECT id FROM employees WHERE id = ?', [id])
|
||||
if (existing.length === 0) {
|
||||
return res.status(404).json({ code: 404, message: '员工不存在' })
|
||||
}
|
||||
|
||||
const sets = []
|
||||
const params = []
|
||||
for (const f of fields) {
|
||||
if (req.body[f] !== undefined) {
|
||||
sets.push(`${f} = ?`)
|
||||
params.push(req.body[f])
|
||||
}
|
||||
}
|
||||
if (sets.length === 0) {
|
||||
return res.status(400).json({ code: 400, message: '没有需要更新的字段' })
|
||||
}
|
||||
|
||||
params.push(id)
|
||||
await pool.query(`UPDATE employees SET ${sets.join(', ')} WHERE id = ?`, params)
|
||||
|
||||
const [rows] = await pool.query('SELECT * FROM employees WHERE id = ?', [id])
|
||||
res.json({ code: 0, message: 'ok', data: rows[0] })
|
||||
} catch (e) {
|
||||
console.error('[employees update] error:', e)
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/employees/:id —— 删除
|
||||
async function remove(req, res) {
|
||||
const { id } = req.params
|
||||
try {
|
||||
const [existing] = await pool.query('SELECT id FROM employees WHERE id = ?', [id])
|
||||
if (existing.length === 0) {
|
||||
return res.status(404).json({ code: 404, message: '员工不存在' })
|
||||
}
|
||||
await pool.query('DELETE FROM employees WHERE id = ?', [id])
|
||||
res.json({ code: 0, message: 'ok' })
|
||||
} catch (e) {
|
||||
console.error('[employees delete] error:', e)
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { list, detail, create, update, remove }
|
||||
Reference in New Issue
Block a user