chore(hospital): 补充本院与分院命名注释并加入院区数据维护脚本
明确本院 name 即主体名称、分院才带「主体名称-分院名称」;新增主院区改名与未支付取消订单清理脚本。
This commit is contained in:
@@ -80,11 +80,11 @@ const buildFudanFields = (fudan) => ({
|
||||
/**
|
||||
* Hospital Schema
|
||||
* 医院基础信息:名称、主体名称、昵称、拼音、省市、地址、电话、科室、等级、复旦综合榜档位。
|
||||
* 分院/院区按独立医院记录,不设 parentId;名称约定为「主体名称-分院名称」。
|
||||
* 分院/院区按独立医院记录,不设 parentId;本院 name 为主体名称,分院约定为「主体名称-分院名称」。
|
||||
*/
|
||||
const HospitalSchema = mongoose.Schema(
|
||||
{
|
||||
name: { type: String, required: true, comment: "医院名称,约定为「主体名称-分院名称」" },
|
||||
name: { type: String, required: true, comment: "医院名称;本院为主体名称本身,分院约定为「主体名称-分院名称」" },
|
||||
entityName: { type: String, default: "", index: true, comment: "主体名称;为空时自动取 name 分隔符前的部分" },
|
||||
nicknames: { type: [String], default: [], comment: "医院常用昵称;同一主体下的院区共享" },
|
||||
pinyin: { type: String, default: "", index: true, comment: "医院名称全拼(小写无分隔)" },
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 一次性清理:删除「未确认(pending)、未支付且已取消」的陪诊订单。
|
||||
*
|
||||
* 只有未确认且未收款的订单取消才物理删除;已确认订单即使未收款也要保留。
|
||||
*
|
||||
* 用法(在 api/health 下):
|
||||
* node scripts/delete_unpaid_cancelled_escort_records.js # 仅统计,不删除
|
||||
* node scripts/delete_unpaid_cancelled_escort_records.js --apply # 实际删除
|
||||
*/
|
||||
import { DBModel } from "../models/index.js";
|
||||
|
||||
DBModel.init();
|
||||
const conn = DBModel.dbConnection;
|
||||
const apply = process.argv.includes("--apply");
|
||||
|
||||
async function cleanup() {
|
||||
// 旧数据把取消写在 status 里(status=cancelled),无法判断取消前是否已确认,不纳入清理
|
||||
const filter = {
|
||||
status: "pending",
|
||||
"payment.status": "unpaid",
|
||||
cancelStatus: "cancelled",
|
||||
};
|
||||
|
||||
const records = await DBModel.EscortRecord.find(filter, {
|
||||
_id: 1,
|
||||
userId: 1,
|
||||
status: 1,
|
||||
cancelStatus: 1,
|
||||
"payment.status": 1,
|
||||
}).lean();
|
||||
|
||||
console.log(`matched pending+unpaid+cancelled escort records: ${records.length}`);
|
||||
records.forEach((record) => {
|
||||
console.log(
|
||||
` ${record._id} userId=${record.userId} status=${record.status} cancelStatus=${record.cancelStatus || "none"}`
|
||||
);
|
||||
});
|
||||
|
||||
if (!apply) {
|
||||
console.log("dry-run only. re-run with --apply to delete.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (records.length > 0) {
|
||||
const result = await DBModel.EscortRecord.deleteMany(filter);
|
||||
console.log(`deleted: ${result.deletedCount}`);
|
||||
}
|
||||
}
|
||||
|
||||
cleanup()
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
})
|
||||
.finally(() => {
|
||||
conn.close();
|
||||
});
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 一次性数据修复:本院记录的 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); });
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* 生成「本院记录改名」提案:用 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); });
|
||||
}
|
||||
Reference in New Issue
Block a user