diff --git a/models/schema/hospital.js b/models/schema/hospital.js index 830d34d..ebb7e62 100644 --- a/models/schema/hospital.js +++ b/models/schema/hospital.js @@ -3,6 +3,7 @@ import mongoose from "mongoose"; import { pinyin } from "pinyin-pro"; import { hospitalRanking } from "../../resource/fudan_hospital_ranking.js"; +import { resolveHospitalNicknames } from "../../resource/hospital_nicknames.js"; // 复旦综合榜未上榜医院的默认档位:排在所有上榜医院之后 const DEFAULT_FUDAN_RANK = 999; @@ -68,13 +69,14 @@ const resolveFudanRank = (names = []) => { /** * Hospital Schema - * 医院基础信息:名称、主体名称、拼音、省市、地址、电话、科室、等级、复旦综合榜档位。 + * 医院基础信息:名称、主体名称、昵称、拼音、省市、地址、电话、科室、等级、复旦综合榜档位。 * 分院/院区按独立医院记录,不设 parentId;名称约定为「主体名称-分院名称」。 */ const HospitalSchema = mongoose.Schema( { 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: "医院名称全拼(小写无分隔)" }, pinyinFL: { type: String, default: "", index: true, comment: "医院名称拼音首字母" }, province: { type: String, default: "", index: true, comment: "省份" }, @@ -134,6 +136,8 @@ HospitalSchema.pre("save", function (next) { this.pinyinFL = fields.pinyinFL; } + this.nicknames = resolveHospitalNicknames(this.entityName, name); + const fudan = resolveFudanRank([this.entityName, name]); this.fudanRank = fudan ? fudan.rank : DEFAULT_FUDAN_RANK; this.fudanGrade = fudan ? fudan.grade : ""; @@ -167,7 +171,7 @@ HospitalSchema.statics.findHospitals = async function (options = {}) { if (entityName) filter.entityName = toRegex(entityName); if (keyword) { const re = toRegex(keyword); - filter.$or = [{ name: re }, { entityName: re }, { pinyin: re }, { pinyinFL: re }]; + filter.$or = [{ name: re }, { entityName: re }, { pinyin: re }, { pinyinFL: re }, { nicknames: re }]; } const skip = (page - 1) * pageSize; @@ -224,11 +228,12 @@ HospitalSchema.statics.updateHospitalById = async function (id, update) { } } - // 名称或主体名称变更后重算复旦榜档位;未上榜写默认值 + // 名称或主体名称变更后重算昵称与复旦榜档位;未上榜写默认值 if (update.name !== undefined || update.entityName !== undefined) { const current = await this.findById(id).select("name entityName").lean(); const finalName = update.name !== undefined ? update.name : current?.name; const finalEntityName = update.entityName !== undefined ? update.entityName : current?.entityName; + update.nicknames = resolveHospitalNicknames(finalEntityName, finalName); const fudan = resolveFudanRank([finalEntityName, finalName]); update.fudanRank = fudan ? fudan.rank : DEFAULT_FUDAN_RANK; update.fudanGrade = fudan ? fudan.grade : ""; diff --git a/resource/hospital_nicknames.js b/resource/hospital_nicknames.js new file mode 100644 index 0000000..ad3ec15 --- /dev/null +++ b/resource/hospital_nicknames.js @@ -0,0 +1,88 @@ +/** + * 医院常用昵称 + * + * 按主体名称(entityName)维护:同一主体下的分院/院区共享同一组昵称。 + * 只收录北京地区常用、指向明确的叫法,避免把不同医院的俗称混在一起。 + */ +const NICKNAMES_BY_ENTITY = { + // ===== 综合与专科知名医院 ===== + "中国医学科学院北京协和医院": ["协和", "北京协和", "协和医院"], + "北京大学第一医院": ["北大医院", "北医一院"], + "北京大学人民医院": ["北大人民", "北人", "人民医院"], + "北京大学第三医院": ["北医三院", "北医三"], + "北京大学肿瘤医院": ["北肿", "北大肿瘤"], + "中国医学科学院肿瘤医院": ["东肿", "医科院肿瘤"], + "中国医学科学院阜外医院": ["阜外", "阜外医院"], + "中国医学科学院整形外科医院": ["八大处", "整形医院"], + "首都医科大学宣武医院": ["宣武", "宣武医院"], + "首都医科大学附属北京友谊医院": ["友谊", "友谊医院"], + "首都医科大学附属北京儿童医院": ["北京儿童医院", "儿童医院"], + "首都医科大学附属首都儿童医学中心": ["儿研所", "首都儿科研究所"], + "首都医科大学附属北京天坛医院": ["天坛", "天坛医院"], + "首都医科大学附属北京同仁医院": ["同仁", "同仁医院"], + "首都医科大学附属北京安贞医院": ["安贞", "安贞医院"], + "首都医科大学附属北京朝阳医院": ["朝阳", "朝阳医院"], + "首都医科大学附属北京积水潭医院": ["积水潭", "积水潭医院"], + "首都医科大学附属北京安定医院": ["安定", "安定医院"], + "首都医科大学附属北京地坛医院": ["地坛", "地坛医院"], + "首都医科大学附属北京佑安医院": ["佑安", "佑安医院"], + "首都医科大学附属北京胸科医院": ["胸科", "胸科医院"], + "首都医科大学附属北京口腔医院": ["首医口腔", "北京口腔"], + "北京大学口腔医院": ["北大口腔", "北医口腔"], + "北京大学第六医院": ["北医六院", "北大六院", "六院"], + + // ===== 中医与专科 ===== + "中国中医科学院广安门医院": ["广安门", "广安门医院"], + "中国中医科学院西苑医院": ["西苑", "西苑医院"], + "中国中医科学院眼科医院": ["中医眼科"], + "中国中医科学院望京医院": ["望京医院", "望京"], + "北京中医药大学东直门医院": ["东直门", "东直门医院"], + "北京中医药大学东方医院": ["东方医院"], + "北京中医药大学第三附属医院": ["北中医三院", "三附院"], + "北京中医药大学附属护国寺中医院": ["护国寺中医院", "护国寺"], + "中日友好医院": ["中日", "中日友好", "中日医院"], + "北京医院": ["卫生部北京医院"], + "首都医科大学附属北京中医医院": ["宽街中医院", "北京中医医院"], + "首都医科大学附属北京妇产医院": ["北京妇产医院", "妇产医院"], + "首都医科大学附属北京世纪坛医院": ["世纪坛", "世纪坛医院"], + "北京回龙观医院": ["回龙观", "回龙观医院"], + "北京市肛肠医院": ["二龙路医院", "二龙路"], + + // ===== 军队医院:按医学中心主体分别维护旧院名 ===== + "中国人民解放军总医院第一医学中心": ["301医院", "301", "解放军总医院"], + "中国人民解放军总医院第二医学中心": ["301医院", "301", "南楼"], + "中国人民解放军总医院第三医学中心": ["武警总医院", "武警总院"], + "中国人民解放军总医院第四医学中心": ["304医院", "304"], + "中国人民解放军总医院第五医学中心": ["302医院", "302", "307医院", "307"], + "中国人民解放军总医院第六医学中心": ["海军总医院", "海总"], + "中国人民解放军总医院第七医学中心": ["陆军总医院", "陆总"], + "中国人民解放军总医院第八医学中心": ["309医院", "309"], + "中国人民解放军总医院第九医学中心": ["306医院", "306"], + "中国人民解放军第三零五医院": ["305医院", "305"], + "中国人民解放军空军特色医学中心": ["空军总医院", "空总"], + "中国人民解放军火箭军特色医学中心": ["火箭军总医院", "火箭军"], + "中国人民解放军联勤保障部队第九八四医院": ["984医院", "984"], +}; + +// 与主体名称匹配口径一致:去掉括号别名与空白/连接符 +const normalizeEntityName = (value) => + String(value || "") + .trim() + .replace(/(/g, "(") + .replace(/)/g, ")") + .replace(/[((][^))]*[))]/g, "") + .replace(/[\s\--—–·]/g, ""); + +/** + * 根据主体名称取昵称;主体为空时用医院全名兜底 + * @param {string} entityName 主体名称 + * @param {string} name 医院全名 + * @returns {string[]} 昵称列表,未收录时返回空数组 + */ +const resolveHospitalNicknames = (entityName, name = "") => { + const key = normalizeEntityName(entityName) || normalizeEntityName(name); + const nicknames = NICKNAMES_BY_ENTITY[key]; + return Array.isArray(nicknames) ? [...nicknames] : []; +}; + +export { resolveHospitalNicknames }; diff --git a/scripts/backfill_hospital_nicknames.js b/scripts/backfill_hospital_nicknames.js new file mode 100644 index 0000000..37460f9 --- /dev/null +++ b/scripts/backfill_hospital_nicknames.js @@ -0,0 +1,65 @@ +/** + * 一次性回填:按主体名称给存量医院写入常用昵称。 + * + * 用法:在 api/health 下执行 `node scripts/backfill_hospital_nicknames.js` + * 同一主体下的分院/院区共享同一组昵称;未收录的医院写空数组。 + */ +import { DBModel } from "../models/index.js"; +import { resolveHospitalNicknames } from "../resource/hospital_nicknames.js"; + +DBModel.init(); +const conn = DBModel.dbConnection; + +async function backfill() { + const hospitals = await DBModel.Hospital.find( + {}, + { name: 1, entityName: 1, nicknames: 1 } + ).lean(); + + const operations = []; + let withNicknames = 0; + + for (const hospital of hospitals) { + const nicknames = resolveHospitalNicknames(hospital.entityName, hospital.name); + if (nicknames.length > 0) withNicknames += 1; + + const existing = Array.isArray(hospital.nicknames) ? hospital.nicknames : []; + const unchanged = existing.length === nicknames.length + && existing.every((value, index) => value === nicknames[index]); + if (unchanged) continue; + + operations.push({ + updateOne: { + filter: { _id: hospital._id }, + update: { $set: { nicknames } }, + }, + }); + } + + if (operations.length > 0) { + await DBModel.Hospital.bulkWrite(operations); + } + + console.log( + `backfilled: total=${hospitals.length}, withNicknames=${withNicknames}, updated=${operations.length}` + ); + await conn.close(); +} + +if (conn.readyState === 1) { + backfill().catch((error) => { + console.error(error); + process.exit(1); + }); +} else { + conn.once("connected", () => { + backfill().catch((error) => { + console.error(error); + process.exit(1); + }); + }); + conn.once("error", (error) => { + console.error(error); + process.exit(1); + }); +}