oms/backend/database/migrations/20260916000000-create-export-tasks.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

53 lines
2.5 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';
/**
* 导出任务表(全局任务池):提交即落「待处理」任务,定时任务逐个处理并生成 CSV。
* 状态:0待处理 1处理中 2已完成 3失败;文件按 年/月/日 分目录存储。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'export_tasks',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
type: { type: Sequelize.STRING(20), allowNull: false, comment: '导出类型:stock_record/product/order' },
status: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '0待处理 1处理中 2已完成 3失败' },
sql: { type: Sequelize.TEXT, comment: '实际执行的 MySQL 查询语句(? 占位参数)' },
params: { type: Sequelize.TEXT, comment: '筛选参数 JSON' },
file_path: { type: Sequelize.STRING(255), comment: '相对上传根目录的文件路径' },
file_name: { type: Sequelize.STRING(255), comment: '文件名' },
total_count: { type: Sequelize.INTEGER.UNSIGNED, defaultValue: 0, comment: '总行数' },
processed_count: { type: Sequelize.INTEGER.UNSIGNED, defaultValue: 0, comment: '已处理行数' },
progress: { type: Sequelize.TINYINT.UNSIGNED, defaultValue: 0, comment: '进度百分比' },
error_message: { type: Sequelize.STRING(500), comment: '失败原因' },
created_by: { type: Sequelize.BIGINT.UNSIGNED, comment: '提交人ID' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '导出任务(全局任务池)表' }
);
await queryInterface.addIndex('export_tasks', { fields: [ 'principal_id' ], name: 'idx_et_principal' });
await queryInterface.addIndex('export_tasks', { fields: [ 'status' ], name: 'idx_et_status' });
await queryInterface.addIndex('export_tasks', { fields: [ 'created_at' ], name: 'idx_et_created' });
},
async down(queryInterface) {
await queryInterface.dropTable('export_tasks');
},
};