fix(storage.receiving): 收货完成同步商品图片增加存量上限保护,已有图片超2张跳过复制,批量预统计去重并清理调试输出

This commit is contained in:
liufumingren 2026-09-01 00:50:14 +08:00
parent 277e80e8ca
commit ff84a99306

View File

@ -4885,11 +4885,16 @@ class ReceivingService extends Service {
* 收货完成后异步补全商品资料:绑定图片、补填毛重。
*
* 遍历当前收货单所有已绑定系统商品的明细:
* 1. 若商品 is_images === 'N' 且该明细有收货图片,则将收货图片复制为商品图片(FileReference),
* 并将商品的 is_images 更新为 'Y'。
* 1. 若该明细有收货图片,且商品在 wms_file_reference 中已有商品图片(business_type='stock'、
* business_id=商品ID)引用总数 <= 2,则将收货图片复制为商品图片(FileReference,按 file_id 去重),
* 并将商品的 is_images 更新为 'Y';若已有图片总数 > 2,则跳过该商品的图片复制与 is_images 更新,
* 避免商品图片无限累积。
* 2. 若商品 gross_weight 为 0 或 null,且明细的 weight > 0,则将商品毛重更新为该明细的 weight。
*
* 本方法通过 setImmediate 调用,运行在事务提交后,任何异常只记录日志,不影响主进程。
* 商品已有图片计数采用一次性批量查询(避免 N+1),口径为跨来源总量(不区分 created_by),
* 并按 warehouse_id 隔离;查询结果同时用于 file_id 内存去重与计数。
*
* 本方法在收货完成后异步调用,不阻塞主进程,任何异常只记录日志,不影响业务。
*
* @param {Object} options 参数对象
* @param {number} options.receivingId 收货单 ID
@ -4898,7 +4903,6 @@ class ReceivingService extends Service {
* @return {Promise<void>}
*/
async _syncProductDataFromReceiving({ receivingId, warehouseId, details }) {
console.log('_syncProductDataFromReceiving异步执行中...'); // 调试日志
const { ctx, app } = this;
// 只处理已绑定系统商品的明细
@ -4908,6 +4912,31 @@ class ReceivingService extends Service {
if (boundDetails.length === 0) return;
// -------- 批量预统计商品已有商品图片引用(跨来源总量,不区分 created_by),避免逐条 N+1 查询 --------
const productIds = Array.from(new Set(boundDetails.map(d => Number(d.system_product_id))));
const stockImageRefs = await app.model.FileReference.findAll({
where: {
business_type: 'stock',
business_id: productIds.map(id => String(id)),
warehouse_id: warehouseId,
},
attributes: [ 'business_id', 'file_id' ],
raw: true,
});
// 商品已有图片计数表:商品ID(字符串) -> 已有图片总数
const stockImageCountMap = new Map();
// 商品已有图片 file_id 表:商品ID(字符串) -> Set<file_id>(内存去重用)
const stockImageFileIdMap = new Map();
for (const ref of stockImageRefs) {
const productKey = ref.business_id;
stockImageCountMap.set(productKey, (stockImageCountMap.get(productKey) || 0) + 1);
if (!stockImageFileIdMap.has(productKey)) {
stockImageFileIdMap.set(productKey, new Set());
}
stockImageFileIdMap.get(productKey).add(ref.file_id);
}
for (const detail of boundDetails) {
const systemProductId = Number(detail.system_product_id);
try {
@ -4924,6 +4953,7 @@ class ReceivingService extends Service {
if (!stock) continue;
// -------- 1. 商品图片补全 --------
// 查询该明细是否有收货图片
const detailImageRefs = await app.model.FileReference.findAll({
where: {
@ -4936,42 +4966,54 @@ class ReceivingService extends Service {
});
if (detailImageRefs.length > 0) {
// 查询商品已有的图片 file_id(避免重复)
const existingStockImageFileIds = await app.model.FileReference.findAll({
where: {
business_type: 'stock',
business_id: String(systemProductId),
warehouse_id: warehouseId,
},
attributes: [ 'file_id' ],
raw: true,
}).then(rows => rows.map(r => r.file_id));
const productKey = String(systemProductId);
const existingStockImageCount = stockImageCountMap.get(productKey) || 0;
const toCreate = detailImageRefs
.filter(ref => !existingStockImageFileIds.includes(ref.file_id))
.map(ref => ({
file_id: ref.file_id,
business_type: 'stock',
business_id: String(systemProductId),
warehouse_id: warehouseId,
display_order: 0,
created_by: 'system',
created_at: new Date(),
}));
if (existingStockImageCount > 2) {
// 规则:已有图片数 > 2 时不再复制收货图片,也不更新 is_images,避免商品图片无限累积
ctx.logger.info(
`[_syncProductDataFromReceiving] 商品 ${systemProductId} 已有图片 ${existingStockImageCount} 张(>2),跳过图片复制(收货单 ${receivingId} 明细 ${detail.id})`
);
} else {
// 使用批量预统计的已有 file_id 做内存去重,避免重复复制
const existingStockImageFileIds = stockImageFileIdMap.get(productKey) || new Set();
if (toCreate.length > 0) {
await app.model.FileReference.bulkCreate(toCreate, { ignoreDuplicates: true });
const toCreate = detailImageRefs
.filter(ref => !existingStockImageFileIds.has(ref.file_id))
.map(ref => ({
file_id: ref.file_id,
business_type: 'stock',
business_id: productKey,
warehouse_id: warehouseId,
display_order: 0,
created_by: 'system',
created_at: new Date(),
}));
if (toCreate.length > 0) {
await app.model.FileReference.bulkCreate(toCreate, { ignoreDuplicates: true });
// 同步内存映射:同一收货单内同一商品的多个明细不会重复复制、也不会突破图片数上限
const createdFileIds = toCreate.map(ref => ref.file_id);
stockImageCountMap.set(
productKey,
(stockImageCountMap.get(productKey) || 0) + createdFileIds.length
);
const mergedFileIds = new Set(existingStockImageFileIds);
createdFileIds.forEach(fileId => mergedFileIds.add(fileId));
stockImageFileIdMap.set(productKey, mergedFileIds);
}
// 更新商品 is_images 标记
await app.model.StockWarehouse.update(
{ is_images: 'Y' },
{ where: { id: systemProductId } }
);
ctx.logger.info(
`[_syncProductDataFromReceiving] 商品 ${systemProductId} 已补全图片,共复制 ${toCreate.length} 张(收货单 ${receivingId} 明细 ${detail.id})`
);
}
// 更新商品 is_images 标记
await app.model.StockWarehouse.update(
{ is_images: 'Y' },
{ where: { id: systemProductId } }
);
ctx.logger.info(
`[_syncProductDataFromReceiving] 商品 ${systemProductId} 已补全图片,共复制 ${toCreate.length} 张(收货单 ${receivingId} 明细 ${detail.id})`
);
}
// -------- 2. 毛重补全 --------
@ -4989,7 +5031,7 @@ class ReceivingService extends Service {
}
} catch (err) {
// 单条明细失败不中断后续处理
console.log(
ctx.logger.error(
`[_syncProductDataFromReceiving] 处理明细 ${detail.id} (商品 ${systemProductId}) 失败: ${err.message}`
);
}