来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
110 lines
3.7 KiB
TypeScript
110 lines
3.7 KiB
TypeScript
import { pool } from '../db';
|
|
import type { TenantContext } from './tenantScope';
|
|
import { getPlatformDefaultBranchCode } from './platformBranch';
|
|
|
|
export interface ExceptionPrediction {
|
|
type: string;
|
|
severity: 'low' | 'medium' | 'high';
|
|
message: string;
|
|
related_code?: string;
|
|
}
|
|
|
|
function branchFilter(ctx: TenantContext | null | undefined, alias: string, params: unknown[]) {
|
|
if (ctx?.branchCode && !ctx.isSuperAdmin) {
|
|
params.push(ctx.branchCode);
|
|
return `AND ${alias}.branch_code = $${params.length}`;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/** P2-7:全链路异常预测(规则引擎) */
|
|
export async function predictExceptions(ctx?: TenantContext | null): Promise<ExceptionPrediction[]> {
|
|
const predictions: ExceptionPrediction[] = [];
|
|
const params: unknown[] = [];
|
|
const branchSql = branchFilter(ctx, 'sp', params);
|
|
|
|
const overduePrep = await pool.query(
|
|
`SELECT COUNT(*)::int AS cnt FROM supplier_preparations sp
|
|
WHERE prepare_status >= 10 AND prepare_status < 30
|
|
AND created_at < NOW() - INTERVAL '2 hours' ${branchSql}`,
|
|
params
|
|
);
|
|
if (Number(overduePrep.rows[0]?.cnt) >= 3) {
|
|
predictions.push({
|
|
type: 'prep_delay',
|
|
severity: 'high',
|
|
message: `${overduePrep.rows[0].cnt} 单备货超过 2 小时未完成,可能延误配送`,
|
|
});
|
|
}
|
|
|
|
const idleParams: unknown[] = [];
|
|
const idleBranch = branchFilter(ctx, 'dt', idleParams);
|
|
const idleDelivery = await pool.query(
|
|
`SELECT COUNT(*)::int AS cnt FROM delivery_tasks dt
|
|
WHERE task_status = 20 AND COALESCE(dt.start_time, dt.created_at) < NOW() - INTERVAL '30 minutes' ${idleBranch}`,
|
|
idleParams
|
|
);
|
|
if (Number(idleDelivery.rows[0]?.cnt) >= 2) {
|
|
predictions.push({
|
|
type: 'delivery_idle',
|
|
severity: 'medium',
|
|
message: `${idleDelivery.rows[0].cnt} 个配送任务 30 分钟无进展,建议联系司机`,
|
|
});
|
|
}
|
|
|
|
const redSkus = await pool.query(`SELECT COUNT(*)::int AS cnt FROM market_prices WHERE price_level = 'red'`);
|
|
if (Number(redSkus.rows[0]?.cnt) >= 5) {
|
|
predictions.push({
|
|
type: 'market_red_spike',
|
|
severity: 'medium',
|
|
message: `${redSkus.rows[0].cnt} 个 SKU 市场红灯,新单分配将受阻`,
|
|
});
|
|
}
|
|
|
|
const negMargin = await pool.query(
|
|
`SELECT COUNT(*)::int AS cnt FROM orders o
|
|
WHERE o.order_status >= 20 AND o.created_at >= NOW() - INTERVAL '7 days'
|
|
AND o.total_amount > 0
|
|
AND o.total_amount < (
|
|
SELECT COALESCE(SUM(cd.amount), 0) FROM cost_details cd WHERE cd.order_code = o.order_code
|
|
) LIMIT 1`
|
|
);
|
|
if (Number(negMargin.rows[0]?.cnt) >= 1) {
|
|
predictions.push({
|
|
type: 'negative_margin',
|
|
severity: 'high',
|
|
message: '近 7 天存在负毛利订单,建议进入 L2 降级并核查成本',
|
|
});
|
|
}
|
|
|
|
return predictions;
|
|
}
|
|
|
|
export async function runExceptionPredictionScan(ctx?: TenantContext | null) {
|
|
const predictions = await predictExceptions(ctx);
|
|
let ticketsCreated = 0;
|
|
|
|
for (const p of predictions.filter((x) => x.severity === 'high')) {
|
|
const existing = await pool.query(
|
|
`SELECT 1 FROM exception_tickets
|
|
WHERE ticket_type = $1 AND ticket_status < 90
|
|
LIMIT 1`,
|
|
[p.type]
|
|
);
|
|
if (existing.rowCount) {
|
|
continue;
|
|
}
|
|
const code = `EXP${Date.now()}${Math.random().toString(36).slice(2, 6).toUpperCase()}`;
|
|
const branchCode = ctx?.branchCode || (await getPlatformDefaultBranchCode());
|
|
const priority = p.severity === 'high' ? 1 : p.severity === 'medium' ? 2 : 3;
|
|
await pool.query(
|
|
`INSERT INTO exception_tickets (ticket_code, ticket_type, priority, ticket_status, handle_remark, branch_code)
|
|
VALUES ($1, $2, $3, 10, $4, $5)`,
|
|
[code, p.type, priority, p.message, branchCode]
|
|
);
|
|
ticketsCreated += 1;
|
|
}
|
|
|
|
return { predictions, ticketsCreated };
|
|
}
|