From 5dafbefa342df2323c42b65188aa1379c9fe3db9 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 3 Sep 2026 17:01:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(order):=20=E6=89=93=E5=8C=85=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=88=97=E8=A1=A8=E5=BC=82=E6=AD=A5=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E5=B9=B6=E6=96=B0=E5=A2=9E=20warehouse=5Fid+create=5Ftime=20?= =?UTF-8?q?=E5=A4=8D=E5=90=88=E7=B4=A2=E5=BC=95=EF=BC=8C=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E4=B8=8E=E5=AF=BC=E5=87=BA=E6=B6=88=E9=99=A4?= =?UTF-8?q?=20filesort=20=E6=85=A2=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/model/pack_task.js | 1 + app/service/systemMethods/order/packing.js | 274 ++++++++++- app/service/systems/order_packing.js | 460 ++++++++++++++++-- ...0902100000-add-packing-list-query-index.js | 19 + 4 files changed, 711 insertions(+), 43 deletions(-) create mode 100644 database/migrations/20260902100000-add-packing-list-query-index.js diff --git a/app/model/pack_task.js b/app/model/pack_task.js index 16944d7..fe2319f 100644 --- a/app/model/pack_task.js +++ b/app/model/pack_task.js @@ -128,6 +128,7 @@ module.exports = app => { indexes: [ { name: 'idx_order_id', fields: [ 'order_id' ] }, { name: 'idx_warehouse_id', fields: [ 'warehouse_id' ] }, + { name: 'idx_warehouse_create_time', fields: [ 'warehouse_id', 'create_time' ] }, { name: 'idx_workbench_id', fields: [ 'workbench_id' ] }, { name: 'idx_wave_id', fields: [ 'wave_id' ] }, { name: 'idx_waybill_code', fields: [ 'waybill_code' ] }, diff --git a/app/service/systemMethods/order/packing.js b/app/service/systemMethods/order/packing.js index f9b25ab..9819759 100644 --- a/app/service/systemMethods/order/packing.js +++ b/app/service/systemMethods/order/packing.js @@ -2,6 +2,9 @@ const MainService = require('../../main'); +const PACKING_EXPORT_TASK_CACHE_KEY_PREFIX = 'packing_export_task_'; +const PACKING_EXPORT_TASK_CACHE_TTL_SECONDS = 1800; + /** * 系统方法层:order.packing(打包任务接口包装层)。 * 主要职责:统一包装系统方法接口,处理权限、日志等公共逻辑,调用底层 service 实现业务功能。 @@ -11,9 +14,12 @@ class OrderPackingMethodService extends MainService { constructor(ctx) { super(ctx); const _noNeedLogin = ''; - const _noNeedRight = 'list,get'; - const _noNeedLog = 'list,get'; - const _noNeedAuth = 'list,get'; + // 免权限方法列表:export/exportTaskProgress 免权限(异步任务模式,与 order.export 口径一致,无 wms_permission 记录) + const _noNeedRight = 'list,get,export,exportTaskProgress'; + // 免日志方法列表:导出与进度查询不记录操作日志(与订单导出口径一致) + const _noNeedLog = 'list,get,export,exportTaskProgress'; + // 免鉴权(仓库有效期检查)方法列表:导出与进度查询不检查仓库有效期(与订单导出口径一致) + const _noNeedAuth = 'list,get,export,exportTaskProgress'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } @@ -91,6 +97,268 @@ class OrderPackingMethodService extends MainService { const result = await service.systems.orderPacking.forceRecord({ id }); return this.success('ok', result); } + + /** + * 提交打包任务导出任务(异步任务模式,与 order.export 口径一致)。 + * 入参 where/sort 与 list 完全一致;提交后立即返回 task_id, + * 后台异步生成单 Sheet Excel(打包任务列表)并上传, + * 通过 exportTaskProgress 查询进度与下载链接。 + * + * @param {Object} data 请求体。 + * @param {Object} [data.where] 筛选条件(与 list 的 where 语义一致)。 + * @param {Object} [data.sort] 排序参数(与 list 的 sort 语义一致)。 + * @return {Promise} 统一成功包装响应(含 task_id/status/progress 等任务信息)。 + */ + async export(data = {}) { + const { service } = this; + const { sort } = data; + const { where } = this.prepareWhereContext(data, { + specialFieldMap: { + quickWhere: { + bucket: 'quick', + }, + }, + }); + + // 仓库隔离:由服务端从当前上下文自动注入,禁止前端传入 + const warehouseId = Number(this.ctx.user && this.ctx.user.store_id || 0); + const enforcedWhere = { ...where }; + if (warehouseId > 0) { + enforcedWhere.warehouse_id = { operator: 'eq', value: warehouseId }; + } + + const taskId = this.generateExportTaskId(warehouseId); + const taskPayload = { + task_id: taskId, + status: 'submitted', + progress: 0, + current_count: 0, + total_count: 0, + message: '打包任务导出任务已提交', + result: null, + error: '', + }; + + await this.setExportTaskProgress(taskId, taskPayload); + + setImmediate(async () => { + try { + await this.setExportTaskProgress(taskId, { + task_id: taskId, + status: 'running', + progress: 5, + current_count: 0, + total_count: 0, + message: '打包任务导出任务执行中', + result: null, + error: '', + }); + + const result = await service.systems.orderPacking.exportPackTask({ + where: enforcedWhere, + sort, + warehouseId, + onProgress: async progress => { + const safeProgress = this.isPlainObject(progress) ? progress : {}; + await this.setExportTaskProgress(taskId, { + task_id: taskId, + status: safeProgress.status || 'running', + progress: safeProgress.progress, + current_count: safeProgress.currentCount, + total_count: safeProgress.totalCount, + message: safeProgress.message || '打包任务导出任务执行中', + result: safeProgress.result || null, + error: '', + }); + }, + }); + + await this.setExportTaskProgress(taskId, { + task_id: taskId, + status: 'success', + progress: 100, + current_count: result.current_count || 0, + total_count: result.total_count || 0, + message: '打包任务导出任务完成', + result, + error: '', + }); + } catch (error) { + const errorMessage = error && error.message ? error.message : `${error}`; + await this.setExportTaskProgress(taskId, { + task_id: taskId, + status: 'failed', + progress: 100, + current_count: 0, + total_count: 0, + message: '打包任务导出任务失败', + result: null, + error: errorMessage, + }); + } + }); + + return this.success('ok', { + ...taskPayload, + update_time: Date.now(), + }); + } + + /** + * 查询打包任务导出任务进度(与 order.exportTaskProgress 口径一致)。 + * + * @param {Object} data 请求体。 + * @param {string} [data.task_id] 任务ID(下划线写法)。 + * @param {string} [data.taskId] 任务ID(驼峰写法)。 + * @return {Promise} 统一成功包装响应 + * (含 task_id/status/progress/current_count/total_count/message/error/download_url/update_time)。 + */ + async exportTaskProgress(data = {}) { + const taskId = this.normalizeExportTaskId(data); + if (!taskId) { + return this.success('ok', { + task_id: '', + status: 'not_found', + progress: 0, + current_count: 0, + total_count: 0, + message: '任务不存在', + result: null, + error: '', + download_url: '', + update_time: Date.now(), + }); + } + + const cachedTask = await this.getExportTaskProgress(taskId); + if (!cachedTask) { + return this.success('ok', { + task_id: taskId, + status: 'not_found', + progress: 0, + current_count: 0, + total_count: 0, + message: '任务不存在', + result: null, + error: '', + download_url: '', + update_time: Date.now(), + }); + } + + const safeResult = this.isPlainObject(cachedTask.result) ? cachedTask.result : {}; + const downloadUrl = String( + safeResult.url + || safeResult.cdnUrl + || safeResult.cosUrl + || safeResult.fileKey + || '' + ); + + return this.success('ok', { + task_id: taskId, + ...cachedTask, + download_url: downloadUrl, + }); + } + + /** + * 生成打包任务导出任务ID。 + * @param {number|string} [warehouseId=''] 仓库ID(缺省兜底取当前上下文 store_id)。 + * @return {string} 导出任务ID。 + */ + generateExportTaskId(warehouseId = '') { + const normalizedWarehouseId = warehouseId || this.ctx?.user?.store_id || 'unknown'; + const timestamp = Date.now(); + const randomToken = Math.random().toString(36).slice(2, 10); + return `packing_export_${normalizedWarehouseId}_${timestamp}_${randomToken}`; + } + + /** + * 归一化打包任务导出任务ID。 + * @param {Object} data 请求体。 + * @param {string} [data.task_id] 任务ID(下划线写法)。 + * @param {string} [data.taskId] 任务ID(驼峰写法)。 + * @return {string} 归一化后的任务ID。 + */ + normalizeExportTaskId(data = {}) { + const normalizedData = this.isPlainObject(data) ? data : {}; + return String(normalizedData.task_id || normalizedData.taskId || '').trim(); + } + + /** + * 获取打包任务导出任务缓存键。 + * @param {string} taskId 任务ID。 + * @return {string} 缓存键。 + */ + getExportTaskCacheKey(taskId = '') { + const normalizedTaskId = String(taskId || '').trim(); + if (!normalizedTaskId) { + return ''; + } + return `${PACKING_EXPORT_TASK_CACHE_KEY_PREFIX}${normalizedTaskId}`; + } + + /** + * 写入打包任务导出任务进度缓存(Redis TTL 30 分钟)。 + * @param {string} taskId 任务ID。 + * @param {Object} payload 任务状态载荷。 + * @return {Promise} 无返回值。 + */ + async setExportTaskProgress(taskId = '', payload = {}) { + const cacheKey = this.getExportTaskCacheKey(taskId); + if (!cacheKey || typeof this.app.setCacheAsync !== 'function') { + return; + } + + const safePayload = this.isPlainObject(payload) ? payload : {}; + const normalizeCount = value => { + const numericValue = Number(value); + return Number.isFinite(numericValue) && numericValue >= 0 ? Math.round(numericValue) : 0; + }; + const progressPayload = { + task_id: String(taskId || '').trim(), + status: String(safePayload.status || 'submitted').trim() || 'submitted', + progress: Number.isFinite(Number(safePayload.progress)) ? Math.min(100, Math.max(0, Math.round(Number(safePayload.progress)))) : 0, + current_count: normalizeCount(safePayload.current_count), + total_count: normalizeCount(safePayload.total_count), + message: String(safePayload.message || ''), + result: safePayload.result ?? null, + error: String(safePayload.error || ''), + update_time: Date.now(), + }; + + try { + await this.app.setCacheAsync(cacheKey, progressPayload, PACKING_EXPORT_TASK_CACHE_TTL_SECONDS); + } catch (error) { + const errorMessage = error && error.message ? error.message : `${error}`; + this.ctx.logger.warn(`[order.packing.export] 写入导出任务缓存失败: ${errorMessage}`); + } + } + + /** + * 读取打包任务导出任务进度缓存。 + * @param {string} taskId 任务ID。 + * @return {Promise} 任务缓存,未命中返回 null。 + */ + async getExportTaskProgress(taskId = '') { + const cacheKey = this.getExportTaskCacheKey(taskId); + if (!cacheKey || typeof this.app.getCacheAsync !== 'function') { + return null; + } + + try { + const cached = await this.app.getCacheAsync(cacheKey); + if (!this.isPlainObject(cached)) { + return null; + } + return cached; + } catch (error) { + const errorMessage = error && error.message ? error.message : `${error}`; + this.ctx.logger.warn(`[order.packing.export] 读取导出任务缓存失败: ${errorMessage}`); + return null; + } + } } module.exports = OrderPackingMethodService; diff --git a/app/service/systems/order_packing.js b/app/service/systems/order_packing.js index 46c3f78..f87fe1d 100644 --- a/app/service/systems/order_packing.js +++ b/app/service/systems/order_packing.js @@ -1,8 +1,19 @@ 'use strict'; +const crypto = require('node:crypto'); +const fs = require('node:fs'); +const fsp = require('node:fs/promises'); +const os = require('node:os'); +const path = require('node:path'); +const ExcelJS = require('exceljs'); const Service = require('egg').Service; const { stripWaybillSegmentSuffix, escapeLikeWildcards } = require('../../lib/waybill_code'); +/** 打包任务导出主表分页拉取大小 */ +const PACK_TASK_EXPORT_PAGE_SIZE = 500; +/** 打包任务导出关联数据分批拉取大小 */ +const PACK_TASK_EXPORT_BATCH_SIZE = 100; + /** * 快捷搜索运单号模糊降级的最小关键词长度。 * 运单号通常较长(如 JDX057941864338),短于该长度的输入(如单字符 "7")没有模糊搜索意义, @@ -50,48 +61,31 @@ class OrderPackingService extends Service { } return `http://w${warehouseId}-c${recordingComputerId}.o.gztbyc.com${videoPath}`; } + /** - * 打包任务列表查询(仓库侧)。 - * 按仓库维度(warehouse_id)查询打包任务,支持分页、筛选、排序。 + * 创建带业务错误码的异常对象。 * - * @param {Object} where 筛选条件对象 - * @param {Object} [where.status] 任务状态:0=待处理 1=打包中 2=已完成 3=异常 - * @param {Object} [where.pack_mode] 打包模式:1=散单 2=团购 - * @param {Object} [where.waybill_code] 运单号(通过 wms_order_express 解析 order_id 后过滤;先剥离一单多件分段后缀,主体运单号等值匹配) - * @param {Object} [where.wave_no] 波次号(模糊匹配) - * @param {Object} [where.order_id] 订单ID(精确匹配) - * @param {Object} [where.workbench_id] 工作台ID(精确匹配) - * @param {Object} [where.quickWhere] 快捷搜索:支持波次号模糊匹配;运单号通过 express→order→wave 链路解析(团购按波次号查、散单按订单ID查;运单号先剥离分段后缀等值匹配,精确未命中且关键词长度≥4 时才降级模糊,短词禁止降级避免误命中) - * @param {Object} [where.start_time] 开始打包时间范围,支持 {operator:'between', value:[start,end]} - * @param {Object} [where.start_time_start] 开始打包时间起始(兼容旧写法) - * @param {Object} [where.start_time_end] 开始打包时间结束(兼容旧写法) - * @param {Object} [where.end_time] 结束打包时间范围,支持 {operator:'between', value:[start,end]} - * @param {Object} [where.create_time] 创建时间范围,支持 {operator:'between', value:[start,end]} - * @param {Object} pagination 分页参数 { page, pageSize } - * @param {Object} sort 排序参数 { sortField, sortOrder } - * @return {Promise} 打包任务列表与分页信息 + * @param {string} message 错误信息。 + * @param {number} [code=400] 业务错误码。 + * @return {Error} 包含 code 的异常对象。 */ - async list(where = {}, pagination = {}, sort = {}) { + createBusinessError(message, code = 400) { + const error = new Error(`${message}`); + error.code = code; + return error; + } + + /** + * 构建列表筛选条件(list 与 export 复用同一套语义)。 + * + * @param {Object} where 筛选条件对象(与 list 入参一致) + * @param {Object} Op Sequelize Op 对象 + * @param {number} warehouseId 仓库ID + * @return {Promise} Sequelize where 条件对象 + */ + async _buildListWhere(where = {}, Op, warehouseId) { const { ctx, app } = this; - const { Op } = app.Sequelize; - const page = Math.max(Number(pagination.page) || 1, 1); - const pageSize = Math.max(Number(pagination.pageSize) || 20, 1); - - // 仓库上下文 - const warehouseId = Number(ctx.user && ctx.user.store_id); - if (!warehouseId) { - return { - code: 400, - message: '用户未绑定仓库,无法查询打包任务', - data: { - list: [], - pagination: { page, pageSize, total: 0, totalPages: 0, hasPrevious: false, hasNext: false }, - }, - }; - } - - // 构建查询条件 const queryWhere = { warehouse_id: warehouseId }; // 状态筛选 @@ -381,16 +375,76 @@ class OrderPackingService extends Service { // 创建时间范围 applyTimeRangeFilter('create_time', where.create_time); - // 排序 + return queryWhere; + } + + /** + * 构建列表排序(list 与 export 复用同一套语义)。 + * @param {Object} sort 排序参数 { sortField, sortOrder } + * @return {Array} Sequelize order 数组 + */ + _buildListOrder(sort = {}) { const sortField = sort.sortField || 'create_time'; const sortOrder = String(sort.sortOrder || 'DESC').toUpperCase() === 'ASC' ? 'ASC' : 'DESC'; const validSortFields = [ 'id', 'order_id', 'warehouse_id', 'workbench_id', 'wave_id', 'pack_quantity', 'pack_mode', 'status', 'start_time', 'end_time', 'create_time', 'update_time' ]; const actualSortField = validSortFields.includes(sortField) ? sortField : 'create_time'; + return [[ actualSortField, sortOrder ]]; + } + /** + * 打包任务列表查询(仓库侧)。 + * 按仓库维度(warehouse_id)查询打包任务,支持分页、筛选、排序。 + * + * @param {Object} where 筛选条件对象 + * @param {Object} [where.status] 任务状态:0=待处理 1=打包中 2=已完成 3=异常 + * @param {Object} [where.pack_mode] 打包模式:1=散单 2=团购 + * @param {Object} [where.waybill_code] 运单号(通过 wms_order_express 解析 order_id 后过滤;先剥离一单多件分段后缀,主体运单号等值匹配) + * @param {Object} [where.wave_no] 波次号(模糊匹配) + * @param {Object} [where.order_id] 订单ID(精确匹配) + * @param {Object} [where.workbench_id] 工作台ID(精确匹配) + * @param {Object} [where.quickWhere] 快捷搜索:支持波次号模糊匹配;运单号通过 express→order→wave 链路解析(团购按波次号查、散单按订单ID查;运单号先剥离分段后缀等值匹配,精确未命中且关键词长度≥4 时才降级模糊,短词禁止降级避免误命中) + * @param {Object} [where.start_time] 开始打包时间范围,支持 {operator:'between', value:[start,end]} + * @param {Object} [where.start_time_start] 开始打包时间起始(兼容旧写法) + * @param {Object} [where.start_time_end] 开始打包时间结束(兼容旧写法) + * @param {Object} [where.end_time] 结束打包时间范围,支持 {operator:'between', value:[start,end]} + * @param {Object} [where.create_time] 创建时间范围,支持 {operator:'between', value:[start,end]} + * @param {Object} pagination 分页参数 { page, pageSize } + * @param {Object} sort 排序参数 { sortField, sortOrder } + * @return {Promise} 打包任务列表与分页信息 + */ + async list(where = {}, pagination = {}, sort = {}) { + const { ctx, app } = this; + const { Op } = app.Sequelize; + + const page = Math.max(Number(pagination.page) || 1, 1); + const pageSize = Math.max(Number(pagination.pageSize) || 20, 1); + + // 仓库上下文 + const warehouseId = Number(ctx.user && ctx.user.store_id); + if (!warehouseId) { + return { + code: 400, + message: '用户未绑定仓库,无法查询打包任务', + data: { + list: [], + pagination: { page, pageSize, total: 0, totalPages: 0, hasPrevious: false, hasNext: false }, + }, + }; + } + + // 构建查询条件(与导出复用同一套筛选语义) + const queryWhere = await this._buildListWhere(where, Op, warehouseId); + + + // 排序(与导出复用同一套排序语义) + const order = this._buildListOrder(sort); // 查询(含工作台、视频和运单关联) - const { count, rows } = await ctx.model.PackTask.findAndCountAll({ + // 性能优化:count 与 rows 分离。findAndCountAll 携带 hasMany include(videos/expresses)时, + // count 会生成 COUNT(DISTINCT) + 双 LEFT JOIN(生产实测 520ms),拆开后 count 走单表索引(约 44ms)。 + const count = await ctx.model.PackTask.count({ where: queryWhere }); + const rows = await ctx.model.PackTask.findAll({ where: queryWhere, - order: [[ actualSortField, sortOrder ]], + order, limit: pageSize, offset: (page - 1) * pageSize, include: [ @@ -479,6 +533,332 @@ class OrderPackingService extends Service { }; } + /** + * 导出打包任务列表(单 Sheet Excel,异步任务内部调用)。 + * 复用 list 的 where/sort 语义;仓库隔离由方法层注入 warehouseId,此处再强制兜底; + * 分页拉取主表全量数据后批量关联工作台/视频/运单,流式生成 Excel 并上传。 + * + * @param {Object} options 导出选项。 + * @param {Object} [options.where] 标准化 where 条件(与 list 一致)。 + * @param {Object} [options.sort] 排序参数(与 list 一致)。 + * @param {number} [options.warehouseId] 仓库ID(由方法层从上下文注入)。 + * @param {Function} [options.onProgress] 进度回调。 + * @return {Promise} 上传结果与统计信息(含 url/cdnUrl/cosUrl/fileKey/current_count/total_count/sheet_count)。 + */ + async exportPackTask(options = {}) { + const { ctx, app } = this; + const { Op } = app.Sequelize; + const safeOptions = options && typeof options === 'object' && !Array.isArray(options) ? options : {}; + const where = safeOptions.where && typeof safeOptions.where === 'object' ? { ...safeOptions.where } : {}; + const sort = safeOptions.sort && typeof safeOptions.sort === 'object' ? { ...safeOptions.sort } : {}; + const warehouseId = Number(safeOptions.warehouseId || 0); + const progressHandler = typeof safeOptions.onProgress === 'function' + ? safeOptions.onProgress + : async () => {}; + + if (!Number.isFinite(warehouseId) || warehouseId <= 0) { + throw this.createBusinessError('缺少仓库上下文', 400); + } + + // 复用 list 的 where/sort 语义,并强制仓库隔离(服务端注入,前端无需传 warehouse_id) + const queryWhere = await this._buildListWhere(where, Op, warehouseId); + const order = this._buildListOrder(sort); + + const totalCount = await ctx.model.PackTask.count({ where: queryWhere }); + + await progressHandler({ + status: 'running', + progress: totalCount > 0 ? 10 : 85, + currentCount: 0, + totalCount, + message: totalCount > 0 ? `已命中 ${totalCount} 条打包任务,开始导出` : '未命中数据,开始生成空文件', + }); + + // ---- 分页拉取主表全量数据 ---- + const allTaskRows = []; + let page = 1; + while (allTaskRows.length < totalCount) { + const rows = await ctx.model.PackTask.findAll({ + where: queryWhere, + order, + limit: PACK_TASK_EXPORT_PAGE_SIZE, + offset: (page - 1) * PACK_TASK_EXPORT_PAGE_SIZE, + raw: true, + }); + if (!Array.isArray(rows) || rows.length === 0) { + break; + } + allTaskRows.push(...rows); + page += 1; + await progressHandler({ + status: 'running', + progress: Math.min(30, 10 + Math.round((allTaskRows.length / Math.max(totalCount, 1)) * 20)), + currentCount: allTaskRows.length, + totalCount, + message: `正在拉取打包任务列表,已完成 ${allTaskRows.length}/${totalCount}`, + }); + } + + // ---- 批量拉取关联数据(工作台 / 视频 / 运单) ---- + const taskIds = allTaskRows + .map(row => Number(row.id)) + .filter(id => Number.isInteger(id) && id > 0); + const workbenchIds = [ ...new Set( + allTaskRows.map(row => Number(row.workbench_id || 0)).filter(id => Number.isInteger(id) && id > 0) + ) ]; + const orderIds = [ ...new Set( + allTaskRows.map(row => Number(row.order_id || 0)).filter(id => Number.isInteger(id) && id > 0) + ) ]; + + /** @type {Map} */ + const workbenchMap = new Map(); + if (workbenchIds.length > 0) { + const workbenches = await ctx.model.Workbench.findAll({ + where: { id: { [Op.in]: workbenchIds } }, + attributes: [ 'id', 'name', 'type', 'barcode' ], + raw: true, + }); + for (const wb of workbenches) { + workbenchMap.set(Number(wb.id), wb); + } + } + + /** @type {Map>} */ + const videosMap = new Map(); + if (taskIds.length > 0) { + for (let idx = 0; idx < taskIds.length; idx += PACK_TASK_EXPORT_BATCH_SIZE) { + const batchIds = taskIds.slice(idx, idx + PACK_TASK_EXPORT_BATCH_SIZE); + const videos = await ctx.model.PackTaskVideo.findAll({ + where: { pack_task_id: { [Op.in]: batchIds } }, + attributes: [ 'id', 'pack_task_id', 'video_url', 'camera_id', 'status' ], + raw: true, + }); + for (const video of videos) { + const key = Number(video.pack_task_id); + if (!videosMap.has(key)) { + videosMap.set(key, []); + } + videosMap.get(key).push(video); + } + } + } + + /** @type {Map} */ + const cameraNameMap = new Map(); + const cameraIds = [ ...new Set( + Array.from(videosMap.values()) + .flat() + .map(v => Number(v.camera_id || 0)) + .filter(id => Number.isInteger(id) && id > 0) + ) ]; + if (cameraIds.length > 0) { + const cameras = await ctx.model.Camera.findAll({ + where: { id: { [Op.in]: cameraIds } }, + attributes: [ 'id', 'custom_name', 'channel_name' ], + raw: true, + }); + for (const camera of cameras) { + cameraNameMap.set(Number(camera.id), camera.custom_name || camera.channel_name || ''); + } + } + + /** @type {Map>} */ + const expressesMap = new Map(); + if (orderIds.length > 0) { + for (let idx = 0; idx < orderIds.length; idx += PACK_TASK_EXPORT_BATCH_SIZE) { + const batchOrderIds = orderIds.slice(idx, idx + PACK_TASK_EXPORT_BATCH_SIZE); + const expresses = await ctx.model.OrderExpress.findAll({ + where: { + order_id: { [Op.in]: batchOrderIds }, + warehouse_id: warehouseId, + delete_at: null, + }, + attributes: [ 'id', 'order_id', 'waybill_code' ], + raw: true, + }); + for (const express of expresses) { + const key = Number(express.order_id); + if (!expressesMap.has(key)) { + expressesMap.set(key, []); + } + expressesMap.get(key).push(express); + } + } + } + + await progressHandler({ + status: 'running', + progress: 55, + currentCount: allTaskRows.length, + totalCount, + message: '正在生成 Excel 文件...', + }); + + // ---- 生成 Excel(流式写入,单 Sheet) ---- + const tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'wms-pack-task-export-')); + const now = new Date(); + const dateToken = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`; + const filename = `打包任务导出_${dateToken}.xlsx`; + const filePath = path.join(tempDir, filename); + const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ + filename: filePath, + useStyles: false, + useSharedStrings: false, + }); + + const normalizeCellValue = value => { + if (value === null || value === undefined) return ''; + if (value instanceof Date) return app.formatDate ? app.formatDate(value) : value.toISOString(); + return value; + }; + const formatDateTime = value => (value ? app.formatDate(value) : ''); + + const PACK_MODE_TEXT = { 1: '散单', 2: '团购' }; + const STATUS_TEXT = { 0: '待处理', 1: '打包中', 2: '已完成', 3: '异常' }; + + try { + const sheet = workbook.addWorksheet('打包任务列表'); + sheet.columns = [ + { header: '任务ID', key: 'id', width: 14 }, + { header: '订单ID', key: 'order_id', width: 14 }, + { header: '波次号', key: 'wave_no', width: 18 }, + { header: '运单号', key: 'waybill_code', width: 22 }, + { header: '关联运单号', key: 'waybill_codes', width: 30 }, + { header: '打包模式', key: 'pack_mode_text', width: 10 }, + { header: '任务状态', key: 'status_text', width: 10 }, + { header: '打包数量', key: 'pack_quantity', width: 10 }, + { header: '商品总数量', key: 'item_quantity', width: 12 }, + { header: '商品种类数', key: 'sku_count', width: 12 }, + { header: '工作台', key: 'workbench_name', width: 14 }, + { header: '作业人员', key: 'operator_text', width: 20 }, + { header: '开始打包时间', key: 'start_time', width: 20 }, + { header: '结束打包时间', key: 'end_time', width: 20 }, + { header: '创建时间', key: 'create_time', width: 20 }, + { header: '视频状态', key: 'has_video_text', width: 10 }, + { header: '录像可用', key: 'video_enable_text', width: 10 }, + { header: '备注', key: 'remark', width: 30 }, + ]; + + for (const row of allTaskRows) { + const taskId = Number(row.id); + const workbench = workbenchMap.get(Number(row.workbench_id || 0)); + const videos = videosMap.get(taskId) || []; + const expresses = expressesMap.get(Number(row.order_id || 0)) || []; + const waybillCodes = expresses + .map(e => e.waybill_code) + .filter(code => code && String(code).trim()) + .join(', '); + const hasVideo = videos.length > 0; + const videoEnable = videos.some(v => v.video_url && v.status !== 'failed'); + + sheet.addRow({ + id: taskId, + order_id: row.order_id, + wave_no: normalizeCellValue(row.wave_no), + waybill_code: normalizeCellValue(row.waybill_code), + waybill_codes: waybillCodes, + pack_mode_text: PACK_MODE_TEXT[Number(row.pack_mode)] || '', + status_text: STATUS_TEXT[Number(row.status)] || '', + pack_quantity: row.pack_quantity, + item_quantity: row.item_quantity, + sku_count: row.sku_count, + workbench_name: workbench ? normalizeCellValue(workbench.name) : '', + operator_text: normalizeCellValue(row.operator_text), + start_time: formatDateTime(row.start_time), + end_time: formatDateTime(row.end_time), + create_time: formatDateTime(row.create_time), + has_video_text: hasVideo ? '有视频' : '无视频', + video_enable_text: videoEnable ? '可用' : '不可用', + remark: normalizeCellValue(row.remark), + }).commit(); + } + sheet.commit(); + + await workbook.commit(); + + await progressHandler({ + status: 'running', + progress: 92, + currentCount: allTaskRows.length, + totalCount, + message: '正在上传文件...', + }); + + // ---- 上传文件 ---- + const uploadService = ctx?.service?.systems?.upload; + if (!uploadService || typeof uploadService.uploadFile !== 'function') { + throw new Error('上传服务不可用'); + } + + const stat = await fsp.stat(filePath); + const randomToken = crypto.randomBytes(6).toString('hex'); + const uploadKey = `exports/pack_task/${warehouseId}/${Date.now()}_${randomToken}.xlsx`; + const uploadStream = fs.createReadStream(filePath); + uploadStream.filename = filename; + uploadStream.mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; + uploadStream.fileSize = stat.size; + + const uploadResult = await uploadService.uploadFile(uploadStream, { + key: uploadKey, + category: 'document', + mimetype: 'xls,xlsx,csv', + }); + + let fileRecord = null; + if (typeof uploadService.uploadSuccessNotify === 'function') { + fileRecord = await uploadService.uploadSuccessNotify({ + url: uploadResult.fileName, + fileKey: uploadResult.fileName, + fileName: filename, + fileType: 'document', + mimeType: uploadStream.mimeType, + fileSize: stat.size, + userId: ctx.userId, + description: `打包任务导出(仓库${warehouseId}),共${totalCount}条打包任务`, + }); + } + + // 优先取 uploadFile 返回的完整 URL,其次取 uploadSuccessNotify 落库记录(兼容 data 包裹),最后才回退 COS key + const safeUploadResult = typeof uploadResult === 'object' && uploadResult !== null ? uploadResult : {}; + const resolvedFileRecord = (fileRecord && (fileRecord.data || fileRecord)) || null; + const fileUrlFromRecord = resolvedFileRecord + ? (resolvedFileRecord.file_url || resolvedFileRecord.fileUrl || resolvedFileRecord.url || '') + : ''; + const uploadResultUrl = safeUploadResult.url || safeUploadResult.cdnUrl || safeUploadResult.cosUrl || ''; + + const uploadInfo = { + filename, + fileKey: safeUploadResult.fileName || uploadKey, + url: uploadResultUrl || fileUrlFromRecord || safeUploadResult.fileName || uploadKey, + cdnUrl: safeUploadResult.cdnUrl || safeUploadResult.url || '', + cosUrl: safeUploadResult.cosUrl || '', + current_count: allTaskRows.length, + total_count: totalCount, + sheet_count: 1, + }; + + await progressHandler({ + status: 'success', + progress: 100, + currentCount: allTaskRows.length, + totalCount, + message: '导出任务完成', + result: uploadInfo, + }); + + return uploadInfo; + } catch (error) { + const workbookStream = workbook.stream; + if (workbookStream && typeof workbookStream.destroy === 'function') { + workbookStream.destroy(); + } + throw error; + } finally { + // 清理临时文件(不存在或删除失败均忽略) + try { await fsp.rm(tempDir, { recursive: true, force: true }); } catch (err) { /* ignore */ } + } + } + /** * 打包任务详情查询。 * 按 id + warehouse_id 查询单条打包任务记录。 diff --git a/database/migrations/20260902100000-add-packing-list-query-index.js b/database/migrations/20260902100000-add-packing-list-query-index.js new file mode 100644 index 0000000..040a7ed --- /dev/null +++ b/database/migrations/20260902100000-add-packing-list-query-index.js @@ -0,0 +1,19 @@ +'use strict'; + +/** + * order.packing.list 慢查询优化配套索引。 + * 列表默认按 create_time DESC 排序(_buildListOrder),原 idx_warehouse_id 单列索引 + * 命中后需 filesort 全仓库行数(生产实测 33722 行),新增 (warehouse_id, create_time) + * 复合索引消除 filesort,加速默认列表与导出查询。 + */ +module.exports = { + async up(queryInterface) { + await queryInterface.addIndex('wms_pack_task', [ 'warehouse_id', 'create_time' ], { + name: 'idx_warehouse_create_time', + }); + }, + + async down(queryInterface) { + await queryInterface.removeIndex('wms_pack_task', 'idx_warehouse_create_time'); + }, +};