瞪眼法测试通过
This commit is contained in:
@@ -2,6 +2,24 @@
|
||||
const { pool } = require('../db')
|
||||
const { getDataScope } = require('../middleware/permissions')
|
||||
|
||||
// 格式化日期为 YYYY-MM-DD
|
||||
function formatDate(dateStr) {
|
||||
if (!dateStr) return null
|
||||
if (typeof dateStr === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
|
||||
return dateStr // 已经是正确格式
|
||||
}
|
||||
try {
|
||||
const date = new Date(dateStr)
|
||||
if (isNaN(date.getTime())) return null
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
return `${year}-${month}-${day}`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function pagination(query) {
|
||||
const page = Math.max(Number(query.page) || 1, 1)
|
||||
const pageSize = Math.min(Math.max(Number(query.pageSize) || 10, 1), 100)
|
||||
@@ -13,7 +31,7 @@ function pagination(query) {
|
||||
async function list(req, res) {
|
||||
try {
|
||||
const { page, pageSize, offset } = pagination(req.query)
|
||||
const { name, department, status } = req.query
|
||||
const { id, name, status, department } = req.query
|
||||
|
||||
let where = 'WHERE 1=1'
|
||||
const params = []
|
||||
@@ -28,18 +46,22 @@ async function list(req, res) {
|
||||
params.push(...scope.values)
|
||||
}
|
||||
|
||||
if (id) {
|
||||
where += ' AND id = ?'
|
||||
params.push(Number(id))
|
||||
}
|
||||
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))
|
||||
}
|
||||
if (department) {
|
||||
where += ' AND department LIKE ?'
|
||||
params.push(`%${department}%`)
|
||||
}
|
||||
|
||||
const [[{ total }]] = await pool.query(
|
||||
`SELECT COUNT(*) AS total FROM employees ${where}`,
|
||||
@@ -114,7 +136,7 @@ async function create(req, res) {
|
||||
age !== undefined ? age : null,
|
||||
education || null,
|
||||
department || null,
|
||||
entry_date || null,
|
||||
formatDate(entry_date),
|
||||
position || null,
|
||||
salary !== undefined ? salary : null,
|
||||
phone || null,
|
||||
@@ -160,7 +182,12 @@ async function update(req, res) {
|
||||
for (const f of fields) {
|
||||
if (req.body[f] !== undefined) {
|
||||
sets.push(`${f} = ?`)
|
||||
params.push(req.body[f])
|
||||
// 日期字段需要格式化
|
||||
if (f === 'entry_date') {
|
||||
params.push(formatDate(req.body[f]))
|
||||
} else {
|
||||
params.push(req.body[f])
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sets.length === 0) {
|
||||
@@ -170,6 +197,11 @@ async function update(req, res) {
|
||||
params.push(id)
|
||||
await pool.query(`UPDATE employees SET ${sets.join(', ')} WHERE id = ?`, params)
|
||||
|
||||
// 同步 department 到关联的 users 表
|
||||
if (req.body.department !== undefined) {
|
||||
await pool.query('UPDATE users SET department = ? WHERE employee_id = ?', [req.body.department, id])
|
||||
}
|
||||
|
||||
const [rows] = await pool.query('SELECT * FROM employees WHERE id = ?', [id])
|
||||
res.json({ code: 0, message: 'ok', data: rows[0] })
|
||||
} catch (e) {
|
||||
@@ -196,10 +228,20 @@ async function remove(req, res) {
|
||||
if (existing.length === 0) {
|
||||
return res.status(404).json({ code: 404, message: '员工不存在或无权操作' })
|
||||
}
|
||||
// 禁止删除自己关联的员工
|
||||
const [selfCheck] = await pool.query('SELECT id FROM users WHERE employee_id = ? AND id = ?', [id, req.user.id])
|
||||
if (selfCheck.length > 0) {
|
||||
return res.status(400).json({ code: 400, message: '不能删除自己的员工账号' })
|
||||
}
|
||||
// 同步删除关联的用户账号
|
||||
await pool.query('DELETE FROM users WHERE employee_id = ?', [id])
|
||||
await pool.query('DELETE FROM employees WHERE id = ?', [id])
|
||||
res.json({ code: 0, message: 'ok' })
|
||||
} catch (e) {
|
||||
console.error('[employees delete] error:', e)
|
||||
if (e.code === 'ER_ROW_IS_REFERENCED_2') {
|
||||
return res.status(400).json({ code: 400, message: '该员工被合同或售后记录引用,无法删除' })
|
||||
}
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user