fix(report): 报表文件消息只投递计划配置通知群,未配置回退货主默认站内信群,配置群不可达进入失败退避重试
This commit is contained in:
parent
b64ba0b00b
commit
b91c3f4124
@ -927,19 +927,55 @@ class ReportDeliveryService extends Service {
|
||||
return `【${reportName}】${bizDate} 数据报表已生成,点击查看:${fileUrl}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析计划配置的通知群(notify_group_ids → 有效群行)。
|
||||
* 过滤口径与 sendNotifyIfConfigured 一致:is_blocked=0、软删自动排除、仓库维度一致。
|
||||
* @param {Object} options 入参
|
||||
* @param {Object} options.plan 投递计划
|
||||
* @param {number} options.warehouseId 仓库ID
|
||||
* @return {Promise<Array<{id: number, group_id: string, group_name: string, ext_data: Object|null}>>}
|
||||
* 有效群行列表(未配置群或全部被过滤时返回空数组)
|
||||
* @throws {Error} 站内信群模型不可用时抛出
|
||||
*/
|
||||
async resolveNotifyGroups({ plan, warehouseId }) {
|
||||
const { ctx } = this;
|
||||
const groupIds = this.readJsonArray(plan && plan.notify_group_ids) || [];
|
||||
if (groupIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const inboxGroupModel = ctx.model && ctx.model.InboxGroup;
|
||||
if (!inboxGroupModel || typeof inboxGroupModel.findAll !== 'function') {
|
||||
throw new Error('站内信群模型 InboxGroup 不可用,无法解析通知群');
|
||||
}
|
||||
const { Op } = ctx.app.Sequelize;
|
||||
const rows = await inboxGroupModel.findAll({
|
||||
where: {
|
||||
id: { [Op.in]: groupIds },
|
||||
warehouse_id: Number(warehouseId) || 0,
|
||||
is_blocked: 0,
|
||||
},
|
||||
attributes: [ 'id', 'group_id', 'group_name', 'ext_data' ],
|
||||
raw: true,
|
||||
});
|
||||
return Array.isArray(rows) ? rows : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 按渠道投递(渠道已收敛,仅支持 inbox 站内信群推送)。
|
||||
*
|
||||
* 规则:
|
||||
* 1. 历史计划残留 wecom/link 等已弃用渠道时,按 inbox 兜底执行并记录 warn 日志
|
||||
* (不直接抛错:避免历史计划在 failed 退避循环中耗尽重试次数后永久卡死);
|
||||
* 2. 推送失败(success=false 或消息记录回写 push_status=3)时,立即复用
|
||||
* 2. 群路由(缺陷 G1 修复):报表文件消息只投递到计划配置的通知群(notify_group_ids),
|
||||
* 不再投递到任意「货主默认群」,避免未配置投递的群收到推送;
|
||||
* 未配置通知群的历史计划保持旧行为,回退投递到货主默认站内信群(group_type=store);
|
||||
* 3. 推送失败(success=false 或消息记录回写 push_status=3)时,立即复用
|
||||
* ctx.service.inbox.message.retry 重推一次(重置 push_status=0 异步重推,
|
||||
* ext_id 幂等复用同一条消息记录,不重复写);
|
||||
* 3. 重推仍失败(retry 自身抛错)时向上抛错,交由上层 failed 退避循环
|
||||
* 4. 重推仍失败(retry 自身抛错)时向上抛错,交由上层 failed 退避循环
|
||||
* (MAX_RETRY=3,退避 = retry_count × 60s);
|
||||
* 4. 完成后按计划配置附带发送文本通知(与投递渠道独立);
|
||||
* 5. 幂等键含货主维度(report:{store_id}:{report_definition_id}:{biz_date}:{time_point}),
|
||||
* 5. 完成后按计划配置附带发送文本通知(与投递渠道独立);
|
||||
* 6. 幂等键含货主维度(report:{store_id}:{report_definition_id}:{biz_date}:{time_point}),
|
||||
* 不同货主的投递消息互不复用。
|
||||
*
|
||||
* @param {Object} options 入参
|
||||
@ -969,84 +1005,102 @@ class ReportDeliveryService extends Service {
|
||||
);
|
||||
}
|
||||
|
||||
// inbox:站内信群推送(唯一渠道)
|
||||
const inboxGroupModel = ctx.model && ctx.model.InboxGroup;
|
||||
if (!inboxGroupModel || typeof inboxGroupModel.findOne !== 'function') {
|
||||
throw new Error('站内信群模型 InboxGroup 不可用');
|
||||
}
|
||||
const groupRow = await inboxGroupModel.findOne({
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
group_type: 'store',
|
||||
is_blocked: 0,
|
||||
},
|
||||
attributes: [ 'group_id', 'group_name', 'ext_data' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
});
|
||||
if (!groupRow || !groupRow.group_id) {
|
||||
throw new Error(`货主 ${storeId} 未配置站内信群,无法投递`);
|
||||
// inbox:站内信群推送(唯一渠道)。
|
||||
// 群路由(缺陷 G1 修复):报表文件消息只投递到计划配置的通知群,不再投递到任意「货主默认群」;
|
||||
// 未配置通知群的历史计划保持旧行为,回退到货主默认站内信群(group_type=store,取最早一条)。
|
||||
const configuredGroupIds = this.readJsonArray(plan && plan.notify_group_ids) || [];
|
||||
let targetGroups;
|
||||
if (configuredGroupIds.length > 0) {
|
||||
targetGroups = await this.resolveNotifyGroups({ plan, warehouseId });
|
||||
if (targetGroups.length === 0) {
|
||||
throw new Error(`货主 ${storeId} 配置的通知群不可达(不存在、已删除、被屏蔽或仓库不一致),无法投递`);
|
||||
}
|
||||
} else {
|
||||
const inboxGroupModel = ctx.model && ctx.model.InboxGroup;
|
||||
if (!inboxGroupModel || typeof inboxGroupModel.findOne !== 'function') {
|
||||
throw new Error('站内信群模型 InboxGroup 不可用');
|
||||
}
|
||||
const groupRow = await inboxGroupModel.findOne({
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
store_id: storeId,
|
||||
group_type: 'store',
|
||||
is_blocked: 0,
|
||||
},
|
||||
attributes: [ 'group_id', 'group_name', 'ext_data' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
});
|
||||
if (!groupRow || !groupRow.group_id) {
|
||||
throw new Error(`货主 ${storeId} 未配置站内信群,无法投递`);
|
||||
}
|
||||
targetGroups = [ groupRow ];
|
||||
}
|
||||
|
||||
const warehouseNotify = ctx.service && ctx.service.systems && ctx.service.systems.warehouseNotify;
|
||||
if (!warehouseNotify || typeof warehouseNotify.pushSystemMessage !== 'function') {
|
||||
throw new Error('通知推送服务 warehouseNotify.pushSystemMessage 不可用');
|
||||
}
|
||||
const pushResult = await warehouseNotify.pushSystemMessage({
|
||||
warehouseId,
|
||||
storeId,
|
||||
group: {
|
||||
group_id: groupRow.group_id,
|
||||
group_name: groupRow.group_name || '货主群',
|
||||
ext_data: groupRow.ext_data || null,
|
||||
},
|
||||
content: this.buildMessageContent(definition, bizDate, fileUrl),
|
||||
extId: idempotentKey,
|
||||
extData: {
|
||||
report_definition_id: reportDefinitionId,
|
||||
biz_date: bizDate,
|
||||
time_point: timePoint,
|
||||
file_url: fileUrl,
|
||||
},
|
||||
});
|
||||
|
||||
// 推送失败判定(双保险):success=false;或消息记录回写 push_status=3(推送链路异常时兜底识别)
|
||||
const messageId = (pushResult && Number(pushResult.messageId)) || 0;
|
||||
let pushFailed = !pushResult || pushResult.success === false;
|
||||
if (!pushFailed && messageId > 0) {
|
||||
const InboxMessage = ctx.model && ctx.model.InboxMessage;
|
||||
if (InboxMessage && typeof InboxMessage.findOne === 'function') {
|
||||
try {
|
||||
const row = await InboxMessage.findOne({
|
||||
where: { id: messageId },
|
||||
attributes: [ 'push_status' ],
|
||||
raw: true,
|
||||
});
|
||||
if (row && Number(row.push_status) === 3) {
|
||||
pushFailed = true;
|
||||
}
|
||||
} catch (statusErr) {
|
||||
ctx.logger.warn(`[reportDelivery] 读取消息推送状态失败: ${statusErr.message}`);
|
||||
}
|
||||
}
|
||||
const pushResults = [];
|
||||
for (const groupRow of targetGroups) {
|
||||
const pushResult = await warehouseNotify.pushSystemMessage({
|
||||
warehouseId,
|
||||
storeId,
|
||||
group: {
|
||||
group_id: groupRow.group_id,
|
||||
group_name: groupRow.group_name || '货主群',
|
||||
ext_data: groupRow.ext_data || null,
|
||||
},
|
||||
content: this.buildMessageContent(definition, bizDate, fileUrl),
|
||||
extId: idempotentKey,
|
||||
extData: {
|
||||
report_definition_id: reportDefinitionId,
|
||||
biz_date: bizDate,
|
||||
time_point: timePoint,
|
||||
file_url: fileUrl,
|
||||
},
|
||||
});
|
||||
pushResults.push(pushResult);
|
||||
}
|
||||
|
||||
if (pushFailed) {
|
||||
const reason = (pushResult && (pushResult.reason || pushResult.error)) || '站内信推送失败';
|
||||
// 立即重推一次:复用 inbox.message.retry(重置 push_status=0 并异步重推;ext_id 幂等复用
|
||||
// 同一条消息记录,不重复写)。retry 自身抛错视为重推失败,向上抛出进入 failed 退避循环
|
||||
const retryService = ctx.service && ctx.service.inbox && ctx.service.inbox.message;
|
||||
if (messageId > 0 && retryService && typeof retryService.retry === 'function') {
|
||||
try {
|
||||
await retryService.retry({ warehouseId, id: messageId });
|
||||
ctx.logger.warn(`[reportDelivery] 站内信推送失败已受理立即重推(message_id=${messageId}): ${reason}`);
|
||||
} catch (retryErr) {
|
||||
const retryReason = retryErr && retryErr.message ? retryErr.message : `${retryErr}`;
|
||||
throw new Error(`站内信推送失败且重推失败: ${reason} / ${retryReason}`);
|
||||
// 推送失败判定(双保险):success=false;或消息记录回写 push_status=3(推送链路异常时兜底识别)
|
||||
const retryService = ctx.service && ctx.service.inbox && ctx.service.inbox.message;
|
||||
for (const pushResult of pushResults) {
|
||||
const messageId = (pushResult && Number(pushResult.messageId)) || 0;
|
||||
let pushFailed = !pushResult || pushResult.success === false;
|
||||
if (!pushFailed && messageId > 0) {
|
||||
const InboxMessage = ctx.model && ctx.model.InboxMessage;
|
||||
if (InboxMessage && typeof InboxMessage.findOne === 'function') {
|
||||
try {
|
||||
const row = await InboxMessage.findOne({
|
||||
where: { id: messageId },
|
||||
attributes: [ 'push_status' ],
|
||||
raw: true,
|
||||
});
|
||||
if (row && Number(row.push_status) === 3) {
|
||||
pushFailed = true;
|
||||
}
|
||||
} catch (statusErr) {
|
||||
ctx.logger.warn(`[reportDelivery] 读取消息推送状态失败: ${statusErr.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pushFailed) {
|
||||
const reason = (pushResult && (pushResult.reason || pushResult.error)) || '站内信推送失败';
|
||||
// 立即重推一次:复用 inbox.message.retry(重置 push_status=0 并异步重推;ext_id 幂等复用
|
||||
// 同一条消息记录,不重复写)。retry 自身抛错视为重推失败,向上抛出进入 failed 退避循环
|
||||
if (messageId > 0 && retryService && typeof retryService.retry === 'function') {
|
||||
try {
|
||||
await retryService.retry({ warehouseId, id: messageId });
|
||||
ctx.logger.warn(`[reportDelivery] 站内信推送失败已受理立即重推(message_id=${messageId}): ${reason}`);
|
||||
} catch (retryErr) {
|
||||
const retryReason = retryErr && retryErr.message ? retryErr.message : `${retryErr}`;
|
||||
throw new Error(`站内信推送失败且重推失败: ${reason} / ${retryReason}`);
|
||||
}
|
||||
} else {
|
||||
throw new Error(reason);
|
||||
}
|
||||
} else {
|
||||
throw new Error(reason);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1110,25 +1164,11 @@ class ReportDeliveryService extends Service {
|
||||
bizDate,
|
||||
});
|
||||
|
||||
// 目标解析:内部群ID → 外部群信息(is_blocked=0、软删排除、仓库维度一致)
|
||||
let groups = [];
|
||||
if (groupIds.length > 0) {
|
||||
const inboxGroupModel = ctx.model && ctx.model.InboxGroup;
|
||||
if (!inboxGroupModel || typeof inboxGroupModel.findAll !== 'function') {
|
||||
throw new Error('站内信群模型 InboxGroup 不可用,无法发送文本通知');
|
||||
}
|
||||
const { Op } = ctx.app.Sequelize;
|
||||
const rows = await inboxGroupModel.findAll({
|
||||
where: {
|
||||
id: { [Op.in]: groupIds },
|
||||
warehouse_id: Number(warehouseId) || 0,
|
||||
is_blocked: 0,
|
||||
},
|
||||
attributes: [ 'id', 'group_id', 'group_name', 'ext_data' ],
|
||||
raw: true,
|
||||
});
|
||||
groups = Array.isArray(rows) ? rows : [];
|
||||
}
|
||||
// 目标解析:内部群ID → 外部群信息(is_blocked=0、软删排除、仓库维度一致),
|
||||
// 复用 resolveNotifyGroups 与渠道投递保持同一解析口径
|
||||
const groups = groupIds.length > 0
|
||||
? await this.resolveNotifyGroups({ plan, warehouseId })
|
||||
: [];
|
||||
|
||||
if (groups.length === 0 && memberIds.length === 0) {
|
||||
throw new Error('文本通知目标不可达(通知群不存在、已删除、被屏蔽或未选择群/成员)');
|
||||
|
||||
@ -164,6 +164,12 @@ report.generate(method 层,异步任务,入参 report_id)
|
||||
修复前不含 `store_id`,不同货主的投递/通知消息会互相复用、串扰。
|
||||
- **货主维度强校验**(2026-08-31):`processOne` 对 `store_id`/`report_definition_id` 强校验,
|
||||
任一无效直接抛错「投递计划维度非法(…),拒绝生成报表」,绝不生成跨货主报表。
|
||||
- **文件消息投递目标与群路由口径**(2026-09-01 修复 G1 缺陷):报表文件消息(inbox 渠道)
|
||||
**只投递到计划配置的通知群 `notify_group_ids`**,不再投递任意「货主默认群」;
|
||||
未配置通知群的历史计划保持旧行为,回退投递到货主默认站内信群(`group_type='store'`,按 id ASC 取最早一条)。
|
||||
配置了群但全部不可达(不存在/已删除/被屏蔽/仓库不一致)时直接抛错「配置的通知群不可达…无法投递」
|
||||
进入 `failed` 退避重试(`MAX_RETRY=3`,退避 `retry_count × 60s`),不再静默回退到未配置群。
|
||||
投递与文本通知共用 `resolveNotifyGroups` 解析口径(过滤 `is_blocked=1`、软删、仓库维度一致)。
|
||||
- **失败自动重推**:inbox 推送失败(`success=false` 或消息回写 `push_status=3`)时,立即复用
|
||||
`inbox.message.retry` **重推一次**(`ext_id` 幂等复用同一条消息记录,不重复写);重推仍失败进入
|
||||
`failed` 退避重试:`MAX_RETRY=3`,退避 `retry_count × 60s`;重试扫描条件
|
||||
@ -195,6 +201,10 @@ report.generate(method 层,异步任务,入参 report_id)
|
||||
- 元素为 `wms_inbox_group.id`(内部群 ID),**正整数**、自动去重并升序,最多 **20** 个。
|
||||
- 保存时校验存在性:软删(已删除)的群报错并**列出缺失 ID**;被屏蔽(`is_blocked=1`)的群仍可选中,
|
||||
屏蔽属于发送时过滤维度。
|
||||
- **同时为报表文件消息的投递目标**:计划配置了 `notify_group_ids` 时,文件消息(inbox 渠道)
|
||||
**仅逐群投递到这些配置群**,不再投递任意「货主默认群」;未配置通知群的历史计划回退投递到
|
||||
货主默认站内信群(`group_type='store'`,取最早一条);配置的群全部不可达
|
||||
(不存在/已删除/被屏蔽/仓库不一致)时投递抛错进入 `failed` 退避重试。
|
||||
- 发送时逐群推送(群维度协议字段 `group_id`/`group_name` 为单值),并再次过滤
|
||||
`is_blocked=1`、软删、仓库维度不一致的群;此类群不发送且不进入投递记录快照。
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user