- exportDefs 为每种导出类型声明可传筛选字段与导出列模板,前端据此动态渲染提交表单 - /api/export-tasks/types 返回类型 + 字段 schema;提交时按 schema 过滤参数(cleanFilters) - 扩充订单/商品等导出列定义与格式化规则 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
54 lines
2.4 KiB
JavaScript
54 lines
2.4 KiB
JavaScript
'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: '导出类型:见 app/utils/exportDefs.js#EXPORT_TYPES(key 长度需 ≤ 20)' },
|
||
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;
|
||
};
|