feat(inbound): 入库单完成入库通知货主文案升级,明细行含商品信息,查询失败降级不影响主流程
This commit is contained in:
parent
2597faac4c
commit
277e80e8ca
@ -1238,10 +1238,14 @@ class InboundDirectLogService extends Service {
|
||||
|
||||
/**
|
||||
* 发送入库单完成入库通知(entry_order_completed)。
|
||||
* 文案固定为:入库单号{入库单号}已完成入库,请注意查收。
|
||||
* 文案格式:
|
||||
* 首行「入库单号{入库单号}」;
|
||||
* 明细行「{商品名称} {商家编码} {规格} 数量:{数量}」,行间以「---」分隔;
|
||||
* 末尾行「已入库完毕」。
|
||||
* 明细通过入库单ID查询 wms_entry_items 及关联商品数据,仅展示已入库数量大于 0 的行。
|
||||
* 路由由 notice 固定订阅回退链处理:货主收货群 → 无则货主售后群 → 均无则仓库异常群。
|
||||
* 通知失败不影响入库主流程(由调用方 try/catch 包裹)。
|
||||
* @param {Object} entryOrder 入库单快照(含 entry_order_code / warehouse_id / store_id)
|
||||
* @param {Object} entryOrder 入库单快照(含 id / entry_order_code / warehouse_id / store_id)
|
||||
* @return {Promise<Object|null>} 通知发送结果,通知服务不可用或参数不完整时返回 null
|
||||
*/
|
||||
async notifyEntryOrderCompleted(entryOrder = {}) {
|
||||
@ -1271,7 +1275,22 @@ class InboundDirectLogService extends Service {
|
||||
const entryOrderCode = String(entryOrderCodeRaw || id || '').trim();
|
||||
if (!entryOrderCode) return null;
|
||||
|
||||
const content = `入库单号${entryOrderCode}已完成入库,请注意查收`;
|
||||
// 组装明细文案:明细查询失败时降级为无明细行,仍发送首行 + 结尾行,保证通知可达
|
||||
let detailLines = [];
|
||||
try {
|
||||
const completedItems = await this._queryCompletedEntryItems(id);
|
||||
detailLines = this._buildCompletedItemLines(completedItems);
|
||||
} catch (detailErr) {
|
||||
ctx.logger.warn('[inbound_direct_log.notifyEntryOrderCompleted] 查询入库明细失败,通知降级为不含明细文案', detailErr);
|
||||
}
|
||||
|
||||
const contentLines = [ `入库单号${entryOrderCode}` ];
|
||||
for (let i = 0; i < detailLines.length; i++) {
|
||||
contentLines.push(` ${detailLines[i]}`);
|
||||
if (i < detailLines.length - 1) contentLines.push('---');
|
||||
}
|
||||
contentLines.push(' 已入库完毕');
|
||||
const content = contentLines.join('\n');
|
||||
|
||||
return await noticeService.send({
|
||||
warehouseId,
|
||||
@ -1302,6 +1321,55 @@ class InboundDirectLogService extends Service {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询入库单明细(含商品资料),用于组装完成入库通知文案。
|
||||
* 复用 EntryItems → StockWarehouse 的既有模型关联,不直接写 SQL。
|
||||
* @param {number} entryOrderId 入库单ID
|
||||
* @return {Promise<Array<Object>>} 入库明细列表(含 stock 关联数据),入参无效时返回空数组
|
||||
*/
|
||||
async _queryCompletedEntryItems(entryOrderId) {
|
||||
const { ctx } = this;
|
||||
const orderId = Number(entryOrderId || 0);
|
||||
if (!Number.isFinite(orderId) || orderId <= 0) return [];
|
||||
|
||||
return await ctx.model.EntryItems.findAll({
|
||||
where: { order_id: orderId },
|
||||
include: [
|
||||
{
|
||||
model: ctx.model.StockWarehouse,
|
||||
as: 'stock',
|
||||
required: false,
|
||||
attributes: [ 'id', 'item_name', 'sku_property' ],
|
||||
},
|
||||
],
|
||||
order: [[ 'order_line_no', 'ASC' ]],
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 将入库明细组装为通知文案明细行。
|
||||
* 每行格式:{商品名称} {商家编码} {规格} 数量:{数量},
|
||||
* 仅展示已入库数量(received_quantity)大于 0 的明细行。
|
||||
* 字段来源:商品名称=stock.item_name、商家编码=item.item_code、规格=stock.sku_property、数量=item.received_quantity。
|
||||
* @param {Array<Object>} entryItems 入库明细列表(含 stock 关联数据)
|
||||
* @return {Array<string>} 通知文案明细行列表
|
||||
*/
|
||||
_buildCompletedItemLines(entryItems = []) {
|
||||
const lines = [];
|
||||
for (const item of entryItems || []) {
|
||||
const inboundQty = Number(item.received_quantity) || 0;
|
||||
if (inboundQty <= 0) continue;
|
||||
|
||||
const stock = item.stock || {};
|
||||
const itemName = String(stock.item_name || '').trim();
|
||||
const merchantCode = String(item.item_code || '').trim();
|
||||
const skuProperty = String(stock.sku_property || '').trim();
|
||||
|
||||
lines.push(`${itemName} ${merchantCode} ${skuProperty} 数量:${inboundQty}`);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenAPI 回传:查询 ApiApp 回调地址并 POST 回传。
|
||||
* @param {Object} entryOrder 入库单
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user