来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
162 lines
6.3 KiB
JavaScript
162 lines
6.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 诊断小程序「供货任务」数据缺失
|
|
* 用法: node scripts/prod-diagnose-supplier-preparations.cjs [supplier_code]
|
|
*/
|
|
const path = require('path');
|
|
const serverRoot = path.join(__dirname, '..');
|
|
process.chdir(serverRoot);
|
|
require(path.join(serverRoot, 'dist/env.js')).loadEnv();
|
|
const { pool } = require(path.join(serverRoot, 'dist/db.js'));
|
|
|
|
const filterSupplier = process.argv[2] || '';
|
|
|
|
(async () => {
|
|
console.log('=== supplier_preparations by status (all) ===');
|
|
const byStatus = await pool.query(
|
|
`SELECT prepare_status, COUNT(*)::int cnt
|
|
FROM supplier_preparations
|
|
${filterSupplier ? 'WHERE supplier_code = $1' : ''}
|
|
GROUP BY prepare_status ORDER BY prepare_status`,
|
|
filterSupplier ? [filterSupplier] : []
|
|
);
|
|
console.table(byStatus.rows);
|
|
|
|
console.log('\n=== active (<30) by supplier (top 20) ===');
|
|
const activeBySupplier = await pool.query(
|
|
`SELECT sp.supplier_code, s.company_name, COUNT(*)::int cnt,
|
|
MIN(sp.created_at) AS oldest, MAX(sp.created_at) AS newest
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN suppliers s ON s.supplier_code = sp.supplier_code
|
|
WHERE sp.prepare_status < 30
|
|
${filterSupplier ? 'AND sp.supplier_code = $1' : ''}
|
|
GROUP BY sp.supplier_code, s.company_name
|
|
ORDER BY cnt DESC LIMIT 20`,
|
|
filterSupplier ? [filterSupplier] : []
|
|
);
|
|
console.table(activeBySupplier.rows);
|
|
|
|
console.log('\n=== supplier_users (mobile accounts) ===');
|
|
const users = await pool.query(
|
|
`SELECT su.user_id, su.supplier_code, su.username, su.status,
|
|
s.company_name, p.username AS profile_username, p.full_name
|
|
FROM supplier_users su
|
|
LEFT JOIN suppliers s ON s.supplier_code = su.supplier_code
|
|
LEFT JOIN profiles p ON p.id = su.user_id
|
|
${filterSupplier ? 'WHERE su.supplier_code = $1' : ''}
|
|
ORDER BY su.supplier_code, su.username`,
|
|
filterSupplier ? [filterSupplier] : []
|
|
);
|
|
console.table(users.rows);
|
|
|
|
console.log('\n=== active tasks per supplier_user (simulates API) ===');
|
|
for (const u of users.rows) {
|
|
const active = await pool.query(
|
|
`SELECT COUNT(*)::int cnt FROM supplier_preparations
|
|
WHERE supplier_code = $1 AND prepare_status < 30`,
|
|
[u.supplier_code]
|
|
);
|
|
const archived7d = await pool.query(
|
|
`SELECT COUNT(*)::int cnt FROM supplier_preparations
|
|
WHERE supplier_code = $1 AND prepare_status >= 30
|
|
AND COALESCE(prepare_time, created_at)::date >= CURRENT_DATE - INTERVAL '7 days'`,
|
|
[u.supplier_code]
|
|
);
|
|
console.log(
|
|
`${u.profile_username || u.username} -> ${u.supplier_code} (${u.company_name}) status=${u.status} active=${active.rows[0].cnt} archived_7d=${archived7d.rows[0].cnt}`
|
|
);
|
|
}
|
|
|
|
console.log('\n=== recent orders with allocation but no active prep (last 7 days) ===');
|
|
const orphan = await pool.query(
|
|
`SELECT o.order_code, o.order_status, o.delivery_date, o.created_at,
|
|
(SELECT COUNT(*)::int FROM supplier_preparations sp WHERE sp.order_code = o.order_code) AS prep_cnt,
|
|
(SELECT COUNT(*)::int FROM purchase_orders po WHERE po.order_code = o.order_code) AS po_cnt
|
|
FROM orders o
|
|
WHERE o.created_at >= NOW() - INTERVAL '7 days'
|
|
AND o.order_status >= 20 AND o.order_status < 90
|
|
ORDER BY o.created_at DESC
|
|
LIMIT 30`
|
|
);
|
|
console.table(orphan.rows);
|
|
|
|
console.log('\n=== recent active preparations sample ===');
|
|
const sample = await pool.query(
|
|
`SELECT sp.id, sp.order_code, sp.supplier_code, sp.sku_code,
|
|
COALESCE(sp.item_name, sp.sku_code) AS item_name,
|
|
sp.prepare_status, sp.created_at,
|
|
to_char(COALESCE(o.delivery_date, sp.created_at)::date, 'YYYY-MM-DD') AS task_day
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
WHERE sp.prepare_status < 30
|
|
${filterSupplier ? 'AND sp.supplier_code = $1' : ''}
|
|
ORDER BY sp.created_at DESC LIMIT 15`,
|
|
filterSupplier ? [filterSupplier] : []
|
|
);
|
|
console.table(sample.rows);
|
|
|
|
console.log('\n=== active preps by delivery_date ===');
|
|
const byDelivery = await pool.query(
|
|
`SELECT to_char(o.delivery_date::date, 'YYYY-MM-DD') AS delivery_day, COUNT(*)::int cnt
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
WHERE sp.prepare_status < 30
|
|
GROUP BY 1 ORDER BY 1`
|
|
);
|
|
console.table(byDelivery.rows);
|
|
|
|
const today = new Date().toISOString().slice(0, 10);
|
|
const todayFilter = await pool.query(
|
|
`SELECT COUNT(*)::int cnt FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
WHERE sp.prepare_status < 30
|
|
AND COALESCE(o.delivery_date::date, sp.created_at::date) = $1::date`,
|
|
[today]
|
|
);
|
|
console.log('active with delivery_date=today (' + today + '):', todayFilter.rows[0].cnt);
|
|
|
|
const yesterday = new Date();
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
const yesterdayStr = yesterday.toISOString().slice(0, 10);
|
|
|
|
console.log('\n=== by delivery_date (all statuses) last 3 days ===');
|
|
const recent = await pool.query(
|
|
`SELECT to_char(o.delivery_date::date, 'YYYY-MM-DD') AS delivery_day,
|
|
sp.prepare_status, COUNT(*)::int cnt
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
WHERE o.delivery_date::date >= (CURRENT_DATE - INTERVAL '2 days')
|
|
GROUP BY 1, 2 ORDER BY 1, 2`
|
|
);
|
|
console.table(recent.rows);
|
|
|
|
console.log('\n=== yesterday delivery (' + yesterdayStr + ') detail ===');
|
|
const yDetail = await pool.query(
|
|
`SELECT sp.prepare_status, COUNT(*)::int cnt
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
WHERE COALESCE(o.delivery_date::date, sp.created_at::date) = $1::date
|
|
GROUP BY 1 ORDER BY 1`,
|
|
[yesterdayStr]
|
|
);
|
|
console.table(yDetail.rows);
|
|
|
|
const yPending = await pool.query(
|
|
`SELECT sp.supplier_code, su.company_name, COUNT(*)::int cnt
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
LEFT JOIN suppliers su ON su.supplier_code = sp.supplier_code
|
|
WHERE COALESCE(o.delivery_date::date, sp.created_at::date) = $1::date
|
|
AND sp.prepare_status < 30
|
|
GROUP BY 1, 2 ORDER BY cnt DESC`,
|
|
[yesterdayStr]
|
|
);
|
|
console.log('yesterday pending by supplier:');
|
|
console.table(yPending.rows);
|
|
|
|
await pool.end();
|
|
})().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|