From 791a3a28bfc0632e9c596ce6d2d44d4b348d6254 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Fri, 28 Nov 2025 16:27:32 +0800 Subject: [PATCH] chore: update queryEngine --- src/routes/v1/services/queryEngine.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/routes/v1/services/queryEngine.ts b/src/routes/v1/services/queryEngine.ts index 1a091bf..0ec78d7 100644 --- a/src/routes/v1/services/queryEngine.ts +++ b/src/routes/v1/services/queryEngine.ts @@ -17,7 +17,7 @@ export async function executeNaturalLanguageQuery( // 场景 1: 商家 GMV(支持时间范围) if ((q.includes('gmv') || q.includes('销售额')) && q.includes('商家')) { - const nameMatch = q.match(/商家\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+)["']?/); + const nameMatch = q.match(/商家\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+?)(?:["']?\s*的|\s|$)/); const merchantName = nameMatch ? nameMatch[1] : null; if (!merchantName) throw new Error('请指定商家名称'); @@ -48,7 +48,7 @@ export async function executeNaturalLanguageQuery( // 场景 2: 订单状态 if (q.includes('订单') && (q.includes('状态') || q.includes('详情'))) { - const idMatch = q.match(/订单\s*["']?([a-zA-Z0-9]+)["']?/); + const idMatch = q.match(/订单\s*["']?([a-zA-Z0-9]+?)["']?(?:\s*的|\s|$)/); const orderId = idMatch ? idMatch[1] : null; if (!orderId) throw new Error('请提供订单编号或ID'); @@ -86,7 +86,7 @@ export async function executeNaturalLanguageQuery( // 场景 4: 商品销量 if (q.includes('商品') && q.includes('销量')) { - const nameMatch = q.match(/商品\s*["']?([a-zA-Z0-9\u4e00-\u9fa5\s]+)["']?/); + const nameMatch = q.match(/商品\s*["']?([a-zA-Z0-9\u4e00-\u9fa5\s]+?)["']?(?:\s*的|\s|$)/); const goodsName = nameMatch && nameMatch[1] ? nameMatch[1].trim() : null; if (!goodsName) throw new Error('请指定商品名称'); @@ -105,7 +105,7 @@ export async function executeNaturalLanguageQuery( // 场景 5: 退款率(按商家) if (q.includes('退款率') && q.includes('商家')) { - const nameMatch = q.match(/商家\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+)["']?/); + const nameMatch = q.match(/商家\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+?)(?:["']?\s*的|\s|$)/); const merchantName = nameMatch ? nameMatch[1] : null; if (!merchantName) throw new Error('请指定商家名称'); @@ -163,7 +163,7 @@ export async function executeNaturalLanguageQuery( // 场景 8: 商户提现记录 if (q.includes('提现') && q.includes('商户')) { - const nameMatch = q.match(/商户\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+)["']?/); + const nameMatch = q.match(/商户\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+?)(?:["']?\s*的|\s|$)/); const merchantName = nameMatch ? nameMatch[1] : null; if (!merchantName) throw new Error('请指定商户名称'); @@ -243,7 +243,7 @@ export async function executeNaturalLanguageQuery( // 场景 12: 店铺笔记 if (q.includes('笔记') || q.includes('文章')) { - const titleMatch = q.match(/笔记\s*["']?([a-zA-Z0-9\u4e00-\u9fa5\s]+)["']?/); + const titleMatch = q.match(/笔记\s*["']?([a-zA-Z0-9\u4e00-\u9fa5\s]+?)["']?(?:\s*的|\s|$)/); const noteTitle = titleMatch && titleMatch[1] ? titleMatch[1].trim() : null; let sql = ` @@ -267,22 +267,24 @@ export async function executeNaturalLanguageQuery( if ((q.includes('uniacid') || q.includes('平台id')) && (q.includes('商家') || q.includes('用户'))) { // 处理通过用户ID查询 if (q.includes('用户')) { - const idMatch = q.match(/用户\s*["']?(\d+)["']?/); - const userId = idMatch && idMatch[1] !== undefined ? parseInt(idMatch[1]) : null; - if (!userId) throw new Error('请指定用户ID'); + // 从q中提取用户名。例如: 用户 Lucy999 的 uniacid 是多少? + // 注意:用户名可以包含字母、数字和中文。 + const nameMatch = q.match(/用户\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+?)(?:["']?\s*的|\s|$)/); + const username = nameMatch ? nameMatch[1] : null; + if (!username) throw new Error('请指定用户名'); const sql = ` SELECT u.uniacid, u.uid, u.username, u.site_id FROM lucky_user u - WHERE u.uid = ? + WHERE u.username = ? LIMIT 1 `; - const [rows] = await pool.execute(sql, [userId]); + const [rows] = await pool.execute(sql, [username]); return (rows as any[])[0] || null; } // 处理通过商家名称查询 else { - const nameMatch = q.match(/商家\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+)["']?/); + const nameMatch = q.match(/商家\s*["']?([a-zA-Z0-9\u4e00-\u9fa5]+?)(?:["']?\s*的|\s|$)/); const merchantName = nameMatch ? nameMatch[1] : null; if (!merchantName) throw new Error('请指定商家名称');