来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
54 lines
2.3 KiB
JavaScript
54 lines
2.3 KiB
JavaScript
/**
|
|
* 生产重算客户按天结算单(取价回退修复后)。直接 import 已部署的 dist 代码。
|
|
* 用法: node regen-customer-statements.mjs [branch] [dateFrom] [dateTo]
|
|
* 在生产 server 目录下运行(需 .env 提供 DB 连接)。
|
|
*/
|
|
import { writeFileSync } from 'node:fs';
|
|
import { pool } from './dist/db.js';
|
|
import { runWithTenantDataAccess } from './dist/services/tenantSchemaService.js';
|
|
import { generateCustomerStatements } from './dist/services/financeReconciliationService.js';
|
|
|
|
async function main() {
|
|
const branch = process.argv[2] || 'BR002';
|
|
const dateFrom = process.argv[3] || '2026-07-17';
|
|
const dateTo = process.argv[4] || '2026-07-24';
|
|
|
|
await runWithTenantDataAccess(branch, async (c) => {
|
|
const stmts = await c.query(
|
|
`SELECT * FROM customer_statements
|
|
WHERE cycle_code ~ '^[0-9]{8}$' AND statement_status < 20 AND COALESCE(paid_amount,0)=0
|
|
ORDER BY cycle_code`
|
|
);
|
|
const nos = stmts.rows.map((r) => r.statement_no);
|
|
let lines = [];
|
|
if (nos.length) {
|
|
const l = await c.query(
|
|
`SELECT * FROM statement_lines WHERE statement_type='customer' AND statement_no = ANY($1::text[])`,
|
|
[nos]
|
|
);
|
|
lines = l.rows;
|
|
}
|
|
const backupFile = `customer_statements_backup_${branch}_${Date.now()}.json`;
|
|
writeFileSync(backupFile, JSON.stringify({ statements: stmts.rows, lines }, null, 2));
|
|
console.log(`已备份 ${stmts.rows.length} 张结算单 + ${lines.length} 行明细 -> ${backupFile}`);
|
|
});
|
|
|
|
const result = await runWithTenantDataAccess(branch, async (ctx) =>
|
|
generateCustomerStatements({ dateFrom, dateTo }, ctx)
|
|
);
|
|
console.log(`重算完成: created=${result.created} updated=${result.updated} skipped=${result.skipped}`);
|
|
|
|
await runWithTenantDataAccess(branch, async (c) => {
|
|
const stmt = await c.query(
|
|
`SELECT COALESCE(SUM(total_amount),0)::numeric AS total FROM customer_statements
|
|
WHERE cycle_code ~ '^[0-9]{8}$' AND cycle_code >= $1 AND cycle_code <= $2`,
|
|
[dateFrom.replace(/-/g, ''), dateTo.replace(/-/g, '')]
|
|
);
|
|
console.log(`重算后 ${dateFrom}~${dateTo} 结算单合计 = ¥${Number(stmt.rows[0].total).toFixed(2)}`);
|
|
});
|
|
|
|
await pool.end();
|
|
process.exit(0);
|
|
}
|
|
main().catch((e) => { console.error(e); process.exit(1); });
|