119 lines
4.2 KiB
JavaScript
119 lines
4.2 KiB
JavaScript
/**
|
||
* 生成「本院记录改名」提案:用 doc/hospitals 的本院档案(无字母后缀的编号文件)
|
||
* 里的官方名称 + 主院区地址,去匹配库里的院区记录。
|
||
*
|
||
* 用法:node scripts/propose_main_campus_rename.js
|
||
* 输出 PROPOSED / UNRESOLVED 清单,人工确认后把 PROPOSED 填进
|
||
* scripts/fix_main_campus_name.js 的 RENAMES 执行。
|
||
*/
|
||
import { readdirSync, readFileSync } from "node:fs";
|
||
import path from "node:path";
|
||
import { fileURLToPath } from "node:url";
|
||
import { DBModel } from "../models/index.js";
|
||
|
||
const DOC_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "../../../doc/hospitals");
|
||
|
||
const normalize = (s) =>
|
||
String(s || "")
|
||
.replace(/([^)]*)/g, "")
|
||
.replace(/\([^)]*\)/g, "")
|
||
.replace(/北京市/g, "")
|
||
.replace(/[\s,、。;;,]/g, "");
|
||
|
||
// 解析本院档案:文件名不带字母后缀(01_xxx.md),取标题与主院区地址
|
||
function loadMainDocs() {
|
||
const docs = [];
|
||
for (const file of readdirSync(DOC_DIR)) {
|
||
if (!/^\d+_.*\.md$/.test(file)) continue;
|
||
const text = readFileSync(path.join(DOC_DIR, file), "utf-8");
|
||
const title = (text.match(/^#\s+(.+)$/m) || [])[1]?.trim() || "";
|
||
const addr = (text.match(/主院区地址[::]\s*(.+)/) || [])[1]?.trim() || "";
|
||
docs.push({ file, title, addr: normalize(addr) });
|
||
}
|
||
return docs;
|
||
}
|
||
|
||
// 地址匹配:归一后一方包含另一方,或共同尾部 >= 4 字
|
||
function addrMatch(a, b) {
|
||
if (!a || !b) return false;
|
||
if (a.includes(b) || b.includes(a)) return true;
|
||
const n = Math.min(a.length, b.length);
|
||
for (let k = n; k >= 4; k--) {
|
||
if (a.slice(-k) === b.slice(-k)) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
async function main() {
|
||
const docs = loadMainDocs();
|
||
const Hospital = DBModel.Hospital;
|
||
const hospitals = await Hospital.find({}).select("name entityName address").lean();
|
||
|
||
const hasMainRecord = new Set(
|
||
hospitals.filter((h) => h.entityName && h.name === h.entityName).map((h) => h.entityName)
|
||
);
|
||
|
||
// 按主体聚合「带后缀」的记录
|
||
const groups = new Map();
|
||
for (const h of hospitals) {
|
||
if (!h.entityName || h.name === h.entityName) continue;
|
||
if (!hasMainRecord.has(h.entityName)) {
|
||
if (!groups.has(h.entityName)) groups.set(h.entityName, []);
|
||
groups.get(h.entityName).push(h);
|
||
}
|
||
}
|
||
|
||
const proposed = [];
|
||
const unresolved = [];
|
||
|
||
for (const [entityName, records] of groups) {
|
||
// 找本院档案:标题与主体名称互相包含
|
||
const doc = docs.find(
|
||
(d) => d.title && (d.title === entityName || d.title.includes(entityName) || entityName.includes(d.title))
|
||
);
|
||
|
||
if (records.length === 1 && !doc) {
|
||
// 只有一条记录且无档案:视为本院
|
||
proposed.push({ from: records[0].name, to: entityName, why: "仅一条记录" });
|
||
continue;
|
||
}
|
||
|
||
if (!doc || !doc.addr) {
|
||
unresolved.push({ entityName, records: records.map((r) => `${r.name} @ ${r.address || "-"}`), reason: "未找到本院档案或档案无主院区地址" });
|
||
continue;
|
||
}
|
||
|
||
const hit = records.find((r) => addrMatch(normalize(r.address), doc.addr));
|
||
if (hit) {
|
||
proposed.push({ from: hit.name, to: entityName, why: `地址匹配档案 ${doc.file}` });
|
||
} else {
|
||
unresolved.push({
|
||
entityName,
|
||
docAddr: doc.addr,
|
||
records: records.map((r) => `${r.name} @ ${r.address || "-"}`),
|
||
reason: "档案地址匹配不上任何记录",
|
||
});
|
||
}
|
||
}
|
||
|
||
console.log(`=== PROPOSED (${proposed.length}) ===`);
|
||
for (const p of proposed) console.log(`- ${p.from}\n -> ${p.to} # ${p.why}`);
|
||
console.log(`\n=== UNRESOLVED (${unresolved.length}) ===`);
|
||
for (const u of unresolved) {
|
||
console.log(`- ${u.entityName} # ${u.reason}`);
|
||
if (u.docAddr) console.log(` 档案地址: ${u.docAddr}`);
|
||
for (const r of u.records) console.log(` ${r}`);
|
||
}
|
||
|
||
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); });
|
||
}
|