来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
130 lines
4.7 KiB
JavaScript
130 lines
4.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { Pool } = require('pg');
|
|
|
|
function esc(value) {
|
|
if (value === null || value === undefined) return 'NULL';
|
|
if (typeof value === 'boolean') return value ? 'true' : 'false';
|
|
if (typeof value === 'number') return String(value);
|
|
return `'${String(value).replace(/'/g, "''")}'`;
|
|
}
|
|
|
|
async function main() {
|
|
const outFile =
|
|
process.argv[2] || path.join(__dirname, '..', '..', 'scripts', 'sync-suppliers.sql');
|
|
const pool = new Pool({
|
|
connectionString:
|
|
process.env.DATABASE_URL || 'postgresql://canteen:canteen123@127.0.0.1:5432/canteen_delivery',
|
|
});
|
|
|
|
const suppliers = await pool.query(`
|
|
SELECT supplier_code, company_name, contact_name, contact_phone,
|
|
business_license_url, food_license_url, rating, quota_percent,
|
|
settlement_days, status
|
|
FROM suppliers
|
|
ORDER BY supplier_code
|
|
`);
|
|
|
|
const categories = await pool.query(`
|
|
SELECT supplier_code, category_code, is_primary
|
|
FROM supplier_categories
|
|
ORDER BY category_code, supplier_code
|
|
`);
|
|
|
|
const skuSuppliers = await pool.query(`
|
|
SELECT sku_code, supplier_code, supply_price, quality_rating, lead_time,
|
|
min_order_qty, is_preferred, status
|
|
FROM sku_suppliers
|
|
ORDER BY sku_code, supplier_code
|
|
`);
|
|
|
|
const lines = [
|
|
'-- Auto-generated supplier sync from local database',
|
|
'BEGIN;',
|
|
'',
|
|
`-- suppliers: ${suppliers.rowCount}`,
|
|
];
|
|
|
|
for (const row of suppliers.rows) {
|
|
lines.push(`INSERT INTO suppliers (
|
|
supplier_code, company_name, contact_name, contact_phone,
|
|
business_license_url, food_license_url, rating, quota_percent,
|
|
settlement_days, status
|
|
) VALUES (
|
|
${esc(row.supplier_code)}, ${esc(row.company_name)}, ${esc(row.contact_name)}, ${esc(row.contact_phone)},
|
|
${esc(row.business_license_url)}, ${esc(row.food_license_url)}, ${esc(row.rating)}, ${esc(row.quota_percent ?? 100)},
|
|
${esc(row.settlement_days ?? 30)}, ${esc(row.status ?? 10)}
|
|
) ON CONFLICT (supplier_code) DO UPDATE SET
|
|
company_name = EXCLUDED.company_name,
|
|
contact_name = EXCLUDED.contact_name,
|
|
contact_phone = EXCLUDED.contact_phone,
|
|
business_license_url = EXCLUDED.business_license_url,
|
|
food_license_url = EXCLUDED.food_license_url,
|
|
rating = EXCLUDED.rating,
|
|
quota_percent = EXCLUDED.quota_percent,
|
|
settlement_days = EXCLUDED.settlement_days,
|
|
status = EXCLUDED.status;`);
|
|
}
|
|
|
|
lines.push('', `-- supplier_categories: ${categories.rowCount}`);
|
|
|
|
for (const row of categories.rows) {
|
|
lines.push(`INSERT INTO supplier_categories (supplier_code, category_code, is_primary)
|
|
SELECT ${esc(row.supplier_code)}, ${esc(row.category_code)}, ${row.is_primary ? 'true' : 'false'}
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM supplier_categories sc
|
|
WHERE sc.supplier_code = ${esc(row.supplier_code)} AND sc.category_code = ${esc(row.category_code)}
|
|
);`);
|
|
}
|
|
|
|
if (categories.rows.length) {
|
|
lines.push(`
|
|
UPDATE supplier_categories sc
|
|
SET is_primary = (sc.supplier_code = mapping.supplier_code)
|
|
FROM (VALUES
|
|
${categories.rows
|
|
.filter((row) => row.is_primary)
|
|
.map((row) => ` (${esc(row.category_code)}, ${esc(row.supplier_code)})`)
|
|
.join(',\n')}
|
|
) AS mapping(category_code, supplier_code)
|
|
WHERE sc.category_code = mapping.category_code;`);
|
|
}
|
|
|
|
lines.push('', `-- sku_suppliers: ${skuSuppliers.rowCount}`);
|
|
|
|
for (const row of skuSuppliers.rows) {
|
|
lines.push(`INSERT INTO sku_suppliers (
|
|
sku_code, supplier_code, supply_price, quality_rating, lead_time,
|
|
min_order_qty, is_preferred, status
|
|
) VALUES (
|
|
${esc(row.sku_code)}, ${esc(row.supplier_code)}, ${esc(row.supply_price)}, ${esc(row.quality_rating ?? 4)},
|
|
${esc(row.lead_time ?? 1)}, ${esc(row.min_order_qty ?? 1)}, ${row.is_preferred ? 'true' : 'false'}, ${esc(row.status ?? 1)}
|
|
) ON CONFLICT (sku_code, supplier_code) DO UPDATE SET
|
|
supply_price = EXCLUDED.supply_price,
|
|
quality_rating = EXCLUDED.quality_rating,
|
|
lead_time = EXCLUDED.lead_time,
|
|
min_order_qty = EXCLUDED.min_order_qty,
|
|
is_preferred = EXCLUDED.is_preferred,
|
|
status = EXCLUDED.status;`);
|
|
}
|
|
|
|
lines.push(
|
|
'',
|
|
'COMMIT;',
|
|
'',
|
|
'SELECT COUNT(*)::int AS suppliers FROM suppliers;',
|
|
"SELECT COUNT(*)::int AS supplier_categories FROM supplier_categories;",
|
|
"SELECT COUNT(*)::int AS sku_suppliers FROM sku_suppliers WHERE status = 1;"
|
|
);
|
|
|
|
fs.writeFileSync(outFile, lines.join('\n'), 'utf8');
|
|
console.log(`Exported ${suppliers.rowCount} suppliers, ${categories.rowCount} category bindings, ${skuSuppliers.rowCount} sku_suppliers`);
|
|
console.log(`SQL file: ${outFile}`);
|
|
await pool.end();
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|