瞪眼法测试通过
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)
|
||||
@@ -9,14 +27,16 @@ function pagination(query) {
|
||||
return { page, pageSize, offset }
|
||||
}
|
||||
|
||||
// 列表查询时 JOIN 客户名和员工名
|
||||
// 列表查询时 JOIN 客户名、供应商名和员工名
|
||||
const LIST_SELECT = `
|
||||
SELECT
|
||||
c.*,
|
||||
cu.name AS customer_name,
|
||||
s.name AS supplier_name,
|
||||
e.name AS employee_name
|
||||
FROM contracts c
|
||||
LEFT JOIN customers cu ON c.customer_id = cu.id
|
||||
LEFT JOIN suppliers s ON c.supplier_id = s.id
|
||||
LEFT JOIN employees e ON c.employee_id = e.id
|
||||
`
|
||||
|
||||
@@ -27,25 +47,35 @@ const DETAIL_SELECT = LIST_SELECT
|
||||
async function list(req, res) {
|
||||
try {
|
||||
const { page, pageSize, offset } = pagination(req.query)
|
||||
const { status, customer_name, contract_no, employee_name } = req.query
|
||||
const { status, customer_name, contract_no, contract_name, employee_name, id, effective_date_start, effective_date_end } = req.query
|
||||
|
||||
let where = 'WHERE 1=1'
|
||||
const params = []
|
||||
|
||||
// 数据范围过滤
|
||||
// 数据范围过滤(列表查询使用表别名 c,需要加前缀)
|
||||
const scope = getDataScope(req.user, 'contracts')
|
||||
if (scope.deny) {
|
||||
return res.status(403).json({ code: 403, message: '无权访问此资源' })
|
||||
}
|
||||
if (scope.where) {
|
||||
where += ' AND ' + scope.where
|
||||
// 给 scope.where 中的字段加上表别名 c.
|
||||
const scopedWhere = scope.where.replace(/\b(responsible_user_id|type)\b/g, 'c.$1')
|
||||
where += ' AND ' + scopedWhere
|
||||
params.push(...scope.values)
|
||||
}
|
||||
|
||||
if (id) {
|
||||
where += ' AND c.id = ?'
|
||||
params.push(id)
|
||||
}
|
||||
if (status) {
|
||||
where += ' AND c.status = ?'
|
||||
params.push(status)
|
||||
}
|
||||
if (contract_name) {
|
||||
where += ' AND c.contract_name LIKE ?'
|
||||
params.push(`%${contract_name}%`)
|
||||
}
|
||||
if (customer_name) {
|
||||
where += ' AND cu.name LIKE ?'
|
||||
params.push(`%${customer_name}%`)
|
||||
@@ -58,10 +88,19 @@ async function list(req, res) {
|
||||
where += ' AND e.name LIKE ?'
|
||||
params.push(`%${employee_name}%`)
|
||||
}
|
||||
if (effective_date_start) {
|
||||
where += ' AND c.effective_date >= ?'
|
||||
params.push(effective_date_start)
|
||||
}
|
||||
if (effective_date_end) {
|
||||
where += ' AND c.effective_date <= ?'
|
||||
params.push(effective_date_end)
|
||||
}
|
||||
|
||||
const [[{ total }]] = await pool.query(
|
||||
`SELECT COUNT(*) AS total FROM contracts c
|
||||
LEFT JOIN customers cu ON c.customer_id = cu.id
|
||||
LEFT JOIN suppliers s ON c.supplier_id = s.id
|
||||
LEFT JOIN employees e ON c.employee_id = e.id
|
||||
${where}`,
|
||||
params
|
||||
@@ -100,7 +139,9 @@ async function detail(req, res) {
|
||||
return res.status(403).json({ code: 403, message: '无权访问此资源' })
|
||||
}
|
||||
if (scope.where) {
|
||||
sql += ' AND ' + scope.where
|
||||
// detail 查询使用表别名 c,需要给 scope.where 加上前缀
|
||||
const scopedWhere = scope.where.replace(/\b(responsible_user_id|type)\b/g, 'c.$1')
|
||||
sql += ' AND ' + scopedWhere
|
||||
params.push(...scope.values)
|
||||
}
|
||||
|
||||
@@ -118,22 +159,43 @@ async function detail(req, res) {
|
||||
// POST /api/contracts —— 新增
|
||||
async function create(req, res) {
|
||||
const {
|
||||
customer_id, contract_name, contract_no, contract_content,
|
||||
customer_id, supplier_id, contract_name, contract_no, contract_content,
|
||||
amount, effective_date, expiry_date, employee_id, status, remark,
|
||||
} = req.body || {}
|
||||
|
||||
if (!customer_id) {
|
||||
return res.status(400).json({ code: 400, message: '客户ID必填' })
|
||||
// 根据角色限制合同类型
|
||||
const contractType = req.body.type || (req.user.roleName === 'procurement_manager' ? 'supply' : 'franchise')
|
||||
if (req.user.roleName === 'procurement_manager' && contractType !== 'supply') {
|
||||
return res.status(403).json({ code: 403, message: '采购经理只能创建采购合同' })
|
||||
}
|
||||
if (req.user.roleName === 'franchise_manager' && contractType !== 'franchise') {
|
||||
return res.status(403).json({ code: 403, message: '招商经理只能创建加盟合同' })
|
||||
}
|
||||
|
||||
if (contractType === 'supply' && !supplier_id) {
|
||||
return res.status(400).json({ code: 400, message: '采购合同必须选择供应商' })
|
||||
}
|
||||
if (contractType !== 'supply' && !customer_id) {
|
||||
return res.status(400).json({ code: 400, message: '加盟合同必须选择客户' })
|
||||
}
|
||||
if (!contract_name) {
|
||||
return res.status(400).json({ code: 400, message: '合同名称必填' })
|
||||
}
|
||||
|
||||
try {
|
||||
// 校验客户是否存在
|
||||
const [cust] = await pool.query('SELECT id FROM customers WHERE id = ?', [customer_id])
|
||||
if (cust.length === 0) {
|
||||
return res.status(400).json({ code: 400, message: '关联客户不存在' })
|
||||
// 校验客户是否存在(非采购合同必填)
|
||||
if (customer_id) {
|
||||
const [cust] = await pool.query('SELECT id FROM customers WHERE id = ?', [customer_id])
|
||||
if (cust.length === 0) {
|
||||
return res.status(400).json({ code: 400, message: '关联客户不存在' })
|
||||
}
|
||||
}
|
||||
// 校验供应商是否存在(采购合同必填)
|
||||
if (supplier_id) {
|
||||
const [sup] = await pool.query('SELECT id FROM suppliers WHERE id = ?', [supplier_id])
|
||||
if (sup.length === 0) {
|
||||
return res.status(400).json({ code: 400, message: '关联供应商不存在' })
|
||||
}
|
||||
}
|
||||
// 校验业务员(如果填了)
|
||||
if (employee_id) {
|
||||
@@ -145,18 +207,18 @@ async function create(req, res) {
|
||||
|
||||
const [result] = await pool.query(
|
||||
`INSERT INTO contracts
|
||||
(customer_id, contract_name, contract_no, contract_content, amount, effective_date, expiry_date, employee_id, status, remark, type, responsible_user_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[customer_id, contract_name,
|
||||
(customer_id, supplier_id, contract_name, contract_no, contract_content, amount, effective_date, expiry_date, employee_id, status, remark, type, responsible_user_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[customer_id || null, supplier_id || null, contract_name,
|
||||
contract_no || null,
|
||||
contract_content || null,
|
||||
amount !== undefined ? amount : null,
|
||||
effective_date || null,
|
||||
expiry_date || null,
|
||||
formatDate(effective_date),
|
||||
formatDate(expiry_date),
|
||||
employee_id || null,
|
||||
status || '生效',
|
||||
status || '生效中',
|
||||
remark || null,
|
||||
req.body.type || (req.user.roleName === 'procurement_manager' ? 'supply' : 'franchise'),
|
||||
contractType,
|
||||
req.body.responsible_user_id || req.user.id]
|
||||
)
|
||||
|
||||
@@ -172,12 +234,12 @@ async function create(req, res) {
|
||||
async function update(req, res) {
|
||||
const { id } = req.params
|
||||
const fields = [
|
||||
'customer_id', 'contract_name', 'contract_no', 'contract_content',
|
||||
'customer_id', 'supplier_id', 'contract_name', 'contract_no', 'contract_content',
|
||||
'amount', 'effective_date', 'expiry_date', 'employee_id', 'status', 'remark', 'type', 'responsible_user_id',
|
||||
]
|
||||
|
||||
try {
|
||||
// 确认记录存在且在数据范围内
|
||||
// 确认记录存在且在数据范围内(detail/update/delete 不用表别名)
|
||||
let checkSql = 'SELECT id FROM contracts WHERE id = ?'
|
||||
const checkParams = [id]
|
||||
const scope = getDataScope(req.user, 'contracts')
|
||||
@@ -206,13 +268,24 @@ async function update(req, res) {
|
||||
return res.status(400).json({ code: 400, message: '关联业务员不存在' })
|
||||
}
|
||||
}
|
||||
if (req.body.supplier_id) {
|
||||
const [sup] = await pool.query('SELECT id FROM suppliers WHERE id = ?', [req.body.supplier_id])
|
||||
if (sup.length === 0) {
|
||||
return res.status(400).json({ code: 400, message: '关联供应商不存在' })
|
||||
}
|
||||
}
|
||||
|
||||
const sets = []
|
||||
const params = []
|
||||
for (const f of fields) {
|
||||
if (req.body[f] !== undefined) {
|
||||
sets.push(`${f} = ?`)
|
||||
params.push(req.body[f])
|
||||
// 日期字段需要格式化
|
||||
if (['effective_date', 'expiry_date'].includes(f)) {
|
||||
params.push(formatDate(req.body[f]))
|
||||
} else {
|
||||
params.push(req.body[f])
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sets.length === 0) {
|
||||
|
||||
Reference in New Issue
Block a user