Files
SupplierSystem/scripts/set-br002-rebate-category.ts
T
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

139 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env tsx
/**
* 给一个分公司的客户批量配置「过账扣点 = 按分类」。
*
* 口径(2026-10-08 用户确认,BR002 自贡):
* 干杂 13% / 水果 13% / 蔬菜 13% / 菌菇 13% / 豆制品 13%
* 面食 6%
* 牛肉 8% / 家禽 8% / 水产 8% / 冻货 8% / 猪肉 8%
* 自采:**不设置**(该分类不计扣点)
* 客户范围:本分公司**全部客户**,但排除名单里的客户(那些客户改为「关闭」)
*
* 用法(默认 dry-run,只看不改;必须显式 --apply 才写库):
* cd server && ./node_modules/.bin/tsx --env-file=.env ../scripts/set-br002-rebate-category.ts
* cd server && ./node_modules/.bin/tsx --env-file=.env ../scripts/set-br002-rebate-category.ts --apply
*
* 连接的是 server/.env 的 DATABASE_URL(本地开发库)。脚本会在写库前打印目标库与影响行数,
* **线上要跑时先确认 DATABASE_URL 指向线上库**,并按仓库规则走迁移/授权流程。
*/
import { pool } from '../server/src/db';
import { runWithTenantDbContext } from '../server/src/services/tenantDbContext';
import {
loadCustomerRebateConfig,
saveCustomerRebateConfig,
} from '../server/src/services/customerRebateService';
/** 目标分公司 */
const BRANCH = 'BR002';
/** 分类名 → 比例(0.13 = 13%)。自采刻意不在表里 ⇒ 不设置。 */
const CATEGORY_RATIOS: Record<string, number> = {
干杂: 0.13,
水果: 0.13,
蔬菜: 0.13,
菌菇: 0.13,
豆制品: 0.13,
面食: 0.06,
牛肉: 0.08,
家禽: 0.08,
水产: 0.08,
冻货: 0.08,
猪肉: 0.08,
};
/** 不启用的客户(改为「关闭」) */
const DISABLED_CUSTOMER_CODES = ['CUS34985111820'];
const APPLY = process.argv.includes('--apply');
const pct = (v: number | null) => (v == null ? '—' : `${Number((v * 100).toFixed(4))}%`);
async function main() {
const url = String(process.env.DATABASE_URL || '');
const host = url.replace(/^.*@/, '').replace(/\/.*$/, '');
console.log(`目标库: ${host || '(server/.env 未配置 DATABASE_URL)'} 分公司: ${BRANCH}`);
console.log(`模式: ${APPLY ? '★ APPLY(写库)' : 'DRY-RUN(只看不改)'}\n`);
await runWithTenantDbContext(BRANCH, async () => {
// 1) 分类名 → 分类码(必须唯一命中,否则报错退出,避免写错分类)
const resolved: Record<string, string> = {};
for (const name of Object.keys(CATEGORY_RATIOS)) {
const r = await pool.query(
`SELECT code, name FROM product_catalog
WHERE item_type = 'category' AND name = $1 AND COALESCE(status, 1) = 1`,
[name]
);
if (r.rowCount !== 1) {
throw new Error(
`分类「${name}」在 ${BRANCH} 命中 ${r.rowCount} 条(需恰好 1 条),已中止,未写任何数据`
);
}
resolved[name] = String(r.rows[0].code);
}
const categoryRatios: Record<string, number> = {};
console.log('分类映射:');
for (const [name, ratio] of Object.entries(CATEGORY_RATIOS)) {
categoryRatios[resolved[name]] = ratio;
console.log(` ${name.padEnd(4, ' ')} ${pct(ratio).padStart(6)} ${resolved[name]}`);
}
console.log(' 自采 不设置(不计扣点)\n');
// 2) 目标客户:本分公司全部客户,排除名单里的
const customers = await pool.query(
`SELECT customer_code, company_name, status, rebate_mode, rebate_ratio, rebate_category_ratios
FROM customers ORDER BY company_name`
);
const targets = customers.rows.filter(
(row) => !DISABLED_CUSTOMER_CODES.includes(String(row.customer_code))
);
const disabled = customers.rows.filter((row) =>
DISABLED_CUSTOMER_CODES.includes(String(row.customer_code))
);
console.log(`客户(共 ${customers.rowCount} 个):`);
for (const row of targets) {
const code = String(row.customer_code);
const before = await loadCustomerRebateConfig(code);
console.log(
` [开启] ${String(row.company_name).padEnd(9, ' ')} ${code}` +
` ${before.mode ?? '关闭'}/${before.ratio != null ? pct(before.ratio) : '—'}` +
`/${Object.keys(before.categoryRatios).length}个分类` +
` → category/${Object.keys(categoryRatios).length}个分类`
);
}
for (const row of disabled) {
const code = String(row.customer_code);
const before = await loadCustomerRebateConfig(code);
console.log(
` [关闭] ${String(row.company_name).padEnd(9, ' ')} ${code}` +
` ${before.mode ?? '关闭'}/${Object.keys(before.categoryRatios).length}个分类 → 关闭`
);
}
if (!customers.rowCount) throw new Error(`${BRANCH} 没有客户,已中止`);
if (!APPLY) {
console.log('\nDRY-RUN 结束(未写库)。要真正写入请加 --apply');
return;
}
// 3) 写库(每个客户一次,走服务层做校验/归一)
for (const row of targets) {
await saveCustomerRebateConfig({
customerCode: String(row.customer_code),
mode: 'category',
categoryRatios,
});
}
for (const row of disabled) {
await saveCustomerRebateConfig({ customerCode: String(row.customer_code), mode: null });
}
console.log(`\n✅ 已写入:开启 ${targets.length} 个客户,关闭 ${disabled.length} 个客户`);
});
process.exit(0);
}
void main().catch((err) => {
console.error('失败:', err instanceof Error ? err.message : err);
process.exit(1);
});