322 lines
10 KiB
JavaScript
322 lines
10 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 创建报表投递三表:报表模板定义 / 货主投递计划 / 投递记录。
|
||
*
|
||
* 背景:货主侧 7 日出入库报表采用「配置驱动 + 定时投递」方案:
|
||
* 报表模板可配置上新(免发版),每个货主可配置每日多个投递时间点,
|
||
* 由分钟级扫描调度命中后生成报表并按渠道投递。
|
||
*
|
||
* 三表职责:
|
||
* - wms_report_definition:报表模板定义(code/name/scope + output/source/sheet 三段 JSON 配置)
|
||
* - wms_report_delivery_plan:货主每日投递计划(schedule_times JSON 时间点数组)
|
||
* - wms_report_delivery_log:投递记录(pending→exporting→delivered/failed/cancelled 状态机 + 退避重试)
|
||
*
|
||
* 约定:
|
||
* - 逻辑关联、无物理外键(与项目现状一致)
|
||
* - 唯一索引按项目惯例含 deleted_at,避免软删后重建撞唯一键
|
||
* - 幂等键 uk_store_report_biz_time 保证同一货主同一报表同一数据日期同一时间点不重复投递
|
||
*/
|
||
|
||
const TABLE_DEFINITION = 'wms_report_definition';
|
||
const TABLE_PLAN = 'wms_report_delivery_plan';
|
||
const TABLE_LOG = 'wms_report_delivery_log';
|
||
|
||
module.exports = {
|
||
async up(queryInterface, Sequelize) {
|
||
const { BIGINT, INTEGER, STRING, TINYINT, DATE, DATEONLY, ENUM } = Sequelize;
|
||
const tables = await queryInterface.showAllTables();
|
||
const tableNames = tables.map(t => (typeof t === 'string' ? t : t.tableName));
|
||
|
||
if (!tableNames.includes(TABLE_DEFINITION)) {
|
||
await queryInterface.createTable(TABLE_DEFINITION, {
|
||
id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键',
|
||
},
|
||
code: {
|
||
type: STRING(100),
|
||
allowNull: false,
|
||
comment: '报表编码,全局唯一(如 week_inout_7d)',
|
||
},
|
||
name: {
|
||
type: STRING(100),
|
||
allowNull: false,
|
||
comment: '报表名称',
|
||
},
|
||
scope: {
|
||
type: ENUM('warehouse', 'store', 'open'),
|
||
allowNull: false,
|
||
defaultValue: 'warehouse',
|
||
comment: '适用维度:warehouse=仓库 store=货主 open=通用',
|
||
},
|
||
description: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '报表描述',
|
||
},
|
||
output_config: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '输出配置(文件名/目录等)',
|
||
},
|
||
source_config: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '数据源配置(查询器 key + 窗口参数)',
|
||
},
|
||
sheet_config: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '工作表配置(列定义/合并/样式/行过滤/列级 format)',
|
||
},
|
||
is_enabled: {
|
||
type: TINYINT(1),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '是否启用:0=停用 1=启用',
|
||
},
|
||
remark: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '备注',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
}, {
|
||
comment: '报表模板定义表',
|
||
engine: 'InnoDB',
|
||
charset: 'utf8mb4',
|
||
collate: 'utf8mb4_general_ci',
|
||
});
|
||
|
||
await queryInterface.addIndex(TABLE_DEFINITION, [ 'code', 'deleted_at' ], {
|
||
unique: true,
|
||
name: 'uk_code',
|
||
});
|
||
await queryInterface.addIndex(TABLE_DEFINITION, [ 'deleted_at' ], {
|
||
name: 'idx_deleted_at',
|
||
});
|
||
}
|
||
|
||
if (!tableNames.includes(TABLE_PLAN)) {
|
||
await queryInterface.createTable(TABLE_PLAN, {
|
||
id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键',
|
||
},
|
||
store_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '货主ID(wms_store.id)',
|
||
},
|
||
report_code: {
|
||
type: STRING(100),
|
||
allowNull: false,
|
||
comment: '报表编码(wms_report_definition.code)',
|
||
},
|
||
schedule_times: {
|
||
type: Sequelize.JSON,
|
||
allowNull: false,
|
||
comment: '每日投递时间点数组,如 ["08:30","17:00"](HH:mm)',
|
||
},
|
||
channel: {
|
||
type: STRING(20),
|
||
allowNull: false,
|
||
defaultValue: 'wecom',
|
||
comment: '投递渠道:wecom=企业微信 inbox=站内信群 link=链接',
|
||
},
|
||
enabled: {
|
||
type: TINYINT(1),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '是否启用:0=停用 1=启用',
|
||
},
|
||
late_grace_minutes: {
|
||
type: INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 60,
|
||
comment: '补投宽限分钟:重启/漏跑后在该窗口内补投,超窗作废',
|
||
},
|
||
remark: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '备注',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
}, {
|
||
comment: '货主报表投递计划表',
|
||
engine: 'InnoDB',
|
||
charset: 'utf8mb4',
|
||
collate: 'utf8mb4_general_ci',
|
||
});
|
||
|
||
await queryInterface.addIndex(TABLE_PLAN, [ 'store_id', 'report_code', 'deleted_at' ], {
|
||
unique: true,
|
||
name: 'uk_store_report_code',
|
||
});
|
||
await queryInterface.addIndex(TABLE_PLAN, [ 'enabled', 'deleted_at' ], {
|
||
name: 'idx_enabled_deleted',
|
||
});
|
||
await queryInterface.addIndex(TABLE_PLAN, [ 'deleted_at' ], {
|
||
name: 'idx_deleted_at',
|
||
});
|
||
}
|
||
|
||
if (!tableNames.includes(TABLE_LOG)) {
|
||
await queryInterface.createTable(TABLE_LOG, {
|
||
id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键',
|
||
},
|
||
warehouse_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '仓库ID(生成报表时注入的仓库维度)',
|
||
},
|
||
store_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '货主ID(wms_store.id)',
|
||
},
|
||
report_code: {
|
||
type: STRING(100),
|
||
allowNull: false,
|
||
comment: '报表编码(wms_report_definition.code)',
|
||
},
|
||
biz_date: {
|
||
type: DATEONLY,
|
||
allowNull: false,
|
||
comment: '数据日期(T-1 自然日)',
|
||
},
|
||
time_point: {
|
||
type: STRING(5),
|
||
allowNull: false,
|
||
comment: '命中的投递时间点(HH:mm)',
|
||
},
|
||
status: {
|
||
type: STRING(20),
|
||
allowNull: false,
|
||
defaultValue: 'pending',
|
||
comment: '状态:pending/exporting/delivered/failed/cancelled',
|
||
},
|
||
channel: {
|
||
type: STRING(20),
|
||
allowNull: false,
|
||
defaultValue: 'wecom',
|
||
comment: '投递渠道快照:wecom=企业微信 inbox=站内信群 link=链接',
|
||
},
|
||
file_url: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '报表文件下载链接(COS 带有效期)',
|
||
},
|
||
retry_count: {
|
||
type: INTEGER(3),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '重试次数',
|
||
},
|
||
next_retry_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '下次可投递时间(失败退避)',
|
||
},
|
||
error_message: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '最近一次失败原因',
|
||
},
|
||
delivered_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '投递成功时间',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
}, {
|
||
comment: '报表投递记录表',
|
||
engine: 'InnoDB',
|
||
charset: 'utf8mb4',
|
||
collate: 'utf8mb4_general_ci',
|
||
});
|
||
|
||
await queryInterface.addIndex(TABLE_LOG, [ 'store_id', 'report_code', 'biz_date', 'time_point', 'deleted_at' ], {
|
||
unique: true,
|
||
name: 'uk_store_report_biz_time',
|
||
});
|
||
await queryInterface.addIndex(TABLE_LOG, [ 'status', 'id' ], {
|
||
name: 'idx_status_id',
|
||
});
|
||
await queryInterface.addIndex(TABLE_LOG, [ 'status', 'updated_at' ], {
|
||
name: 'idx_status_updated_at',
|
||
});
|
||
await queryInterface.addIndex(TABLE_LOG, [ 'deleted_at' ], {
|
||
name: 'idx_deleted_at',
|
||
});
|
||
}
|
||
},
|
||
|
||
async down(queryInterface) {
|
||
await queryInterface.dropTable(TABLE_LOG);
|
||
await queryInterface.dropTable(TABLE_PLAN);
|
||
await queryInterface.dropTable(TABLE_DEFINITION);
|
||
},
|
||
};
|