'use strict'; /** * 打印数据(params)有效期:店管家面团单打印数据只有 10 分钟有效, * 这里按保守口径回给调用方,便于 WMS 判断是否需要重新拉取。 */ const PRINT_DATA_TTL_SECONDS = 600; /** 单次最多换取的运单号数量(一次打印任务的面单张数不会太多) */ const MAX_BATCH = 200; /** * 拆掉报文外层的 request / deliveryOrders 包装,取真正的入参对象 * @param {Object} data 原始报文 * @return {Object} 入参对象 */ function unwrapBody(data) { if (!data || typeof data !== 'object') return {}; if (data.request && typeof data.request === 'object' && !Array.isArray(data.request)) return data.request; return data; } /** * 归一化成非空字符串数组(兼容单值、逗号分隔、数组) * @param {*} value 原始值 * @return {string[]} 去重后的字符串数组 */ function toStringList(value) { const raw = Array.isArray(value) ? value : [ value ]; const out = []; for (const item of raw) { if (item == null) continue; for (const part of String(item).split(',')) { const text = part.trim(); if (text) out.push(text); } } return [ ...new Set(out) ]; } /** * 解析取号记录里的打印记录(`print_history_ids` 存的是 JSON 字符串) * @param {string|Array} value 打印记录 * @return {Array} 打印记录数组 */ function parsePrintHistoryIds(value) { if (Array.isArray(value)) return value; if (typeof value !== 'string' || !value.trim()) return []; try { const parsed = JSON.parse(value); return Array.isArray(parsed) ? parsed : []; } catch { return []; } } /** * 从一条取号记录里挑出与运单号对应的打印记录 * * 一个取号请求可能取到多张面单(多包裹),`print_history_ids` 与 `waybill_codes` 按顺序对应; * 优先按 `waybill_code` 精确匹配,匹配不到时(历史数据缺该字段)退回按位取。 * @param {Object} record 取号记录(Sequelize 实例或 plain object) * @param {string} waybillCode 运单号 * @param {string[]} [preferredIds] 调用方直传的打印记录ID(命中则优先用) * @return {Object|null} 打印记录 { id, pid, waybill_code, batch_index } */ function pickPrintHistory(record, waybillCode, preferredIds = []) { const list = parsePrintHistoryIds(record && record.print_history_ids); if (!list.length) return null; if (preferredIds.length) { const hit = list.find(item => item && preferredIds.includes(String(item.id))); if (hit) return hit; } const matched = list.find(item => item && String(item.waybill_code || '') === waybillCode); if (matched) return matched; const codes = parsePrintHistoryIds(record && record.waybill_codes); const index = codes.findIndex(code => String(code) === waybillCode); return index >= 0 ? list[index] || null : null; } /** * 解析平台返回的打印数据(params),取出打印机配置与面单 document * * `Data[].PrintDataJson` 是字符串,里面还有一层 `printDataJson`,形如 * `{ PrinterConfig, PrintDataList: { task: { documents: [ { documentID, contents } ] } } }`。 * 同店铺的 PrinterConfig / templateURL 相同,多张面单只需把各自 documents 拼成一个 task。 * @param {string} printData 平台返回的 PrintDataJson * @return {{printer_config: Object|null, document: Object|null, documents: Array}} 解析结果 */ function parsePrintData(printData) { const empty = { printer_config: null, document: null, documents: [] }; if (typeof printData !== 'string' || !printData.trim()) return empty; try { const outer = JSON.parse(printData); const inner = outer && outer.printDataJson ? JSON.parse(outer.printDataJson) : outer; const documents = inner && inner.PrintDataList && inner.PrintDataList.task && inner.PrintDataList.task.documents; const list = Array.isArray(documents) ? documents : []; return { printer_config: (inner && inner.PrinterConfig) || null, document: list[0] || null, documents: list, }; } catch { return empty; } } /** * 取(并缓存)打单账号的完整实例:一次批量请求通常只涉及 1~2 个账号 * @param {Object} ctx egg context * @param {Map} memo 批内缓存 * @param {Object} params 参数 * @param {number} params.accountId 打单账号ID * @param {number} params.principalId 主体ID * @return {Promise} { account, service } */ async function loadAccount(ctx, memo, { accountId, principalId }) { const key = `${principalId}:${accountId}`; if (memo.has(key)) return memo.get(key); const account = await ctx.model.ExternalPrintAccount.findByPk(accountId, { include: [{ model: ctx.model.ExternalPrintPlatform, as: 'platform' }], }); if (!account || !account.platform || (account.principal_id && Number(account.principal_id) !== Number(principalId))) { memo.set(key, null); return null; } const service = ctx.service.externalPrintOrder.createPlatformService(account); const entry = { account, service }; memo.set(key, entry); return entry; } /** * 按运单号(或打印记录ID)重新拉取面单打印数据 * * 背景:取号返回的打印数据只有 10 分钟有效期,WMS 真正打印前必须换新的。 * 定位口径:WMS 传「店铺标识(CaiNiaoAuthInfoId)+ 运单号」,OMS 查取号记录得到该运单号的 * 打印记录ID,再回平台 `PrintHistory/LoadPrintData` 换新的 params。 * 也支持直传 `printHistoryIds`(命中即跳过按运单号的匹配)。 * @param {Object} ctx egg context * @param {Object} params 参数 * @param {number} params.principalId 主体ID(来自 app_key) * @param {number} [params.warehouseId] 仓库ID(来自 app_key,仅用于日志) * @param {Object} params.data 报文 * @param {string} [params.requestId] 调用方 request_id(日志用) * @return {Promise} { ok, message, data } */ async function getPrintData(ctx, { principalId, warehouseId, data, requestId }) { const Op = ctx.app.Sequelize.Op; const body = unwrapBody(data); ctx.logger.info('[waybill.printdata] 换取打印数据 principal=%s warehouse=%s requestId=%s', principalId, warehouseId || '-', requestId || '-'); const waybillCodes = toStringList( body.waybillCodes || body.waybillCode || body.mailNos || body.mailNo || body.expressCodes ); const historyIds = toStringList(body.printHistoryIds || body.printHistoryId); const shopIds = toStringList(body.caiNiaoAuthInfoIds || body.caiNiaoAuthInfoId || body.authInfoIds) .map(value => Number(value)) .filter(value => Number.isFinite(value) && value > 0); if (!waybillCodes.length && !historyIds.length) { return { ok: false, message: '缺少运单号(waybillCodes)或打印记录ID(printHistoryIds)' }; } if (waybillCodes.length > MAX_BATCH) { return { ok: false, message: `单次最多换取 ${MAX_BATCH} 个运单号的打印数据` }; } const where = { principal_id: principalId, status: 1 }; if (shopIds.length) where.cai_niao_auth_info_id = { [Op.in]: shopIds }; if (waybillCodes.length) where.waybill_code = { [Op.in]: waybillCodes }; const records = await ctx.model.WaybillFetchRecord.findAll({ where, order: [[ 'id', 'DESC' ]], }); // 运单号 → 取号记录(同号可能被多次取号,取最新一条) const byWaybill = new Map(); for (const record of records) { const code = String(record.waybill_code || ''); if (!code || byWaybill.has(code)) continue; if (shopIds.length && !shopIds.includes(Number(record.cai_niao_auth_info_id))) continue; byWaybill.set(code, record); } const results = []; const memo = new Map(); const groups = new Map(); // `${accountId}|${host}|${dbname}` → { accountId, host, dbname, items: [] } for (const code of waybillCodes) { const record = byWaybill.get(code); if (!record) { results.push({ waybill_code: code, success: false, message: '未找到该运单号的取号记录(可能不是通过 OMS 取的号)' }); continue; } const history = pickPrintHistory(record, code, historyIds); if (!history) { results.push({ waybill_code: code, success: false, message: '取号记录里没有该运单号的打印记录ID(PrintHistoryIds),无法换新的打印数据', }); continue; } const key = `${record.account_id}|${record.platform_host || ''}|${record.dbname || ''}`; if (!groups.has(key)) { groups.set(key, { accountId: Number(record.account_id), host: String(record.platform_host || ''), dbname: String(record.dbname || ''), items: [], }); } groups.get(key).items.push({ waybillCode: code, record, history }); } // 直传 printHistoryIds 但没有对应运单号的情况:按记录里的运单号补齐 if (historyIds.length) { for (const record of records) { const list = parsePrintHistoryIds(record.print_history_ids) .filter(item => item && historyIds.includes(String(item.id))); for (const history of list) { const code = String(history.waybill_code || record.waybill_code || ''); if (!code || results.some(row => row.waybill_code === code)) continue; if (waybillCodes.length && !waybillCodes.includes(code)) continue; const key = `${record.account_id}|${record.platform_host || ''}|${record.dbname || ''}`; if (!groups.has(key)) { groups.set(key, { accountId: Number(record.account_id), host: String(record.platform_host || ''), dbname: String(record.dbname || ''), items: [], }); } groups.get(key).items.push({ waybillCode: code, record, history }); } } } for (const group of groups.values()) { const found = await fetchGroup(ctx, { principalId, memo, group }); results.push(...found); } const success = results.filter(row => row.success).length; return { ok: true, message: 'ok', data: { total: results.length, success, failed: results.length - success, // params 只有 10 分钟有效期,调用方需要在这个时间内打印 expires_in: PRINT_DATA_TTL_SECONDS, results, }, }; } /** * 按「账号 + 域名 + 分区」分组拉取打印数据(同店铺一次请求取多张) * @param {Object} ctx egg context * @param {Object} params 参数 * @param {number} params.principalId 主体ID * @param {Map} params.memo 批内账号缓存 * @param {Object} params.group 分组 { accountId, host, dbname, items } * @return {Promise>} 逐运单号结果 */ async function fetchGroup(ctx, { principalId, memo, group }) { const { accountId, host, dbname, items } = group; const fallback = message => items.map(item => ({ waybill_code: item.waybillCode, success: false, message, })); const entry = await loadAccount(ctx, memo, { accountId, principalId }); if (!entry) return fallback('打单账号不存在或不属于该主体'); const { service } = entry; if (typeof service.loadPrintData !== 'function') return fallback('当前打单平台不支持重新获取打印数据'); let traceId = ''; try { const info = await service.getUserInfo(); traceId = (info.data && info.data.TraceId) || ''; } catch { traceId = ''; } let list = []; try { list = await service.loadPrintData(host, { traceId, dbname, printHistoryIds: items.map(item => item.history.id), }); } catch (err) { ctx.logger.warn('[waybill.printdata] 换打印数据失败 host=%s dbname=%s:%s', host, dbname, err.message); return fallback(`读取打印数据失败:${err.message}`); } const byHistoryId = new Map(list.map(item => [ String(item.print_history_id), item ])); return items.map(item => { const row = byHistoryId.get(String(item.history.id)); if (!row) { return { waybill_code: item.waybillCode, success: false, message: '平台未返回该运单号的打印数据' }; } const parsed = parsePrintData(row.print_data); return { waybill_code: item.waybillCode, success: true, message: 'ok', print_history_id: row.print_history_id, template_id: row.template_id, cai_niao_auth_info_id: item.record.cai_niao_auth_info_id == null ? null : Number(item.record.cai_niao_auth_info_id), dbname: item.record.dbname || '', expires_in: PRINT_DATA_TTL_SECONDS, // 新的打印数据(平台原样返回的 params 字符串) print_data: row.print_data, // 拆好的结构:同店铺多张面单共用一个 printer_config,documents 拼起来即可 printer_config: parsed.printer_config, document: parsed.document, }; }); } module.exports = { getPrintData, // 供单测/脚本复用 unwrapBody, toStringList, parsePrintHistoryIds, pickPrintHistory, parsePrintData, };