feat(backend): 导出任务按类型声明筛选字段与导出模板

- exportDefs 为每种导出类型声明可传筛选字段与导出列模板,前端据此动态渲染提交表单
- /api/export-tasks/types 返回类型 + 字段 schema;提交时按 schema 过滤参数(cleanFilters)
- 扩充订单/商品等导出列定义与格式化规则

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-10-30 23:06:26 +08:00
parent 6c03a20364
commit 4343bcdd17
4 changed files with 1022 additions and 160 deletions

View File

@ -3,7 +3,7 @@
const fs = require('fs');
const { Controller } = require('egg');
const { parsePagination, buildPageResult } = require('../lib/pagination');
const { EXPORT_TYPES } = require('../utils/exportDefs');
const { EXPORT_TYPES, cleanFilters, listExportTypeSchema } = require('../utils/exportDefs');
/**
* @controller ExportTaskController 导出任务(全局任务池)
@ -15,14 +15,14 @@ const { EXPORT_TYPES } = require('../utils/exportDefs');
class ExportTaskController extends Controller {
/**
* @summary 导出类型选项
* @description 返回全部导出类型及其可传筛选字段(前端据此动态渲染提交表单)
* @router get /api/export-tasks/types
* @apikey
* @response 200 baseResponse success
*/
async types() {
const { ctx } = this;
const list = Object.keys(EXPORT_TYPES).map(k => ({ value: k, label: EXPORT_TYPES[k].label }));
ctx.body = { code: 0, message: 'success', data: list };
ctx.body = { code: 0, message: 'success', data: listExportTypeSchema() };
}
/**
@ -41,18 +41,7 @@ class ExportTaskController extends Controller {
ctx.body = { code: 400, message: '不支持的导出类型' };
return;
}
const clean = {};
if (filters && typeof filters === 'object') {
const allowed = [
'product_id', 'warehouse_id', 'biz_type', 'direction', 'bill_type', 'item_type',
'bill_no', 'keyword', 'category_name', 'status', 'submit_status', 'shop_id',
'start_time', 'end_time',
];
for (const k of allowed) {
const v = filters[k];
if (v !== undefined && v !== null && v !== '') clean[k] = v;
}
}
const clean = cleanFilters(type, filters);
const task = await ctx.service.exportTask.submit(type, clean, ctx.state.principalId, ctx.userId);
ctx.body = { code: 0, message: 'success', data: { id: task.id, type, status: task.status } };
}

View File

@ -14,7 +14,7 @@ module.exports = app => {
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 订单' },
type: { type: STRING(20), allowNull: false, comment: '导出类型:见 app/utils/exportDefs.js#EXPORT_TYPES(key 长度需 ≤ 20)' },
status: {
type: TINYINT.UNSIGNED,
allowNull: false,

View File

@ -44,7 +44,7 @@ class ExportTaskService extends Service {
* @return {Promise<Object>} 任务实例
*/
async submit(type, filters, principalId, userId) {
const { sql } = buildExportSql(type, filters, principalId);
const { sql } = buildExportSql(type, filters, principalId, userId);
return this.ctx.model.ExportTask.create({
principal_id: principalId,
type,
@ -134,7 +134,7 @@ class ExportTaskService extends Service {
const sheet = workbook.addWorksheet(TYPE_LABELS[task.type] || 'export');
try {
const filters = safeParse(task.params);
const { sql, countSql, columns, replacements } = buildExportSql(task.type, filters, task.principal_id);
const { sql, countSql, columns, replacements } = buildExportSql(task.type, filters, task.principal_id, task.created_by);
const [ countRow ] = await ctx.model.query(countSql, { replacements, type: QueryTypes.SELECT });
const total = Number(countRow && countRow.cnt) || 0;

File diff suppressed because it is too large Load Diff