Files
SupplierSystem/server/check-pending-inbound.cjs
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

103 lines
4.0 KiB
JavaScript

const { Pool } = require('pg');
const fs = require('fs');
(async () => {
const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8');
const m = env.match(/^DATABASE_URL=(.+)$/m);
const pool = new Pool({
connectionString: m[1].trim().replace(/^["']|["']$/g, ''),
});
await pool.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`);
// Pending inspections created recently
const pending = await pool.query(
`SELECT i.id, i.order_code, i.sku_code, i.item_name, i.supplier_code,
i.prepare_qty, i.purchase_price, i.source_type, i.created_at,
i.branch_code, o.order_status, o.customer_code, o.created_at AS order_created_at,
sp.prepare_status, sp.prepare_time, sp.created_at AS prep_created_at,
sp.purchase_price AS prep_price
FROM inspections i
LEFT JOIN orders o ON o.order_code = i.order_code
LEFT JOIN supplier_preparations sp
ON sp.order_code = i.order_code
AND sp.sku_code IS NOT DISTINCT FROM i.sku_code
AND COALESCE(sp.supplier_code, '') = COALESCE(i.supplier_code, '')
WHERE i.inspection_time IS NULL
AND COALESCE(i.inbound_batch_no, '') = ''
AND COALESCE(i.compliance_status, 0) < 40
AND i.created_at >= NOW() - INTERVAL '36 hours'
ORDER BY i.created_at DESC
LIMIT 40`
);
console.log('PENDING_RECENT_COUNT', pending.rows.length);
for (const r of pending.rows) {
const prepDone = r.prepare_time ? new Date(r.prepare_time).getTime() : null;
const inspCreated = new Date(r.created_at).getTime();
const lagMin =
prepDone != null ? Math.round((inspCreated - prepDone) / 60000) : null;
console.log(
JSON.stringify({
item: r.item_name,
order: r.order_code,
order_status: r.order_status,
insp_created: r.created_at,
prep_status: r.prepare_status,
prep_time: r.prepare_time,
prep_created: r.prep_created_at,
lag_min_after_prep: lagMin,
source: r.source_type,
qty: r.prepare_qty,
price: r.purchase_price,
})
);
}
// Bucket: created in last 2h vs older pending still open
const buckets = await pool.query(
`SELECT
COUNT(*) FILTER (WHERE i.created_at >= NOW() - INTERVAL '2 hours')::int AS last_2h,
COUNT(*) FILTER (WHERE i.created_at >= NOW() - INTERVAL '12 hours'
AND i.created_at < NOW() - INTERVAL '2 hours')::int AS last_2_12h,
COUNT(*) FILTER (WHERE i.created_at < NOW() - INTERVAL '12 hours'
AND i.created_at >= NOW() - INTERVAL '36 hours')::int AS last_12_36h,
COUNT(*) FILTER (
WHERE i.inspection_time IS NULL
AND COALESCE(i.inbound_batch_no, '') = ''
AND COALESCE(i.compliance_status, 0) < 40
)::int AS all_pending
FROM inspections i
WHERE i.created_at >= NOW() - INTERVAL '36 hours'
OR (
i.inspection_time IS NULL
AND COALESCE(i.inbound_batch_no, '') = ''
AND COALESCE(i.compliance_status, 0) < 40
)`
);
console.log('BUCKETS', buckets.rows[0]);
// Cases where inspection created long after prep completed
const delayed = await pool.query(
`SELECT i.item_name, i.order_code, i.created_at AS insp_at, sp.prepare_time,
ROUND(EXTRACT(EPOCH FROM (i.created_at - sp.prepare_time))/60.0)::int AS lag_min
FROM inspections i
JOIN supplier_preparations sp
ON sp.order_code = i.order_code
AND sp.sku_code IS NOT DISTINCT FROM i.sku_code
AND COALESCE(sp.supplier_code, '') = COALESCE(i.supplier_code, '')
WHERE i.inspection_time IS NULL
AND COALESCE(i.inbound_batch_no, '') = ''
AND sp.prepare_time IS NOT NULL
AND i.created_at > sp.prepare_time + INTERVAL '30 minutes'
AND i.created_at >= NOW() - INTERVAL '48 hours'
ORDER BY lag_min DESC
LIMIT 20`
);
console.log('DELAYED_AFTER_PREP', delayed.rows);
await pool.end();
})().catch((e) => {
console.error(e);
process.exit(1);
});