183 lines
5.3 KiB
JavaScript
183 lines
5.3 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_inbox_message';
|
||
|
||
module.exports = {
|
||
/**
|
||
* 创建站内信消息表。
|
||
* @param {Object} queryInterface Sequelize QueryInterface 实例。
|
||
* @param {Object} Sequelize Sequelize 类。
|
||
* @return {Promise<void>} 迁移执行结果。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const tables = await queryInterface.showAllTables();
|
||
const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME);
|
||
if (exists) return;
|
||
|
||
await queryInterface.createTable(TABLE_NAME, {
|
||
id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
ext_id: {
|
||
type: Sequelize.STRING(128),
|
||
allowNull: false,
|
||
comment: '外部唯一消息主键(IM msgId / API requestId 等),用于幂等去重',
|
||
},
|
||
warehouse_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '所属仓库ID',
|
||
},
|
||
store_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '关联货主ID',
|
||
},
|
||
direction: {
|
||
type: Sequelize.STRING(10),
|
||
allowNull: false,
|
||
defaultValue: 'inbound',
|
||
comment: '消息方向:inbound=外部→WMS收件 / outbound=WMS→外部回信',
|
||
},
|
||
parent_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '关联消息ID(回信时指向原消息,形成对话链)',
|
||
},
|
||
source_type: {
|
||
type: Sequelize.STRING(20),
|
||
allowNull: false,
|
||
comment: '来源类型:im / api',
|
||
},
|
||
source_extra: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: true,
|
||
comment: '来源附加信息(IM 平台 / API app_id)',
|
||
},
|
||
target_type: {
|
||
type: Sequelize.STRING(20),
|
||
allowNull: false,
|
||
comment: '目标类型:im / api',
|
||
},
|
||
target_channel: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: false,
|
||
comment: '目标渠道:wecom / dingtalk / feishu / warehouse_api / webhook',
|
||
},
|
||
target_url: {
|
||
type: Sequelize.STRING(500),
|
||
allowNull: false,
|
||
comment: '推送目标URL(IM Bot webhook 地址 / API 回调地址)',
|
||
},
|
||
ref_code: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: true,
|
||
comment: '外部引用编码,推送/回执时原样携带',
|
||
},
|
||
ext_data: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '外部透传数据,外部传入任意JSON,WMS不解析,回信时原样返回',
|
||
},
|
||
flags: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '处理标记,外部传入控制WMS处理行为',
|
||
},
|
||
push_status: {
|
||
type: Sequelize.TINYINT(4),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '推送状态:0=待推送 1=推送中 2=已送达 3=失败',
|
||
},
|
||
push_started_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '开始推送时间(僵尸检测:超5分钟仍为1则重置为0)',
|
||
},
|
||
retry_count: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '已重试次数',
|
||
},
|
||
push_response: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '目标响应(http_status, body, error)',
|
||
},
|
||
pushed_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '推送完成时间',
|
||
},
|
||
work_order_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '关联工单ID(预留)',
|
||
},
|
||
work_order_no: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: true,
|
||
comment: '工单号(预留)',
|
||
},
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
});
|
||
|
||
// 唯一索引 — 外部幂等去重
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'ext_id' ], {
|
||
unique: true,
|
||
name: 'uk_ext_id',
|
||
});
|
||
|
||
// 仓库+状态查询
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'push_status' ], {
|
||
name: 'idx_warehouse_status',
|
||
});
|
||
|
||
// 仓库+方向+时间
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'direction', 'created_at' ], {
|
||
name: 'idx_warehouse_direction',
|
||
});
|
||
|
||
// 对话链查询
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'parent_id' ], {
|
||
name: 'idx_parent',
|
||
});
|
||
|
||
// 时间范围查询
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'created_at' ], {
|
||
name: 'idx_created_at',
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 回滚站内信消息表。
|
||
* @param {Object} queryInterface Sequelize QueryInterface 实例。
|
||
* @return {Promise<void>} 迁移回滚结果。
|
||
*/
|
||
down: async queryInterface => {
|
||
await queryInterface.dropTable(TABLE_NAME);
|
||
},
|
||
};
|