feat(hospital): 新增医院昵称并支持昵称搜索

This commit is contained in:
lik
2026-11-05 14:30:48 +08:00
parent 0e3ab3eeb0
commit 9df847e658
3 changed files with 161 additions and 3 deletions
+8 -3
View File
@@ -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 : "";