From b7605fcf34181c8669d0265e3bc2fe0f57385338 Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 22 Jul 2026 17:53:32 +0800 Subject: [PATCH] fix issues --- server/handlers.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server/handlers.js b/server/handlers.js index 5fa8170..f97b95b 100644 --- a/server/handlers.js +++ b/server/handlers.js @@ -134,19 +134,19 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) { if (existing) { console.log('🛡️ 兜底纠正: 转为更新模式'); isNew = false; purchase = existing; } } if (!isNew && !purchase) { - purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ?').get(roomId, `%${aiResult.purchase_item}%`, '已完成'); + purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? ORDER BY created_at DESC LIMIT 1').get(roomId, `%${aiResult.purchase_item}%`); // 全名匹配失败 → 去掉括号内容后用剩余关键词回退匹配 if (!purchase) { const keyword = aiResult.purchase_item.replace(/[((].*[))]/g, '').trim(); if (keyword.length >= 2) { console.log('🔍 全名不匹配,尝试关键词:', keyword); - purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ? ORDER BY created_at DESC LIMIT 1').get(roomId, `%${keyword}%`, '已完成'); + purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? ORDER BY created_at DESC LIMIT 1').get(roomId, `%${keyword}%`); } } - // 所有 LIKE 失败 → 回退到房间内最近一条未完成采购(用户纠正的大概率是它) + // 所有 LIKE 失败 → 回退到房间内最近一条采购 if (!purchase) { - console.log('🔍 LIKE 全失败,回退到最近未完成记录'); - purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND status != ? ORDER BY created_at DESC LIMIT 1').get(roomId, '已完成'); + console.log('🔍 LIKE 全失败,回退到最近记录'); + purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? ORDER BY created_at DESC LIMIT 1').get(roomId); } } console.log('📦 purchase_item:', aiResult.purchase_item, isNew ? '(强制新建)' : purchase ? '(匹配已有)' : '(未匹配-新建)'); @@ -163,6 +163,14 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) { const freight = aiResult.freight || 0; const amount = quantity * unitPrice + freight; + // 新建但金额为 0 且 AI 未提供数量/单价 → 拒绝(避免垃圾记录) + if (!purchase && amount === 0 && aiResult.quantity === undefined && aiResult.unit_price === undefined) { + console.log('🚫 拒绝新建 amount=0 记录, item:', aiResult.purchase_item); + storeAndBroadcastText(roomId, '小财', `无法创建「${aiResult.purchase_item}」:缺少数量和价格信息,请补充。`); + addToHistory(roomId, 'assistant', '缺少数量和价格'); + return; + } + if (!purchase) { const id = uuidv4(); const paymentMethod = aiResult.payment_method || (aiResult.method === '淘宝' ? '支付宝' : (aiResult.method || '未指定'));