Files
api_health/scripts/fix_main_campus_name.js
lik 2c79128ca1 chore(hospital): 补充本院与分院命名注释并加入院区数据维护脚本
明确本院 name 即主体名称、分院才带「主体名称-分院名称」;新增主院区改名与未支付取消订单清理脚本。
2026-11-05 20:16:58 +08:00

86 lines
3.2 KiB
JavaScript

/**
* 一次性数据修复:本院记录的 name 不应带「-分院名称」后缀。
*
* 背景:约定 name 为「主体名称-分院名称」,但部分本院记录也带了院区后缀,
* 如「中国医学科学院北京协和医院-王府井帅府园院区」实为本院,应改为「中国医学科学院北京协和医院」。
*
* 用法(在 api/health 下执行):
* 1. 默认 dry-run:只列出疑似本院记录(带分隔符后缀、且同主体下没有 name == entityName 的记录)
* node scripts/fix_main_campus_name.js
* 2. 确认后,在下方 RENAMES 填入「旧名称 -> 新名称」,执行:
* node scripts/fix_main_campus_name.js --apply
*
* 改名走模型静态方法,自动重算 entityName / pinyin / nicknames / fudanRank。
*/
import { DBModel } from "../models/index.js";
// 确认后的改名清单(dry-run 输出确认后填写)
const RENAMES = [
// { from: "中国医学科学院北京协和医院-王府井帅府园院区", to: "中国医学科学院北京协和医院" },
];
const APPLY = process.argv.includes("--apply");
const ENTITY_SEPARATOR = /[--—–]/;
async function main() {
const Hospital = DBModel.Hospital;
const hospitals = await Hospital.find({}).select("name entityName address").lean();
// 同主体下是否已存在「name == entityName」的本院记录
const hasMainRecord = new Set();
for (const h of hospitals) {
if (h.entityName && h.name === h.entityName) hasMainRecord.add(h.entityName);
}
const candidates = hospitals
.filter((h) => {
if (!h.entityName || h.name === h.entityName) return false;
// name 超出 entityName 的部分即为「分院名称」,本院记录不应有
return h.name.startsWith(h.entityName) && h.name.length > h.entityName.length
&& ENTITY_SEPARATOR.test(h.name.slice(h.entityName.length, h.entityName.length + 1));
})
.filter((h) => !hasMainRecord.has(h.entityName));
console.log(`total=${hospitals.length}, candidates=${candidates.length}`);
for (const h of candidates) {
console.log(`- name: ${h.name}\n entityName: ${h.entityName}\n address: ${h.address || "-"}`);
}
if (!APPLY) {
console.log("\n[dry-run] 未做任何修改。确认后填写 RENAMES 并加 --apply 执行。");
await DBModel.dbConnection.close();
return;
}
if (RENAMES.length === 0) {
console.log("[apply] RENAMES 为空,跳过。");
await DBModel.dbConnection.close();
return;
}
for (const { from, to } of RENAMES) {
const doc = await Hospital.findOne({ name: from }).exec();
if (!doc) {
console.warn(`[skip] 未找到: ${from}`);
continue;
}
doc.name = to;
doc.entityName = ""; // 触发 pre-save 重新派生
doc.pinyin = "";
doc.pinyinFL = "";
await doc.save();
console.log(`[ok] ${from} -> ${doc.name} (entityName=${doc.entityName})`);
}
await DBModel.dbConnection.close();
}
DBModel.init();
const conn = DBModel.dbConnection;
if (conn.readyState === 1) {
main().catch((e) => { console.error(e); process.exit(1); });
} else {
conn.once("connected", () => main().catch((e) => { console.error(e); process.exit(1); }));
conn.once("error", (e) => { console.error(e); process.exit(1); });
}