135 lines
3.9 KiB
JavaScript
135 lines
3.9 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 报表投递记录 Model。
|
||
* 状态机 pending → exporting → delivered/failed/cancelled;
|
||
* 失败按退避重试(retry_count/next_retry_at),幂等键
|
||
* uk_store_report_biz_time 保证同一货主同一报表定义同一数据日期同一时间点不重复投递;
|
||
* report_definition_id 关联报表定义,report_name 为投递时报表名称快照(审计回放)。
|
||
*
|
||
* @param {import('egg').Application} app Egg 应用实例。
|
||
* @return {import('sequelize').ModelCtor<import('sequelize').Model>} Sequelize 模型。
|
||
*/
|
||
module.exports = app => {
|
||
const { BIGINT, INTEGER, STRING, DATE, DATEONLY } = app.Sequelize;
|
||
|
||
const ReportDeliveryLog = app.model.define('report_delivery_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_definition_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '报表定义ID(wms_report_definition.id)',
|
||
},
|
||
report_name: {
|
||
type: STRING(100),
|
||
allowNull: true,
|
||
comment: '报表名称快照(投递时的定义 name,审计回放)',
|
||
},
|
||
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: 'inbox',
|
||
comment: '投递渠道快照:inbox=站内信群(wecom/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: '投递成功时间',
|
||
},
|
||
notify_targets: {
|
||
type: app.Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '投递目标快照:{group_ids:[],member_ids:[]}(本次实际发送的群/成员,审计回放)',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
}, {
|
||
tableName: 'wms_report_delivery_log',
|
||
freezeTableName: true,
|
||
underscored: true,
|
||
timestamps: true,
|
||
createdAt: 'created_at',
|
||
updatedAt: 'updated_at',
|
||
deletedAt: 'deleted_at',
|
||
paranoid: true,
|
||
comment: '报表投递记录表',
|
||
indexes: [
|
||
{ name: 'uk_store_report_biz_time', unique: true, fields: [ 'store_id', 'report_definition_id', 'biz_date', 'time_point', 'deleted_at' ] },
|
||
{ name: 'idx_status_id', fields: [ 'status', 'id' ] },
|
||
{ name: 'idx_status_updated_at', fields: [ 'status', 'updated_at' ] },
|
||
{ name: 'idx_deleted_at', fields: [ 'deleted_at' ] },
|
||
],
|
||
});
|
||
|
||
return ReportDeliveryLog;
|
||
};
|