'use strict'; /** * 导出定义与 SQL 构造(纯函数,便于单测与复用) * - EXPORT_TYPES:支持的导出类型(出入库记录 / 商品 / 订单) * - buildExportSql(type, filters, principalId):按类型与筛选条件构造「参数化」SELECT 语句, * 返回 { sql, countSql, columns, replacements };所有用户输入经 ? 占位参数传递,避免 SQL 注入。 * - columns:每列 { key(查询字段), header(XLSX 表头), format?(原始值→展示值) },用于生成 XLSX 与中文标签。 */ /** 支持的导出类型 */ const EXPORT_TYPES = { stock_record: { label: '出入库记录', table: 'stock_records' }, product: { label: '商品', table: 'products' }, order: { label: '订单', table: 'orders' }, }; /** 状态中文(与 export_tasks.status 对齐) */ const STATUS_LABELS = { 0: '待处理', 1: '处理中', 2: '已完成', 3: '失败' }; /** 类型中文 */ const TYPE_LABELS = { stock_record: '出入库记录', product: '商品', order: '订单', }; const BIZ_LABELS = { outbound_lock: '出库推送锁定', outbound_unlock: '出库解锁', outbound_restore: '出库驳回回补', outbound_deduct: '出库确认扣减', inbound_increase: '入库确认增加', order_lock: '订单审核锁定', order_unlock: '订单解锁', order_deduct: '订单发货确认扣减', }; const ORDER_STATUS = { 1: '待处理', 2: '处理中', 3: '已完成', 4: '已取消' }; const SUBMIT_STATUS = { 0: '未上交', 1: '上交中', 2: '已上交', 3: '上交失败' }; /** * 解析收货人 JSON(orders.receiver 可能为对象或 JSON 字符串) * @param {*} value 原始值 * @return {string} 拼接后的「姓名 电话 地址」 */ function formatReceiver(value) { if (!value) return ''; let obj = value; if (typeof value === 'string') { try { obj = JSON.parse(value); } catch { return value; } } if (obj && typeof obj === 'object') { return [ obj.name, obj.phone, obj.address ].filter(Boolean).join(' '); } return String(value); } /** * 构造导出 SQL * @param {string} type 导出类型(见 EXPORT_TYPES) * @param {Object} filters 筛选条件(来自前端,键已做白名单过滤) * @param {number} principalId 主体ID(强制隔离,作为首个 ? 参数) * @return {{sql: string, countSql: string, columns: Array, replacements: Array}} */ function buildExportSql(type, filters, principalId) { const f = filters || {}; const def = EXPORT_TYPES[type]; if (!def) throw new Error('不支持的导出类型: ' + type); const wheres = [ 'principal_id = ?' ]; const replacements = [ principalId ]; function like(val) { return '%' + String(val == null ? '' : val) + '%'; } let columns; let select; if (type === 'stock_record') { columns = [ { key: 'id', header: 'ID' }, { key: 'created_at', header: '记录时间' }, { key: 'biz_type', header: '业务动作', format: v => BIZ_LABELS[v] || v }, { key: 'direction', header: '方向', format: v => (v === 1 ? '入库' : v === 2 ? '出库' : v) }, { key: 'bill_type', header: '单据类型', format: v => ({ 1: '入库单', 2: '出库单', 3: '发货单' }[v] || v) }, { key: 'bill_no', header: '单据号' }, { key: 'item_code', header: '商品编码' }, { key: 'item_name', header: '商品名称' }, { key: 'item_type', header: '库存类型', format: v => (v === 'CC' ? '残次品' : '正品') }, { key: 'quantity', header: '变动数量' }, { key: 'quantity_before', header: '总数量(前)' }, { key: 'quantity_after', header: '总数量(后)' }, { key: 'lock_quantity', header: '锁定数量' }, { key: 'lock_before', header: '锁定(前)' }, { key: 'lock_after', header: '锁定(后)' }, { key: 'available_before', header: '可用(前)' }, { key: 'available_after', header: '可用(后)' }, { key: 'remark', header: '备注' }, ]; select = columns.map(c => c.key).join(', '); if (f.product_id) { wheres.push('product_id = ?'); replacements.push(Number(f.product_id)); } if (f.warehouse_id) { wheres.push('warehouse_id = ?'); replacements.push(Number(f.warehouse_id)); } if (f.biz_type) { wheres.push('biz_type = ?'); replacements.push(String(f.biz_type)); } if (f.direction) { wheres.push('direction = ?'); replacements.push(Number(f.direction)); } if (f.bill_type) { wheres.push('bill_type = ?'); replacements.push(Number(f.bill_type)); } if (f.item_type) { wheres.push('item_type = ?'); replacements.push(String(f.item_type).toUpperCase()); } if (f.bill_no) { wheres.push('bill_no LIKE ?'); replacements.push(like(f.bill_no)); } if (f.keyword) { wheres.push('(item_code LIKE ? OR item_name LIKE ? OR bill_no LIKE ?)'); replacements.push(like(f.keyword), like(f.keyword), like(f.keyword)); } if (f.start_time) { wheres.push('created_at >= ?'); replacements.push(String(f.start_time) + ' 00:00:00'); } if (f.end_time) { wheres.push('created_at <= ?'); replacements.push(String(f.end_time) + ' 23:59:59'); } } else if (type === 'product') { columns = [ { key: 'id', header: 'ID' }, { key: 'item_code', header: '商品编码' }, { key: 'item_name', header: '商品名称' }, { key: 'sku_property', header: 'SKU规格' }, { key: 'category_name', header: '分类' }, { key: 'brand_name', header: '品牌' }, { key: 'barcode', header: '条码' }, { key: 'stock_unit', header: '单位' }, { key: 'retail_price', header: '零售价' }, { key: 'cost_price', header: '成本价' }, { key: 'status', header: '状态', format: v => (v === 1 || v === '1' || v === '启用' ? '启用' : '禁用') }, { key: 'remark', header: '备注' }, { key: 'created_at', header: '创建时间' }, ]; select = columns.map(c => c.key).join(', '); if (f.keyword) { wheres.push('(item_code LIKE ? OR item_name LIKE ? OR barcode LIKE ?)'); replacements.push(like(f.keyword), like(f.keyword), like(f.keyword)); } if (f.category_name) { wheres.push('category_name LIKE ?'); replacements.push(like(f.category_name)); } if (f.status !== undefined && f.status !== null && f.status !== '') { wheres.push('status = ?'); replacements.push(Number(f.status)); } } else if (type === 'order') { columns = [ { key: 'id', header: 'ID' }, { key: 'order_no', header: '订单号' }, { key: 'shop_id', header: '店铺ID' }, { key: 'order_type', header: '订单类型' }, { key: 'status', header: '状态', format: v => ORDER_STATUS[v] || v }, { key: 'wms_status', header: 'WMS状态' }, { key: 'wms_order_no', header: 'WMS单号' }, { key: 'submit_status', header: '上交状态', format: v => SUBMIT_STATUS[v] || v }, { key: 'total_amount', header: '订单金额' }, { key: 'pay_amount', header: '实付金额' }, { key: 'receiver', header: '收货人', format: v => formatReceiver(v) }, { key: 'remark', header: '备注' }, { key: 'created_at', header: '创建时间' }, ]; select = columns.map(c => c.key).join(', '); if (f.keyword) { wheres.push('(order_no LIKE ? OR remark LIKE ?)'); replacements.push(like(f.keyword), like(f.keyword)); } if (f.status !== undefined && f.status !== null && f.status !== '') { wheres.push('status = ?'); replacements.push(Number(f.status)); } if (f.submit_status !== undefined && f.submit_status !== null && f.submit_status !== '') { wheres.push('submit_status = ?'); replacements.push(Number(f.submit_status)); } if (f.shop_id) { wheres.push('shop_id = ?'); replacements.push(Number(f.shop_id)); } if (f.start_time) { wheres.push('created_at >= ?'); replacements.push(String(f.start_time) + ' 00:00:00'); } if (f.end_time) { wheres.push('created_at <= ?'); replacements.push(String(f.end_time) + ' 23:59:59'); } } else { throw new Error('不支持的导出类型: ' + type); } const whereSql = wheres.join(' AND '); const sql = `SELECT ${select} FROM ${def.table} WHERE ${whereSql} ORDER BY id DESC`; const countSql = `SELECT COUNT(*) AS cnt FROM ${def.table} WHERE ${whereSql}`; return { sql, countSql, columns, replacements }; } /** * 将 Date 格式化为本地时间字符串 YYYY-MM-DD HH:mm:ss(XLSX 单元格可读) * @param {Date|string|number} value 时间 * @return {string} */ function formatDate(value) { const dt = value instanceof Date ? value : new Date(value); if (isNaN(dt.getTime())) return ''; const p = n => String(n).padStart(2, '0'); return `${dt.getFullYear()}-${p(dt.getMonth() + 1)}-${p(dt.getDate())} ${p(dt.getHours())}:${p(dt.getMinutes())}:${p(dt.getSeconds())}`; } module.exports = { EXPORT_TYPES, STATUS_LABELS, TYPE_LABELS, buildExportSql, formatDate, };