oms/backend/app/model/exportTask.js
liufumingren fc58f79b42 feat(backend): 新增商品导出任务
- 新增 export_tasks 表与模型(迁移 20260916000000),记录导出类型、状态、进度、
  文件名与错误信息
- app/service/exportTask.js:生成 CSV(exceljs)并落盘到 upload/exports/<主体>/<年>/<月>/<日>
- app/schedule/exportWorker.js 定时执行待处理任务,cleanupExport.js 清理过期文件
- 接口:POST /api/export-tasks 提交、GET /api/export-tasks 列表与进度、
  GET /api/export-tasks/:id/download 下载
- app/utils/exportDefs.js 定义各导出类型的数据源;warehouseProduct / stockRecord 补充
  库存相关数据
- 契约补 exportTask 系列与 apikey 安全定义;.eslintrc 放行相关文件与 apikey 标签
- package-lock 同步
2026-09-14 22:14:23 +08:00

54 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 导出任务表,对应 export_tasks
* 全局导出任务池:前端提交导出请求(指定类型与筛选)即写入一条「待处理」任务并立即返回任务ID,
* 由定时任务(exportWorker)逐个认领并处理(生成 XLSX 落盘),前端轮询进度,完成后触发浏览器下载。
* 文件落盘路径区分 年/月/日:<uploadDir>/exports/YYYY/MM/DD/export_<type>_<id>_<ts>.xlsx
* @param {Egg.Application} app egg application
* @return {object} exportTask model
*/
module.exports = app => {
const { BIGINT, STRING, TINYINT, INTEGER, TEXT, DATE } = app.Sequelize;
const ExportTask = app.model.define('export_task', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID(按主体隔离)' },
type: { type: STRING(20), allowNull: false, comment: '导出类型:stock_record 出入库记录 / product 商品 / order 订单' },
status: {
type: TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '状态:0待处理 1处理中 2已完成 3失败',
},
sql: { type: TEXT, comment: '实际执行的 MySQL 查询语句(? 占位参数),用于审计与重跑' },
params: { type: TEXT, comment: '筛选参数 JSON(与 type 对应)' },
file_path: { type: STRING(255), comment: '相对上传根目录的文件路径 exports/YYYY/MM/DD/...' },
file_name: { type: STRING(255), comment: '文件名' },
total_count: { type: INTEGER.UNSIGNED, defaultValue: 0, comment: '总行数' },
processed_count: { type: INTEGER.UNSIGNED, defaultValue: 0, comment: '已处理行数(进度)' },
progress: { type: TINYINT.UNSIGNED, defaultValue: 0, comment: '进度百分比 0-100' },
error_message: { type: STRING(500), comment: '失败原因' },
created_by: { type: BIGINT.UNSIGNED, comment: '提交人ID' },
created_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
}, {
tableName: 'export_tasks',
indexes: [
{ fields: [ 'principal_id' ] },
{ fields: [ 'status' ] },
{ fields: [ 'created_at' ] },
],
});
return ExportTask;
};