来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
91 lines
3.3 KiB
JavaScript
91 lines
3.3 KiB
JavaScript
const { Pool } = require('pg');
|
|
const fs = require('fs');
|
|
|
|
async function loadDbUrl() {
|
|
const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8');
|
|
const m = env.match(/^DATABASE_URL=(.+)$/m);
|
|
if (!m) throw new Error('no DATABASE_URL');
|
|
return m[1].trim().replace(/^["']|["']$/g, '');
|
|
}
|
|
|
|
(async () => {
|
|
const pool = new Pool({ connectionString: await loadDbUrl() });
|
|
await pool.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`);
|
|
|
|
const skus = await pool.query(
|
|
`SELECT sku_code, sku_name, cost_price, sale_price
|
|
FROM skus WHERE sku_name ILIKE '%猪耳%' LIMIT 20`
|
|
);
|
|
console.log('SKUS', JSON.stringify(skus.rows, null, 2));
|
|
const codes = skus.rows.map((r) => r.sku_code);
|
|
if (!codes.length) {
|
|
await pool.end();
|
|
return;
|
|
}
|
|
|
|
const quotes = await pool.query(
|
|
`SELECT qs.quote_sheet_code, qs.branch_code, qs.approval_status, qs.status, qs.expires_at,
|
|
i.sku_code, i.cost_price, i.sale_price, i.price, qsc.customer_code
|
|
FROM customer_quote_sheet_items i
|
|
JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code
|
|
LEFT JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code
|
|
WHERE i.sku_code = ANY($1)
|
|
ORDER BY qs.updated_at DESC NULLS LAST
|
|
LIMIT 40`,
|
|
[codes]
|
|
);
|
|
console.log('QUOTES_COUNT', quotes.rows.length);
|
|
console.log('QUOTE_SAMPLE', JSON.stringify(quotes.rows.slice(0, 5), null, 2));
|
|
|
|
const preps = await pool.query(
|
|
`SELECT sp.id, sp.order_code, sp.sku_code, sp.item_name, sp.purchase_price, sp.prepare_status,
|
|
sp.branch_code, sp.created_at, o.customer_code, o.branch_code AS order_branch
|
|
FROM supplier_preparations sp
|
|
LEFT JOIN orders o ON o.order_code = sp.order_code
|
|
WHERE sp.sku_code = ANY($1) OR sp.item_name ILIKE '%猪耳%'
|
|
ORDER BY sp.created_at DESC NULLS LAST
|
|
LIMIT 30`,
|
|
[codes]
|
|
);
|
|
console.log('PREPS', JSON.stringify(preps.rows, null, 2));
|
|
|
|
for (const p of preps.rows.slice(0, 20)) {
|
|
const customerCode = String(p.customer_code || '').trim();
|
|
const branchCode = String(p.order_branch || p.branch_code || '').trim() || null;
|
|
if (!customerCode || !p.sku_code) {
|
|
console.log('SKIP_NO_CUST', p.order_code, p.item_name, p.purchase_price);
|
|
continue;
|
|
}
|
|
const hit = await pool.query(
|
|
`SELECT COALESCE(i.cost_price, 0)::float AS cost_price, qs.quote_sheet_code
|
|
FROM customer_quote_sheet_items i
|
|
INNER JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code
|
|
INNER JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code
|
|
WHERE qsc.customer_code = $1 AND i.sku_code = $2
|
|
AND qs.approval_status = 30 AND qs.status = 1
|
|
AND (qs.expires_at IS NULL OR qs.expires_at > NOW())
|
|
AND ($3::text IS NULL OR qs.branch_code IS NOT DISTINCT FROM $3)
|
|
ORDER BY qs.updated_at DESC NULLS LAST
|
|
LIMIT 1`,
|
|
[customerCode, p.sku_code, branchCode]
|
|
);
|
|
console.log(
|
|
'RESOLVE',
|
|
JSON.stringify({
|
|
order: p.order_code,
|
|
customer: customerCode,
|
|
branch: branchCode,
|
|
sku: p.sku_code,
|
|
item: p.item_name,
|
|
prep_price: p.purchase_price,
|
|
hit: hit.rows[0] || null,
|
|
})
|
|
);
|
|
}
|
|
|
|
await pool.end();
|
|
})().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|