'use strict'; /** * 拉单任务表(异步任务池):一键拉取全部外部打单平台店铺 / 后续其它来源的订单。 * * 与 export_tasks 的设计一致:提交只落一条「待处理」记录并立即返回任务ID, * 由定时任务(orderSyncWorker)认领后逐个店铺拉单,进度与逐店结果写回本表,前端轮询展示。 * * - scope:任务范围,当前只有 external_print(外部打单平台店铺),后续可扩展其它来源 * - shop_ids:null/空 表示按 scope 匹配全部可拉取店铺;否则仅拉取指定店铺 * - items:逐店结果 JSON 数组(店铺名 / 时间窗 / 订单数 / 新增 / 更新 / 跳过 / 作废 / 失败原因) */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable('order_sync_tasks', { id: { type: Sequelize.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID(按主体隔离)' }, scope: { type: Sequelize.STRING(30), allowNull: false, defaultValue: 'external_print', comment: '拉单范围:external_print=外部打单平台店铺(后续可扩展其它来源)', }, status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '状态:0待处理 1处理中 2已完成 3部分失败 4失败', }, start_time: { type: Sequelize.DATE, allowNull: true, comment: '拉单起始时间(按平台下单时间过滤)' }, end_time: { type: Sequelize.DATE, allowNull: true, comment: '拉单结束时间' }, lookback_days: { type: Sequelize.INTEGER, allowNull: true, comment: '回看天数(0=全部待处理)' }, shop_ids: { type: Sequelize.JSON, allowNull: true, comment: '指定店铺ID数组;空表示按范围匹配全部' }, total_shops: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '待拉取店铺数' }, processed_shops: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '已处理店铺数' }, failed_shops: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '失败店铺数' }, created_orders: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '新增订单数' }, updated_orders: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '更新订单数' }, skipped_orders: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '跳过(平台已发货)订单数' }, voided_orders: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '自动作废订单数(合计)' }, voided_shipped_orders: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '自动作废中「平台已发货/已终结」的订单数', }, voided_missing_orders: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '自动作废中「平台已查不到」的订单数', }, progress: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '进度百分比 0-100' }, items: { type: Sequelize.JSON, allowNull: true, comment: '逐店结果 JSON 数组' }, error_message: { type: Sequelize.STRING(500), allowNull: true, comment: '整体失败原因' }, created_by: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '提交人ID' }, started_at: { type: Sequelize.DATE, allowNull: true, comment: '开始执行时间' }, finished_at: { type: Sequelize.DATE, allowNull: true, comment: '执行结束时间' }, created_at: { type: Sequelize.DATE, allowNull: true, comment: '创建时间' }, updated_at: { type: Sequelize.DATE, allowNull: true, comment: '更新时间' }, }); await queryInterface.addIndex('order_sync_tasks', [ 'principal_id', 'status' ], { name: 'idx_order_sync_task_principal_status', }); await queryInterface.addIndex('order_sync_tasks', [ 'status' ], { name: 'idx_order_sync_task_status', }); await queryInterface.addIndex('order_sync_tasks', [ 'created_at' ], { name: 'idx_order_sync_task_created', }); }, async down(queryInterface) { await queryInterface.removeIndex('order_sync_tasks', 'idx_order_sync_task_created'); await queryInterface.removeIndex('order_sync_tasks', 'idx_order_sync_task_status'); await queryInterface.removeIndex('order_sync_tasks', 'idx_order_sync_task_principal_status'); await queryInterface.dropTable('order_sync_tasks'); }, };