关联附件功能完成
This commit is contained in:
@@ -408,16 +408,19 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
|
|||||||
// 引用最近图片:用户要求把之前的图片关联到某采购
|
// 引用最近图片:用户要求把之前的图片关联到某采购
|
||||||
let usedRecentImage = false;
|
let usedRecentImage = false;
|
||||||
if (aiResult.use_recent_image) {
|
if (aiResult.use_recent_image) {
|
||||||
const recentMsg = db.prepare("SELECT attachments FROM messages WHERE room_id = ? AND attachments IS NOT NULL AND attachments != '' ORDER BY timestamp DESC LIMIT 1").get(roomId);
|
console.log('🔍 use_recent_image: 查找最近图片消息, roomId=', roomId);
|
||||||
|
const recentMsg = db.prepare("SELECT attachments FROM messages WHERE room_id = ? AND attachments IS NOT NULL AND attachments != '' AND attachments != '[]' ORDER BY timestamp DESC LIMIT 1").get(roomId);
|
||||||
|
console.log('🔍 最近消息:', recentMsg ? recentMsg.attachments : '(无)');
|
||||||
if (recentMsg) {
|
if (recentMsg) {
|
||||||
try {
|
try {
|
||||||
const files = JSON.parse(recentMsg.attachments);
|
const files = JSON.parse(recentMsg.attachments);
|
||||||
if (files.length) { attachments = files; usedRecentImage = true; }
|
if (files.length) { attachments = files; usedRecentImage = true; console.log('✅ 引用图片:', files.length, '个'); }
|
||||||
} catch(e) {}
|
} catch(e) { console.error('❌ 解析附件失败:', e); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const createdTime = aiResult.created_time || now;
|
const createdTime = aiResult.created_time || now;
|
||||||
let purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ?').get(roomId, `%${aiResult.purchase_item}%`, '已完成');
|
let purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ?').get(roomId, `%${aiResult.purchase_item}%`, '已完成');
|
||||||
|
console.log('📦 purchase_item:', aiResult.purchase_item, purchase ? '(已有)' : '(新建)');
|
||||||
let replyText = '';
|
let replyText = '';
|
||||||
const quantity = aiResult.quantity || 1;
|
const quantity = aiResult.quantity || 1;
|
||||||
const unitPrice = aiResult.unit_price || 0;
|
const unitPrice = aiResult.unit_price || 0;
|
||||||
@@ -477,14 +480,17 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (attachments?.length) {
|
if (attachments?.length) {
|
||||||
|
console.log('📎 插入附件:', attachments.length, '个, purchase_id=', purchase.id);
|
||||||
const insertAttach = db.prepare('INSERT INTO purchase_attachments (purchase_id, file_path, uploaded_by, timestamp) VALUES (?,?,?,?)');
|
const insertAttach = db.prepare('INSERT INTO purchase_attachments (purchase_id, file_path, uploaded_by, timestamp) VALUES (?,?,?,?)');
|
||||||
attachments.forEach(fp => insertAttach.run(purchase.id, fp, username, now));
|
attachments.forEach(fp => { console.log(' 📎', fp); insertAttach.run(purchase.id, fp, username, now); });
|
||||||
if (usedRecentImage) {
|
if (usedRecentImage) {
|
||||||
replyText = `📎 已为「${aiResult.purchase_item}」关联 ${attachments.length} 个附件`;
|
replyText = `📎 已为「${aiResult.purchase_item}」关联 ${attachments.length} 个附件`;
|
||||||
db.prepare('INSERT INTO purchase_history (purchase_id, action, user, timestamp) VALUES (?,?,?,?)').run(purchase.id, `添加附件:${attachments.length} 个`, username, now);
|
db.prepare('INSERT INTO purchase_history (purchase_id, action, user, timestamp) VALUES (?,?,?,?)').run(purchase.id, `添加附件:${attachments.length} 个`, username, now);
|
||||||
|
console.log('✅ usedRecentImage 完成, replyText:', replyText);
|
||||||
}
|
}
|
||||||
} else if (usedRecentImage) {
|
} else if (usedRecentImage) {
|
||||||
replyText = `❌ 未找到最近的图片消息,请先发送图片再试。`;
|
replyText = `❌ 未找到最近的图片消息,请先发送图片再试。`;
|
||||||
|
console.log('⚠️ usedRecentImage 但附件为空');
|
||||||
}
|
}
|
||||||
storeAndBroadcastText(roomId, '小财', replyText);
|
storeAndBroadcastText(roomId, '小财', replyText);
|
||||||
addToHistory(roomId, 'assistant', replyText);
|
addToHistory(roomId, 'assistant', replyText);
|
||||||
|
|||||||
Reference in New Issue
Block a user