来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import { pool } from '../db';
|
|
|
|
export interface AskDataResult {
|
|
intent: string;
|
|
question: string;
|
|
rows: Record<string, unknown>[];
|
|
note: string;
|
|
}
|
|
|
|
/**
|
|
* 管理层问数 MVP:仅识别白名单意图,禁止任意 SQL。
|
|
* 支持:上周客户毛利掉了 / 近7日销量前十 / 待验收订单数
|
|
*/
|
|
export async function askBusinessData(input: {
|
|
question: string;
|
|
branchCode?: string | null;
|
|
}): Promise<AskDataResult> {
|
|
const q = String(input.question || '').trim();
|
|
if (!q) {
|
|
throw Object.assign(new Error('请输入问题'), { status: 400 });
|
|
}
|
|
const branch = String(input.branchCode || '').trim() || null;
|
|
const lower = q.toLowerCase();
|
|
|
|
if (/毛利|利润|margin/.test(q)) {
|
|
const params: unknown[] = [];
|
|
let branchSql = '';
|
|
if (branch) {
|
|
params.push(branch);
|
|
branchSql = ` AND o.branch_code = $${params.length}`;
|
|
}
|
|
const result = await pool.query(
|
|
`SELECT
|
|
o.customer_code,
|
|
MAX(c.company_name) AS customer_name,
|
|
ROUND(SUM(COALESCE(oi.amount, 0))::numeric, 2) AS sales_amount,
|
|
COUNT(DISTINCT o.order_code)::int AS order_count
|
|
FROM orders o
|
|
JOIN order_items oi ON oi.order_code = o.order_code
|
|
LEFT JOIN customers c ON c.customer_code = o.customer_code
|
|
WHERE o.created_at >= NOW() - INTERVAL '14 days'
|
|
${branchSql}
|
|
GROUP BY o.customer_code
|
|
ORDER BY sales_amount ASC
|
|
LIMIT 20`,
|
|
params
|
|
);
|
|
return {
|
|
intent: 'customer_sales_low',
|
|
question: q,
|
|
rows: result.rows,
|
|
note: '近14日销售额最低客户(代理毛利异常排查,非完整毛利账本)',
|
|
};
|
|
}
|
|
|
|
if (/销量|卖得|热销|top/.test(lower) || /销量|热销/.test(q)) {
|
|
const params: unknown[] = [];
|
|
let branchSql = '';
|
|
if (branch) {
|
|
params.push(branch);
|
|
branchSql = ` AND o.branch_code = $${params.length}`;
|
|
}
|
|
const result = await pool.query(
|
|
`SELECT
|
|
oi.sku_code,
|
|
MAX(COALESCE(oi.item_name, s.sku_name)) AS sku_name,
|
|
ROUND(SUM(oi.quantity)::numeric, 2) AS qty,
|
|
COUNT(*)::int AS lines
|
|
FROM order_items oi
|
|
JOIN orders o ON o.order_code = oi.order_code
|
|
LEFT JOIN skus s ON s.sku_code = oi.sku_code
|
|
WHERE o.created_at >= NOW() - INTERVAL '7 days'
|
|
${branchSql}
|
|
GROUP BY oi.sku_code
|
|
ORDER BY qty DESC NULLS LAST
|
|
LIMIT 10`,
|
|
params
|
|
);
|
|
return {
|
|
intent: 'top_sku_7d',
|
|
question: q,
|
|
rows: result.rows,
|
|
note: '近7日销量 Top10(数量合计)',
|
|
};
|
|
}
|
|
|
|
if (/待验收|验收/.test(q)) {
|
|
const params: unknown[] = [];
|
|
let branchSql = '';
|
|
if (branch) {
|
|
params.push(branch);
|
|
branchSql = ` AND branch_code = $${params.length}`;
|
|
}
|
|
const result = await pool.query(
|
|
`SELECT COUNT(*)::int AS pending_inspection_orders
|
|
FROM orders
|
|
WHERE order_status = 40 ${branchSql}`,
|
|
params
|
|
);
|
|
return {
|
|
intent: 'pending_inspection_count',
|
|
question: q,
|
|
rows: result.rows,
|
|
note: '当前待验收订单数',
|
|
};
|
|
}
|
|
|
|
throw Object.assign(
|
|
new Error('暂不支持该问题。可试:「近7日热销」「毛利偏低客户」「待验收订单数」'),
|
|
{ status: 400, code: 'unsupported_intent' }
|
|
);
|
|
}
|