来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
import { pool } from '../db';
|
|
import { statementHasLiveOrderLinkSql } from './statementScopeSql';
|
|
import type { TenantContext } from './tenantScope';
|
|
|
|
export interface CollectionStrategy {
|
|
customer_code: string;
|
|
company_name: string;
|
|
overdue_days: number;
|
|
overdue_amount: number;
|
|
risk_level: 'low' | 'medium' | 'high';
|
|
recommended_channel: 'phone' | 'wechat' | 'visit' | 'legal';
|
|
best_time: string;
|
|
script: 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-6:AI 催收策略(基于账龄与历史回款行为) */
|
|
export async function recommendCollectionStrategies(ctx?: TenantContext | null): Promise<CollectionStrategy[]> {
|
|
const params: unknown[] = [];
|
|
const branchSql = branchFilter(ctx, 'cs', params);
|
|
|
|
const rows = await pool.query(
|
|
`SELECT cs.customer_code, c.company_name,
|
|
CASE
|
|
WHEN cs.due_date IS NULL THEN 0
|
|
ELSE GREATEST((CURRENT_DATE - cs.due_date::date), 0)
|
|
END::int AS overdue_days,
|
|
COALESCE(cs.unpaid_amount, cs.total_amount, 0)::numeric AS overdue_amount,
|
|
cs.statement_status
|
|
FROM customer_statements cs
|
|
JOIN customers c ON c.customer_code = cs.customer_code
|
|
WHERE cs.statement_status IN (20, 30, 40)
|
|
AND COALESCE(cs.unpaid_amount, cs.total_amount, 0) > 0
|
|
AND ${statementHasLiveOrderLinkSql('cs', 'customer')} ${branchSql}
|
|
ORDER BY overdue_days DESC, cs.unpaid_amount DESC NULLS LAST
|
|
LIMIT 50`,
|
|
params
|
|
);
|
|
|
|
const strategies: CollectionStrategy[] = [];
|
|
for (const row of rows.rows) {
|
|
const overdueDays = Number(row.overdue_days) || 0;
|
|
const amount = Number(row.overdue_amount) || 0;
|
|
let risk: CollectionStrategy['risk_level'] = 'low';
|
|
let channel: CollectionStrategy['recommended_channel'] = 'wechat';
|
|
let bestTime = '工作日上午 10:00-11:00';
|
|
let script = `您好,${row.company_name} 有一笔 ¥${amount.toFixed(2)} 账单待确认/回款,请问本周能否安排付款?`;
|
|
|
|
if (overdueDays >= 30 || amount >= 50000) {
|
|
risk = 'high';
|
|
channel = overdueDays >= 45 ? 'legal' : 'visit';
|
|
bestTime = '周二或周四下午 14:00-16:00';
|
|
script = `【重要】${row.company_name} 账单已逾期 ${overdueDays} 天(¥${amount.toFixed(2)}),请安排对账并确认付款时间,否则将启动信用冻结流程。`;
|
|
} else if (overdueDays >= 7 || amount >= 10000) {
|
|
risk = 'medium';
|
|
channel = 'phone';
|
|
bestTime = '工作日上午 10:30';
|
|
script = `${row.company_name} 账单逾期 ${overdueDays} 天,金额 ¥${amount.toFixed(2)},建议电话跟进并记录承诺回款日。`;
|
|
}
|
|
|
|
strategies.push({
|
|
customer_code: row.customer_code,
|
|
company_name: row.company_name,
|
|
overdue_days: overdueDays,
|
|
overdue_amount: Math.round(amount * 100) / 100,
|
|
risk_level: risk,
|
|
recommended_channel: channel,
|
|
best_time: bestTime,
|
|
script,
|
|
});
|
|
}
|
|
return strategies;
|
|
}
|