diff --git a/backend/app/service/exportTask.js b/backend/app/service/exportTask.js index 0d781c6..bf27d8f 100644 --- a/backend/app/service/exportTask.js +++ b/backend/app/service/exportTask.js @@ -4,7 +4,7 @@ const fs = require('fs'); const path = require('path'); const { Service } = require('egg'); const ExcelJS = require('exceljs'); -const { buildExportSql, STATUS_LABELS, TYPE_LABELS, formatDate } = require('../utils/exportDefs'); +const { buildExportSql, STATUS_LABELS, TYPE_LABELS, formatDate, EXPORT_TYPES } = require('../utils/exportDefs'); /** 任务状态:0待处理 1处理中 2已完成 3失败 */ const STATUS = { PENDING: 0, PROCESSING: 1, COMPLETED: 2, FAILED: 3 }; @@ -164,6 +164,11 @@ class ExportTaskService extends Service { replacements, type: QueryTypes.SELECT, }); + // 类型自定义的补充列(如订单的序列号数量/列表):按当前 chunk 批量补齐,避免跨库方言差异 + const typeDef = EXPORT_TYPES[task.type]; + if (typeDef && typeof typeDef.decorateRows === 'function' && rows.length) { + await typeDef.decorateRows(ctx, rows, { principalId: task.principal_id, filters }); + } for (const row of rows) { const vals = {}; for (const c of columns) { diff --git a/backend/app/utils/exportDefs.js b/backend/app/utils/exportDefs.js index 95de6d0..67b62e6 100644 --- a/backend/app/utils/exportDefs.js +++ b/backend/app/utils/exportDefs.js @@ -179,7 +179,7 @@ function jsonText(value) { } /** - * 序列号列表 → 可读文本(MySQL JSON_ARRAYAGG 可能返回 JSON 字符串或数组) + * 序列号列表 → 可读文本(兼容数组 / JSON 字符串 / 逗号分隔字符串) * @param {*} value 原值 * @return {string} 用「、」连接后的文本 */ @@ -199,35 +199,6 @@ function snListText(value) { return String(list); } -/** - * 订单级序列号聚合表达式(order_sn_codes 一行一个 SN) - * @param {string} alias 订单表别名 - * @return {{count: string, list: string}} 数量与列表表达式 - */ -function orderSnExpr(alias) { - const where = `osc.order_id = ${alias}.id AND osc.principal_id = ${alias}.principal_id`; - return { - count: `(SELECT COUNT(*) FROM order_sn_codes osc WHERE ${where})`, - list: `(SELECT JSON_ARRAYAGG(x.sn_code) FROM (SELECT osc.sn_code FROM order_sn_codes osc WHERE ${where} ORDER BY osc.id) x)`, - }; -} - -/** - * 明细级序列号聚合表达式:按行号匹配,行号为空的回传记录按商品编码兜底 - * @param {string} orderAlias 订单表别名(o) - * @param {string} itemAlias 明细表别名(oi) - * @return {{count: string, list: string}} 数量与列表表达式 - */ -function itemSnExpr(orderAlias, itemAlias) { - const where = `osc.order_id = ${orderAlias}.id AND osc.principal_id = ${orderAlias}.principal_id` - + ` AND (osc.order_line_no = ${itemAlias}.order_line_no` - + ` OR (osc.order_line_no IS NULL AND osc.item_code = ${itemAlias}.item_code))`; - return { - count: `(SELECT COUNT(*) FROM order_sn_codes osc WHERE ${where})`, - list: `(SELECT JSON_ARRAYAGG(x.sn_code) FROM (SELECT osc.sn_code FROM order_sn_codes osc WHERE ${where} ORDER BY osc.id) x)`, - }; -} - /* 日期边界:只写到天(YYYY-MM-DD)时补 00:00:00 / 23:59:59 */ function dateBound(value, isEnd) { const text = String(value).trim(); @@ -307,13 +278,16 @@ function applyBillOwnerScope(wh, ctx, column) { * @param {Object} options 组装参数 * @param {string} options.from FROM 子句 * @param {Object} options.wh makeWhere() 结果 - * @param {Array} options.columns 列定义 + * @param {Array} options.columns 导出列定义(含仅由 decorateRows 补值的列) + * @param {Array} [options.selectColumns] SQL 实际查询的列(缺省等于 columns) * @param {string} [options.orderBy] 排序(默认 id DESC) * @param {Array} [options.leadRepl] 排在 WHERE 参数之前的参数(如 UNION 各分支的主体ID) * @return {{sql: string, countSql: string, columns: Array, replacements: Array}} 查询语句与列定义 */ -function buildResult({ from, wh, columns, orderBy = 'id DESC', leadRepl = [] }) { - const select = columns.map(c => (c.expr ? `${c.expr} AS ${c.key}` : c.key)).join(', '); +function buildResult({ from, wh, columns, selectColumns, orderBy = 'id DESC', leadRepl = [] }) { + // selectColumns:SQL 真正查询的列(缺省同 columns);columns 仍是导出列定义(可含仅由 decorateRows 补值的列) + const list = selectColumns || columns; + const select = list.map(c => (c.expr ? `${c.expr} AS ${c.key}` : c.key)).join(', '); const base = `SELECT ${select} FROM ${from}${wh.sql()}`; return { sql: `${base} ORDER BY ${orderBy}`, @@ -364,9 +338,10 @@ const ORDER_COLUMNS = [{ key: 'id', header: 'ID' }, { key: 'logistics_name', header: '物流公司名称' }, { key: 'express_code', header: '运单号' }, { key: 'express_codes', header: '运单号集合', format: jsonText }, - // 仓库回传的序列号(order_sn_codes):数量与列表,导出订单时一并带出 - { key: 'sn_count', header: '序列号数量', expr: orderSnExpr('t').count }, - { key: 'sn_codes', header: '序列号列表', expr: orderSnExpr('t').list, format: snListText }, + // 仓库回传的序列号(order_sn_codes):SQL 不查,由 decorateRows 按 chunk 补值 + // (MySQL 5.7 在 UNION/派生表里对「嵌套派生表的相关子查询」会报 Unknown column 't.id') + { key: 'sn_count', header: '序列号数量' }, + { key: 'sn_codes', header: '序列号列表', format: snListText }, { key: 'wms_status', header: 'WMS状态' }, { key: 'wms_order_no', header: 'WMS单号' }, // 回传外部打单平台(配送视图/配送异常视图关注;其它状态表同名字段恒为默认值) @@ -383,14 +358,18 @@ const ORDER_COLUMNS = [{ key: 'id', header: 'ID' }, /** 订单级序列号列(导出含明细时改用明细级 SN,避免与明细列重名) */ const ORDER_SN_COLUMN_KEYS = [ 'sn_count', 'sn_codes' ]; +/** 订单导出 SQL 真正查询的列(去掉只靠 decorateRows 补值的序列号列) */ +const ORDER_SELECT_COLUMNS = ORDER_COLUMNS.filter(c => !ORDER_SN_COLUMN_KEYS.includes(c.key)); + +/** 明细级序列号列(同样由 decorateRows 补值) */ +const ITEM_SN_COLUMN_KEYS = [ 'item_sn_count', 'item_sn_codes' ]; + /** * 订单含明细(export_mode=item)的列:订单字段 + 明细字段 + 明细级序列号 * 订单字段从内层查询结果(别名 o)按列名取,明细字段取 order_status_items(别名 oi) */ const ORDER_ITEM_COLUMNS = [ - ...ORDER_COLUMNS - .filter(c => !ORDER_SN_COLUMN_KEYS.includes(c.key)) - .map(c => ({ ...c, expr: `o.${c.key}` })), + ...ORDER_SELECT_COLUMNS.map(c => ({ ...c, expr: `o.${c.key}` })), { key: 'order_line_no', header: '行号', expr: 'oi.order_line_no' }, { key: 'item_code', header: '商品编码', expr: 'oi.item_code' }, { key: 'item_name', header: '商品名称', expr: 'oi.item_name' }, @@ -407,10 +386,78 @@ const ORDER_ITEM_COLUMNS = [ expr: 'oi.parse_status', format: labels({ 0: '未解析', 1: '已解析', 2: '解析失败' }), }, - { key: 'item_sn_count', header: '序列号数量', expr: itemSnExpr('o', 'oi').count }, - { key: 'item_sn_codes', header: '序列号列表', expr: itemSnExpr('o', 'oi').list, format: snListText }, + { key: 'item_sn_count', header: '序列号数量' }, + { key: 'item_sn_codes', header: '序列号列表', format: snListText }, ]; +/** 订单含明细导出真正查询的列(去掉靠 decorateRows 补值的明细级序列号列) */ +const ORDER_ITEM_SELECT_COLUMNS = ORDER_ITEM_COLUMNS.filter(c => !ITEM_SN_COLUMN_KEYS.includes(c.key)); + +/** + * 订单导出补充序列号列(每个 chunk 查询后按订单ID批量取 SN 再回填) + * + * 不在 SQL 里做聚合的原因是 MySQL 5.7 对「派生表 + 相关子查询 + 嵌套派生表(JSON_ARRAYAGG)」会报 + * `Unknown column 't.id' in 'where clause'`,且 GROUP_CONCAT 受 group_concat_max_len 截断。 + * 这里按当前 chunk 的订单ID批量查 order_sn_codes,纯 JS 分组: + * - 订单行模式:每行填 sn_count / sn_codes(该订单全部 SN) + * - 含明细模式:每行填 item_sn_count / item_sn_codes(按行号匹配,行号为空的回传记录按商品编码兜底) + * @param {Object} ctx egg context(需含 model.query) + * @param {Array} rows 当前 chunk 的查询结果(原地补值) + * @param {Object} options 参数 + * @param {Number} options.principalId 主体ID + * @param {Object} [options.filters] 导出筛选(含 export_mode) + * @return {Promise} 无 + */ +async function decorateOrderRows(ctx, rows, { principalId, filters } = {}) { + const list = Array.isArray(rows) ? rows : []; + if (!list.length) return; + const ids = [ ...new Set(list.map(row => Number(row.id)).filter(id => Number.isInteger(id) && id > 0)) ]; + if (!ids.length) return; + + const QueryTypes = ctx.model.QueryTypes || {}; + const snRows = await ctx.model.query( + 'SELECT order_id, order_line_no, item_code, sn_code FROM order_sn_codes ' + + 'WHERE principal_id = ? AND order_id IN (?) ORDER BY order_id, id', + { replacements: [ principalId, ids ], type: QueryTypes.SELECT } + ); + + const byOrder = new Map(); + const byLine = new Map(); + const byCode = new Map(); + const push = (map, key, value) => { + if (!map.has(key)) map.set(key, []); + map.get(key).push(value); + }; + for (const row of snRows) { + const orderId = Number(row.order_id); + const sn = String(row.sn_code == null ? '' : row.sn_code).trim(); + if (!sn) continue; + push(byOrder, orderId, sn); + const lineNo = String(row.order_line_no == null ? '' : row.order_line_no).trim(); + const itemCode = String(row.item_code == null ? '' : row.item_code).trim(); + if (lineNo) push(byLine, `${orderId}|${lineNo}`, sn); + else if (itemCode) push(byCode, `${orderId}|${itemCode}`, sn); + } + + const exportMode = String((filters && filters.export_mode) || '').trim() === 'item' ? 'item' : 'order'; + for (const row of list) { + const orderId = Number(row.id); + if (exportMode === 'item') { + const lineNo = String(row.order_line_no == null ? '' : row.order_line_no).trim(); + const itemCode = String(row.item_code == null ? '' : row.item_code).trim(); + const sns = byLine.get(`${orderId}|${lineNo}`) + || (itemCode ? byCode.get(`${orderId}|${itemCode}`) : null) + || []; + row.item_sn_count = sns.length; + row.item_sn_codes = sns; + } else { + const sns = byOrder.get(orderId) || []; + row.sn_count = sns.length; + row.sn_codes = sns; + } + } +} + /** 入库单列 */ const INBOUND_COLUMNS = [ { key: 'id', header: 'ID' }, @@ -887,7 +934,7 @@ const EXPORT_TYPES = { const exportMode = String(f.export_mode || '').trim() === 'item' ? 'item' : 'order'; // 订单按状态分表存储:指定状态时只查对应表,未指定时 UNION ALL 各状态表 const stages = ORDER_TABLE_BY_STATUS[f.order_stage] ? [ f.order_stage ] : ORDER_STATUS_ORDER; - const select = ORDER_COLUMNS + const select = ORDER_SELECT_COLUMNS .map(c => (c.expr ? `${c.expr} AS ${c.key}` : c.key)) .join(', '); // 所在公司权限:只能导出自己所属公司(货主)的订单,条件写进各状态表子查询 @@ -956,6 +1003,7 @@ const EXPORT_TYPES = { const orderPart = `(SELECT * FROM (${inner}) AS t${wh.sql()}) AS o`; return buildResult({ columns: ORDER_ITEM_COLUMNS, + selectColumns: ORDER_ITEM_SELECT_COLUMNS, from: `${orderPart} JOIN order_status_items oi ON oi.order_id = o.id AND oi.principal_id = o.principal_id`, // 条件已在子查询内生效,外层不再追加,但 WHERE 参数需跟在订单参数之后 wh: makeWhere(), @@ -965,13 +1013,16 @@ const EXPORT_TYPES = { } return buildResult({ - // 外层只按列名取值:计算列(货主名称 / SN 聚合)已在内层子查询算好 - columns: ORDER_COLUMNS.map(c => (c.expr ? { ...c, expr: undefined } : c)), + columns: ORDER_COLUMNS, + // 外层只按列名取值(计算列已在子查询算好);序列号列由 decorateRows 补值,不进 SQL + selectColumns: ORDER_SELECT_COLUMNS.map(c => ({ ...c, expr: undefined })), from: `(${inner}) AS t`, wh, leadRepl: stageRepl, }); }, + /** 每个 chunk 查询后补充序列号数量/列表(见 decorateOrderRows) */ + decorateRows: decorateOrderRows, }, inbound_order: { @@ -1461,4 +1512,6 @@ module.exports = { cleanFilters, listExportTypeSchema, formatDate, + snListText, + decorateOrderRows, }; diff --git a/backend/test/app/lib/exportDefsOrder.test.js b/backend/test/app/lib/exportDefsOrder.test.js index 56c6c79..5718ac1 100644 --- a/backend/test/app/lib/exportDefsOrder.test.js +++ b/backend/test/app/lib/exportDefsOrder.test.js @@ -7,20 +7,22 @@ const { buildExportSql, cleanFilters } = require('../../../app/utils/exportDefs' const GROUP_ACCESS = { isGroup: true, ownCompanyIds: [] }; describe('test/app/lib/exportDefsOrder.test.js', () => { - it('默认导出一行一个订单:不含明细列,带序列号数量/列表', () => { + it('默认导出一行一个订单:不含明细列,带序列号数量/列表(SN 由 decorateRows 补值,不进 SQL)', () => { const { sql, columns } = buildExportSql('order', { order_stage: 'pending' }, 5, 1, GROUP_ACCESS); const headers = columns.map(c => c.header); assert(headers.includes('序列号数量')); assert(headers.includes('序列号列表')); assert(!headers.includes('行号')); assert(!headers.includes('商家编码')); - assert(sql.includes('JSON_ARRAYAGG'), 'SN 列表用 JSON_ARRAYAGG 聚合'); + // MySQL 5.7 兼容:不在 SQL 里做 SN 聚合(派生表 + 嵌套派生表的相关子查询会报 Unknown column 't.id') + assert(!sql.includes('JSON_ARRAYAGG')); + assert(!sql.includes('order_sn_codes')); assert(!sql.includes('JOIN order_status_items')); // 内层固定补齐筛选/关联要用的列,但不输出到导出表 assert(sql.includes('t.source_platform_code AS source_platform_code')); }); - it('订单含明细:JOIN 明细表,一行一个商品并带明细级序列号', () => { + it('订单含明细:JOIN 明细表,一行一个商品并带明细级序列号列', () => { const { sql, columns, replacements } = buildExportSql( 'order', { order_stage: 'pending', export_mode: 'item' }, @@ -36,6 +38,7 @@ describe('test/app/lib/exportDefsOrder.test.js', () => { assert(sql.includes('oi.order_line_no')); assert(sql.includes('t.principal_id AS principal_id'), '内层需带 principal_id 供关联明细'); assert(sql.includes('ORDER BY o.id DESC')); + assert(!sql.includes('item_sn_count'), '明细级 SN 由 decorateRows 补值,不进 SQL'); assert.deepEqual(replacements, [ 5 ]); }); @@ -93,4 +96,67 @@ describe('test/app/lib/exportDefsOrder.test.js', () => { const idsField = def.filters.find(f => f.key === 'ids'); assert(idsField && idsField.hidden === true); }); + + describe('decorateRows(序列号回填)', () => { + /** + * 造一个只返回固定 SN 行的假 ctx + * @param {Array} snRows 让 model.query 返回的 SN 行 + * @return {Object} 含 calls / model 的假上下文 + */ + function fakeCtx(snRows) { + const calls = []; + return { + calls, + model: { + QueryTypes: { SELECT: 'SELECT' }, + query: async (sql, options) => { calls.push({ sql, options }); return snRows; }, + }, + }; + } + + it('订单行模式:按订单ID汇总 SN 数量与列表', async () => { + const { decorateOrderRows } = require('../../../app/utils/exportDefs'); + const ctx = fakeCtx([ + { order_id: 1, order_line_no: '1', item_code: 'A', sn_code: 'SN1' }, + { order_id: 1, order_line_no: '2', item_code: 'B', sn_code: 'SN2' }, + { order_id: 2, order_line_no: null, item_code: 'C', sn_code: 'SN3' }, + ]); + const rows = [{ id: 1 }, { id: 2 }, { id: 3 }]; + await decorateOrderRows(ctx, rows, { principalId: 9, filters: { export_mode: 'order' } }); + + assert(rows[0].sn_count === 2); + assert(rows[0].sn_codes.join(',') === 'SN1,SN2'); + assert(rows[1].sn_count === 1); + assert(rows[2].sn_count === 0, '无 SN 的订单数量为 0'); + assert.deepEqual(ctx.calls[0].options.replacements, [ 9, [ 1, 2, 3 ]]); + }); + + it('含明细模式:按行号匹配,行号为空的回传记录按商品编码兜底', async () => { + const { decorateOrderRows } = require('../../../app/utils/exportDefs'); + const ctx = fakeCtx([ + { order_id: 1, order_line_no: '1', item_code: 'A', sn_code: 'SN1' }, + { order_id: 1, order_line_no: null, item_code: 'B', sn_code: 'SN2' }, + ]); + const rows = [ + { id: 1, order_line_no: '1', item_code: 'A' }, + { id: 1, order_line_no: '2', item_code: 'B' }, + { id: 1, order_line_no: '3', item_code: 'C' }, + ]; + await decorateOrderRows(ctx, rows, { principalId: 9, filters: { export_mode: 'item' } }); + + assert(rows[0].item_sn_count === 1); + assert(rows[0].item_sn_codes[0] === 'SN1'); + assert(rows[1].item_sn_count === 1, '行号为空的 SN 按商品编码挂到对应行'); + assert(rows[1].item_sn_codes[0] === 'SN2'); + assert(rows[2].item_sn_count === 0); + }); + + it('空数据 / 无订单ID时不查库', async () => { + const { decorateOrderRows } = require('../../../app/utils/exportDefs'); + const ctx = fakeCtx([]); + await decorateOrderRows(ctx, [], { principalId: 9 }); + await decorateOrderRows(ctx, [{ id: null }], { principalId: 9 }); + assert(ctx.calls.length === 0); + }); + }); });