feat(receiving): 审核通过通知返回 Excel 审核明细下载链接,失败回退正文展示明细

This commit is contained in:
liufumingren 2026-08-26 17:40:49 +08:00
parent c5477cf6cb
commit adb7405396
2 changed files with 123 additions and 52 deletions

View File

@ -1190,33 +1190,33 @@ class ReceivingService extends Service {
reviewer: reviewerName,
});
// 1. 发送文字通知
// 0. 加载明细并反查货主商品编码(通知文案与报表共用,失败不影响主流程)
let details = [];
let itemCodeMap = new Map();
try {
const textResult = await this.sendReviewNotice({ receiving: receivingSnapshot, reviewer });
ctx.logger.info('[receiving.reviewReceiving] 文字通知结果', {
details = await ctx.model.ReceivingDetail.findAll({
where: { receiving_id: r.id },
});
itemCodeMap = await this.resolveReviewDetailItemCodeMap(details);
} catch (detailErr) {
ctx.logger.error('[receiving.reviewReceiving] 加载明细失败', {
receiving_id: r.id,
receiving_number: r.receiving_number,
success: !!(textResult && textResult.success),
message: textResult && textResult.message,
matchedHookCount: Number(textResult && textResult.data && textResult.data.matchedHookCount || 0),
sentCount: Number(textResult && textResult.data && textResult.data.sentCount || 0),
failedCount: Number(textResult && textResult.data && textResult.data.failedCount || 0),
});
} catch (textErr) {
ctx.logger.error('[receiving.reviewReceiving] 文字通知发送失败', {
receiving_id: r.id,
error: textErr.message,
error: detailErr.message,
});
}
// 2. 加载明细与图片,生成 Excel 报表
// 1. 加载明细与图片,生成 Excel 报表(复用步骤 0 已加载的明细,为空时回退重新查询)
let reviewReport = null;
try {
const details = await ctx.model.ReceivingDetail.findAll({
where: { receiving_id: r.id },
});
let reportDetails = details;
if (reportDetails.length === 0) {
reportDetails = await ctx.model.ReceivingDetail.findAll({
where: { receiving_id: r.id },
});
}
const detailIds = details.map(d => String(d.id));
const detailIds = reportDetails.map(d => String(d.id));
const detailImageMap = new Map();
if (detailIds.length > 0) {
const detailImageRefs = await ctx.model.FileReference.findAll({
@ -1239,10 +1239,10 @@ class ReceivingService extends Service {
});
const stats = {
total_details: details.length,
matched_details: details.filter(d => d.system_product_id).length,
unmatched_details: details.filter(d => !d.system_product_id).length,
total_quantity: details.reduce((sum, d) => sum + (d.actual_quantity || 0), 0),
total_details: reportDetails.length,
matched_details: reportDetails.filter(d => d.system_product_id).length,
unmatched_details: reportDetails.filter(d => !d.system_product_id).length,
total_quantity: reportDetails.reduce((sum, d) => sum + (d.actual_quantity || 0), 0),
};
const excelService = ctx.service && ctx.service.systems && ctx.service.systems.excel;
@ -1257,7 +1257,7 @@ class ReceivingService extends Service {
businessId: r.id,
expireDays: 90,
sheets: this.buildReviewReportSheets({
receiving: { ...receivingSnapshot, details },
receiving: { ...receivingSnapshot, details: reportDetails },
reviewer,
stats,
detailImageMap,
@ -1273,32 +1273,37 @@ class ReceivingService extends Service {
url: reviewReport && (reviewReport.url || reviewReport.cdnUrl || reviewReport.cosUrl || ''),
});
} catch (reportErr) {
ctx.logger.error('[receiving.reviewReceiving] Excel报表生成失败,跳过文件通知', {
ctx.logger.error('[receiving.reviewReceiving] Excel报表生成失败,回退正文展示明细列表', {
receiving_id: r.id,
error: reportErr.message,
});
}
// 3. 发送文件通知(仅当 Excel 上传成功时)
if (reviewReport) {
try {
const fileResult = await this.sendReviewNotice({ receiving: receivingSnapshot, reviewer, reviewReport });
ctx.logger.info('[receiving.reviewReceiving] 文件通知结果', {
receiving_id: r.id,
receiving_number: r.receiving_number,
success: !!(fileResult && fileResult.success),
message: fileResult && fileResult.message,
matchedHookCount: Number(fileResult && fileResult.data && fileResult.data.matchedHookCount || 0),
sentCount: Number(fileResult && fileResult.data && fileResult.data.sentCount || 0),
failedCount: Number(fileResult && fileResult.data && fileResult.data.failedCount || 0),
reportUrl: reviewReport.url || reviewReport.cdnUrl || reviewReport.cosUrl || '',
});
} catch (fileErr) {
ctx.logger.error('[receiving.reviewReceiving] 文件通知发送失败', {
receiving_id: r.id,
error: fileErr.message,
});
}
// 2. 发送审核完成通知:Excel 生成成功直接带下载链接,失败则回退正文展示全部明细
try {
const noticeResult = await this.sendReviewNotice({
receiving: receivingSnapshot,
reviewer,
reviewReport,
details,
itemCodeMap,
});
ctx.logger.info('[receiving.reviewReceiving] 审核完成通知结果', {
receiving_id: r.id,
receiving_number: r.receiving_number,
hasReport: !!reviewReport,
success: !!(noticeResult && noticeResult.success),
message: noticeResult && noticeResult.message,
matchedHookCount: Number(noticeResult && noticeResult.data && noticeResult.data.matchedHookCount || 0),
sentCount: Number(noticeResult && noticeResult.data && noticeResult.data.sentCount || 0),
failedCount: Number(noticeResult && noticeResult.data && noticeResult.data.failedCount || 0),
reportUrl: reviewReport && (reviewReport.url || reviewReport.cdnUrl || reviewReport.cosUrl || ''),
});
} catch (noticeErr) {
ctx.logger.error('[receiving.reviewReceiving] 审核完成通知发送失败', {
receiving_id: r.id,
error: noticeErr.message,
});
}
ctx.logger.info('[receiving.reviewReceiving] 单据通知处理完成', {
@ -1573,12 +1578,16 @@ class ReceivingService extends Service {
* @param {Object} options.receiving 收货单快照
* @param {Object} [options.reviewer={}] 审核人信息
* @param {Object|null} [options.reviewReport=null] 审核报表(有值则发文件通知)
* @param {Array<Object>|null} [options.details=null] 收货明细列表(用于通知文案展示全部明细)
* @param {Map<number, string>|null} [options.itemCodeMap=null] system_product_id → item_code 映射
* @return {Promise<Object|null>} 发送结果
*/
async sendReviewNotice({
receiving,
reviewer = {},
reviewReport = null,
details = null,
itemCodeMap = null,
} = {}) {
const { ctx } = this;
const rawNoticeService = ctx && ctx.service && ctx.service.systems && ctx.service.systems.notice;
@ -1603,13 +1612,23 @@ class ReceivingService extends Service {
(reviewReport && (reviewReport.filename || reviewReport.fileKey || reviewReport.key)) || `审核明细_${receivingNumber}.xlsx`
).trim();
const content = [
`收货单号:${receivingNumber}`,
`货主:${receiving.store_name || '未知'}`,
`审核人:${reviewerName || '未知'}`,
`审核时间:${this.formatReportDate(receiving.updated_at || new Date())}`,
`备注:${receiving.remark || '无'}`,
].join('\n') + '。';
const detailList = Array.isArray(details) ? details : [];
const codeMap = itemCodeMap instanceof Map ? itemCodeMap : new Map();
const productLines = detailList.map((detail, index) => {
const stockId = Number(detail.system_product_id || 0);
const itemCode = (stockId > 0 && codeMap.get(stockId)) || String(detail.barcode || '').trim() || '未知编码';
const productName = String(detail.product_name || '').trim() || '未知商品';
const quantity = Number(detail.actual_quantity || 0);
return `${index + 1}.${itemCode} ${productName} ${quantity}`;
});
const contentLines = [ `收货单号:${receivingNumber} 完成收货审核` ];
if (reportUrl) {
contentLines.push('', `审核明细下载:${reportUrl}`);
} else if (productLines.length > 0) {
contentLines.push('', '商品:', ...productLines);
}
contentLines.push('', '请及时提交入库单入库');
const content = contentLines.join('\n') + '。';
return await noticeService.send({
warehouseId,
@ -1618,7 +1637,7 @@ class ReceivingService extends Service {
businessName: 'receiving',
event: 'receiving_reviewed',
channel: 'warehouse',
title: `收货单${receivingNumber}审核完成`,
title: `收货单${receivingNumber}完成收货审核`,
content,
url: reportUrl || undefined,
fileName: reportUrl ? reportFileName : '',
@ -1642,6 +1661,42 @@ class ReceivingService extends Service {
});
}
/**
* 反查货主商品编码:收货明细 system_product_id 对应 wms_stock_store.stock_id,取 item_code 作为商品编码。
* @param {Array<Object>} details 收货明细列表(含 system_product_id)
* @return {Promise<Map<number, string>>} system_product_id → item_code
*/
async resolveReviewDetailItemCodeMap(details = []) {
const { ctx, app } = this;
const { Op } = app.Sequelize;
const codeMap = new Map();
if (!Array.isArray(details) || details.length === 0) return codeMap;
const stockIds = [ ...new Set(
details.map(detail => Number(detail.system_product_id || 0)).filter(id => Number.isFinite(id) && id > 0)
) ];
if (stockIds.length === 0) return codeMap;
const rows = await ctx.model.StockStore.findAll({
where: {
stock_id: { [Op.in]: stockIds },
delete_at: null,
},
attributes: [ 'stock_id', 'item_code' ],
order: [[ 'id', 'ASC' ]],
raw: true,
});
rows.forEach(row => {
const stockId = Number(row.stock_id);
if (!stockId || codeMap.has(stockId)) return;
const itemCode = String(row.item_code || '').trim();
if (itemCode) codeMap.set(stockId, itemCode);
});
return codeMap;
}
isEmptyValue(value) {
if (value === undefined || value === null) {
return true;

View File

@ -270,5 +270,21 @@
"触发点:completeReceiving 事务提交后,unmatchedDetails 非空且已绑定货主时发送,失败不影响主流程"
],
"module": "receiving"
},
{
"version": "2026-08-26-warehouse-03",
"date": "2026-08-26",
"type": "changed",
"summary": "审核通过通知(receiving_reviewed)改为返回 Excel 审核明细下载链接,Excel 生成失败时回退正文展示全部明细(item_code 反查 stock_store)",
"methods": [
"notify.reply"
],
"notes": [
"storage/receiving.js 新增 resolveReviewDetailItemCodeMap:批量反查 wms_stock_store(stock_id=明细 system_product_id,delete_at 为空),返回 system_product_id → item_code 映射",
"sendReviewNotice 内容改为「收货单号:Rxx 完成收货审核 / 审核明细下载:Excel链接 / 请及时提交入库单入库」,标题改为完成收货审核;Excel 生成失败时回退正文展示全部明细(1.商品编码 名称 数量)",
"reviewReceiving 调整为先生成 Excel 再发一次通知(成功带下载链接,失败回退文字),替代原「先文字后文件」两次发送",
"item_code 反查不到时回退明细条码,仍无则显示未知编码"
],
"module": "receiving"
}
]