chore: update queryEngine

This commit is contained in:
2025-11-28 16:27:32 +08:00
parent 406c8c9ce2
commit 791a3a28bf

View File

@@ -17,7 +17,7 @@ export async function executeNaturalLanguageQuery(
// 场景 1: 商家 GMV支持时间范围 // 场景 1: 商家 GMV支持时间范围
if ((q.includes('gmv') || q.includes('销售额')) && q.includes('商家')) { 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; const merchantName = nameMatch ? nameMatch[1] : null;
if (!merchantName) throw new Error('请指定商家名称'); if (!merchantName) throw new Error('请指定商家名称');
@@ -48,7 +48,7 @@ export async function executeNaturalLanguageQuery(
// 场景 2: 订单状态 // 场景 2: 订单状态
if (q.includes('订单') && (q.includes('状态') || q.includes('详情'))) { 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; const orderId = idMatch ? idMatch[1] : null;
if (!orderId) throw new Error('请提供订单编号或ID'); if (!orderId) throw new Error('请提供订单编号或ID');
@@ -86,7 +86,7 @@ export async function executeNaturalLanguageQuery(
// 场景 4: 商品销量 // 场景 4: 商品销量
if (q.includes('商品') && q.includes('销量')) { 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; const goodsName = nameMatch && nameMatch[1] ? nameMatch[1].trim() : null;
if (!goodsName) throw new Error('请指定商品名称'); if (!goodsName) throw new Error('请指定商品名称');
@@ -105,7 +105,7 @@ export async function executeNaturalLanguageQuery(
// 场景 5: 退款率(按商家) // 场景 5: 退款率(按商家)
if (q.includes('退款率') && q.includes('商家')) { 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; const merchantName = nameMatch ? nameMatch[1] : null;
if (!merchantName) throw new Error('请指定商家名称'); if (!merchantName) throw new Error('请指定商家名称');
@@ -163,7 +163,7 @@ export async function executeNaturalLanguageQuery(
// 场景 8: 商户提现记录 // 场景 8: 商户提现记录
if (q.includes('提现') && q.includes('商户')) { 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; const merchantName = nameMatch ? nameMatch[1] : null;
if (!merchantName) throw new Error('请指定商户名称'); if (!merchantName) throw new Error('请指定商户名称');
@@ -243,7 +243,7 @@ export async function executeNaturalLanguageQuery(
// 场景 12: 店铺笔记 // 场景 12: 店铺笔记
if (q.includes('笔记') || q.includes('文章')) { 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; const noteTitle = titleMatch && titleMatch[1] ? titleMatch[1].trim() : null;
let sql = ` let sql = `
@@ -267,22 +267,24 @@ export async function executeNaturalLanguageQuery(
if ((q.includes('uniacid') || q.includes('平台id')) && (q.includes('商家') || q.includes('用户'))) { if ((q.includes('uniacid') || q.includes('平台id')) && (q.includes('商家') || q.includes('用户'))) {
// 处理通过用户ID查询 // 处理通过用户ID查询
if (q.includes('用户')) { if (q.includes('用户')) {
const idMatch = q.match(/用户\s*["']?(\d+)["']?/); // 从q中提取用户名。例如 用户 Lucy999 的 uniacid 是多少?
const userId = idMatch && idMatch[1] !== undefined ? parseInt(idMatch[1]) : null; // 注意:用户名可以包含字母、数字和中文。
if (!userId) throw new Error('请指定用户ID'); 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 = ` const sql = `
SELECT u.uniacid, u.uid, u.username, u.site_id SELECT u.uniacid, u.uid, u.username, u.site_id
FROM lucky_user u FROM lucky_user u
WHERE u.uid = ? WHERE u.username = ?
LIMIT 1 LIMIT 1
`; `;
const [rows] = await pool.execute(sql, [userId]); const [rows] = await pool.execute(sql, [username]);
return (rows as any[])[0] || null; return (rows as any[])[0] || null;
} }
// 处理通过商家名称查询 // 处理通过商家名称查询
else { 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; const merchantName = nameMatch ? nameMatch[1] : null;
if (!merchantName) throw new Error('请指定商家名称'); if (!merchantName) throw new Error('请指定商家名称');