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

View File

@ -270,5 +270,21 @@
"触发点:completeReceiving 事务提交后,unmatchedDetails 非空且已绑定货主时发送,失败不影响主流程" "触发点:completeReceiving 事务提交后,unmatchedDetails 非空且已绑定货主时发送,失败不影响主流程"
], ],
"module": "receiving" "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"
} }
] ]