feat(hospital): 新增医院基础信息接口与复旦排名
This commit is contained in:
@@ -4,6 +4,7 @@ import mongoose from 'mongoose';
|
||||
import { EscortRecordSchema } from "./schema/escort_record.js"
|
||||
import { HealthProfileSchema } from "./schema/health_profile.js"
|
||||
import { OrganizationSchema } from "./schema/organization.js"
|
||||
import { HospitalSchema } from "./schema/hospital.js"
|
||||
import { EmployeeSchema } from "./schema/employee.js"
|
||||
import { EmployeeReviewSchema } from "./schema/employee_review.js"
|
||||
import { TrainingApplicationSchema } from "./schema/training_application.js"
|
||||
@@ -16,6 +17,7 @@ class MongoDBSchema {
|
||||
this.User = null;
|
||||
this.EscortRecord = null;
|
||||
this.Organization = null;
|
||||
this.Hospital = null;
|
||||
this.Employee = null;
|
||||
this.HealthProfile = null;
|
||||
this.EmployeeReview = null;
|
||||
@@ -48,6 +50,7 @@ class MongoDBSchema {
|
||||
this.EscortRecord = this.dbConnection.model('escort_record', EscortRecordSchema)
|
||||
this.HealthProfile = this.dbConnection.model('health_profile', HealthProfileSchema)
|
||||
this.Organization = this.dbConnection.model('organization', OrganizationSchema)
|
||||
this.Hospital = this.dbConnection.model('hospital', HospitalSchema)
|
||||
this.Employee = this.dbConnection.model('employee', EmployeeSchema)
|
||||
this.EmployeeReview = this.dbConnection.model('employee_review', EmployeeReviewSchema)
|
||||
this.TrainingApplication = this.dbConnection.model('training_application', TrainingApplicationSchema)
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
"use strict";
|
||||
|
||||
import mongoose from "mongoose";
|
||||
import { pinyin } from "pinyin-pro";
|
||||
import { hospitalRanking } from "../../resource/fudan_hospital_ranking.js";
|
||||
|
||||
// 复旦综合榜未上榜医院的默认档位:排在所有上榜医院之后
|
||||
const DEFAULT_FUDAN_RANK = 999;
|
||||
|
||||
// 复旦综合榜只有等级档位(A+++++ / A++++ / A+++),同档不分先后;
|
||||
// 按榜单出现顺序映射成 1/2/3,后续榜单新增更低档位也能自动延续
|
||||
const FUDAN_GRADE_ORDER = new Map();
|
||||
for (const item of hospitalRanking.rankings) {
|
||||
if (!FUDAN_GRADE_ORDER.has(item.grade)) {
|
||||
FUDAN_GRADE_ORDER.set(item.grade, FUDAN_GRADE_ORDER.size + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 名称归一:统一全角括号、去掉括号别名、空白与连接符,便于跨数据源匹配
|
||||
const normalizeHospitalName = (value) =>
|
||||
String(value || "")
|
||||
.trim()
|
||||
.replace(/(/g, "(")
|
||||
.replace(/)/g, ")")
|
||||
.replace(/[((][^))]*[))]/g, "")
|
||||
.replace(/[\s\--—–·]/g, "");
|
||||
|
||||
const FUDAN_RANK_BY_NAME = new Map();
|
||||
for (const item of hospitalRanking.rankings) {
|
||||
const key = normalizeHospitalName(item.name);
|
||||
if (key && !FUDAN_RANK_BY_NAME.has(key)) {
|
||||
FUDAN_RANK_BY_NAME.set(key, {
|
||||
rank: FUDAN_GRADE_ORDER.get(item.grade) || DEFAULT_FUDAN_RANK,
|
||||
grade: item.grade,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析医院对应的复旦综合榜档位
|
||||
* 以主体名称优先;名称带括号别名时先归一,再处理统一前缀与历史别名。
|
||||
* @param {string[]} names 依次尝试的名称
|
||||
* @returns {{rank:number, grade:string}|null}
|
||||
*/
|
||||
const resolveFudanRank = (names = []) => {
|
||||
for (const value of names) {
|
||||
const key = normalizeHospitalName(value);
|
||||
if (!key) continue;
|
||||
|
||||
const direct = FUDAN_RANK_BY_NAME.get(key);
|
||||
if (direct) return direct;
|
||||
|
||||
// 301 各医学中心/院区统一归到复旦榜的「中国人民解放军总医院(301)」
|
||||
if (key.startsWith("中国人民解放军总医院")) {
|
||||
const hit = FUDAN_RANK_BY_NAME.get("中国人民解放军总医院");
|
||||
if (hit) return hit;
|
||||
}
|
||||
|
||||
// 兼容「首都医科大学附属北京积水潭医院」这类多了统一前缀的名称
|
||||
const withoutPrefix = key.replace(/^首都医科大学附属/, "");
|
||||
if (withoutPrefix !== key) {
|
||||
const hit = FUDAN_RANK_BY_NAME.get(withoutPrefix);
|
||||
if (hit) return hit;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Hospital Schema
|
||||
* 医院基础信息:名称、主体名称、拼音、省市、地址、电话、科室、等级、复旦综合榜档位。
|
||||
* 分院/院区按独立医院记录,不设 parentId;名称约定为「主体名称-分院名称」。
|
||||
*/
|
||||
const HospitalSchema = mongoose.Schema(
|
||||
{
|
||||
name: { type: String, required: true, comment: "医院名称,约定为「主体名称-分院名称」" },
|
||||
entityName: { type: String, default: "", index: true, comment: "主体名称;为空时自动取 name 分隔符前的部分" },
|
||||
pinyin: { type: String, default: "", index: true, comment: "医院名称全拼(小写无分隔)" },
|
||||
pinyinFL: { type: String, default: "", index: true, comment: "医院名称拼音首字母" },
|
||||
province: { type: String, default: "", index: true, comment: "省份" },
|
||||
city: { type: String, default: "", index: true, comment: "城市" },
|
||||
address: { type: String, default: "", comment: "医院地址" },
|
||||
phone: { type: String, default: "", comment: "联系电话" },
|
||||
departments: { type: [String], default: [], comment: "科室名称列表" },
|
||||
level: { type: String, default: "", comment: "医院等级,如 三级甲等" },
|
||||
fudanRank: {
|
||||
type: Number,
|
||||
default: DEFAULT_FUDAN_RANK,
|
||||
index: true,
|
||||
comment: "复旦综合排行榜档位,1 最高;未上榜为默认值",
|
||||
},
|
||||
fudanGrade: { type: String, default: "", comment: "复旦综合排行榜等级,如 A+++++" },
|
||||
meta: {
|
||||
createtime: { type: Date, default: Date.now, comment: "创建时间" },
|
||||
updatetime: { type: Date, default: Date.now, comment: "更新时间" },
|
||||
},
|
||||
},
|
||||
{ minimize: false, strict: false, collection: "hospital", timestamps: false }
|
||||
);
|
||||
|
||||
// 名称唯一:分院靠 name 区分,重名会互相污染。
|
||||
// 用命名索引,避免与历史遗留的 name_1 非唯一索引同名冲突
|
||||
HospitalSchema.index({ name: 1 }, { unique: true, name: "uniq_hospital_name" });
|
||||
|
||||
// ==================== 拼音生成 ====================
|
||||
|
||||
// 根据名称生成全拼与首字母(小写、无分隔)
|
||||
const buildPinyinFields = (name) => {
|
||||
const text = String(name || "");
|
||||
return {
|
||||
pinyin: pinyin(text, { toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, ""),
|
||||
pinyinFL: pinyin(text, { pattern: "first", toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, ""),
|
||||
};
|
||||
};
|
||||
|
||||
// 名称分隔符:兼容中文输入法下常见的横线写法
|
||||
const ENTITY_SEPARATOR = /[--—–]/;
|
||||
|
||||
// 从「主体名称-分院名称」中取主体名称;没有分隔符(独立医院)时返回整个名称
|
||||
const deriveEntityName = (name) => {
|
||||
const text = String(name || "").trim();
|
||||
if (!text) return "";
|
||||
const index = text.search(ENTITY_SEPARATOR);
|
||||
return index > 0 ? text.slice(0, index).trim() : text;
|
||||
};
|
||||
|
||||
// 保存时自动生成主体名称与拼音字段
|
||||
HospitalSchema.pre("save", function (next) {
|
||||
const name = String(this.name || "").trim();
|
||||
if (!String(this.entityName || "").trim()) this.entityName = deriveEntityName(name);
|
||||
if (this.isModified("name") || !this.pinyin || !this.pinyinFL) {
|
||||
const fields = buildPinyinFields(name);
|
||||
this.pinyin = fields.pinyin;
|
||||
this.pinyinFL = fields.pinyinFL;
|
||||
}
|
||||
|
||||
const fudan = resolveFudanRank([this.entityName, name]);
|
||||
this.fudanRank = fudan ? fudan.rank : DEFAULT_FUDAN_RANK;
|
||||
this.fudanGrade = fudan ? fudan.grade : "";
|
||||
next();
|
||||
});
|
||||
|
||||
// ==================== 静态方法 ====================
|
||||
|
||||
// 关键词 → 不区分大小写的包含匹配正则(转义特殊字符,避免查询报错或 ReDoS)
|
||||
const toRegex = (value) => ({
|
||||
$regex: String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"),
|
||||
$options: "i",
|
||||
});
|
||||
|
||||
// 搜索词限长,避免超长正则拖慢查询
|
||||
const MAX_SEARCH_LENGTH = 50;
|
||||
const normalizeSearch = (value) => String(value ?? "").trim().slice(0, MAX_SEARCH_LENGTH);
|
||||
|
||||
/**
|
||||
* 查询医院列表(支持分页、关键词与主体名称筛选)
|
||||
* keyword 同时匹配名称、主体名称、全拼与拼音首字母;
|
||||
* entityName 对主体名称做包含匹配,用于列出同一主体的所有分院/院区。
|
||||
* @returns {Promise<Object>} `{ list, total, page, pageSize }`
|
||||
*/
|
||||
HospitalSchema.statics.findHospitals = async function (options = {}) {
|
||||
const { page = 1, pageSize = 20 } = options;
|
||||
const keyword = normalizeSearch(options.keyword);
|
||||
const entityName = normalizeSearch(options.entityName);
|
||||
const filter = {};
|
||||
|
||||
if (entityName) filter.entityName = toRegex(entityName);
|
||||
if (keyword) {
|
||||
const re = toRegex(keyword);
|
||||
filter.$or = [{ name: re }, { entityName: re }, { pinyin: re }, { pinyinFL: re }];
|
||||
}
|
||||
|
||||
const skip = (page - 1) * pageSize;
|
||||
const [list, total] = await Promise.all([
|
||||
// 复旦榜上榜医院优先,同档按拼音排序;未上榜的默认值排在最后
|
||||
this.find(filter).sort({ fudanRank: 1, pinyin: 1, name: 1 }).skip(skip).limit(pageSize).exec(),
|
||||
this.countDocuments(filter).exec(),
|
||||
]);
|
||||
|
||||
return { list, total, page, pageSize };
|
||||
};
|
||||
|
||||
// 根据ID查找医院
|
||||
HospitalSchema.statics.findHospitalById = async function (id) {
|
||||
return await this.findById(id).exec();
|
||||
};
|
||||
|
||||
// 创建医院
|
||||
HospitalSchema.statics.createHospital = async function (data) {
|
||||
data.meta = { createtime: Date.now(), updatetime: Date.now() };
|
||||
const doc = new this(data);
|
||||
return await doc.save();
|
||||
};
|
||||
|
||||
/**
|
||||
* 根据ID更新医院
|
||||
* 名称变更时同步拼音;主体名称显式传空时回退为名称前缀。
|
||||
* 未显式传主体名称时,仅当旧主体名称也是自动派生值才跟随新名称,
|
||||
* 避免覆盖人工指定的主体(如把独立医院并入某主体)。
|
||||
*/
|
||||
HospitalSchema.statics.updateHospitalById = async function (id, update) {
|
||||
if (update.name !== undefined) {
|
||||
const name = String(update.name ?? "").trim();
|
||||
update.name = name;
|
||||
Object.assign(update, buildPinyinFields(name));
|
||||
}
|
||||
|
||||
if (update.entityName !== undefined) {
|
||||
const entityName = String(update.entityName ?? "").trim();
|
||||
if (entityName) {
|
||||
update.entityName = entityName;
|
||||
} else if (update.name !== undefined) {
|
||||
update.entityName = deriveEntityName(update.name);
|
||||
} else {
|
||||
// 只清空主体名称、没带名称时,用库里现有名称回退
|
||||
const current = await this.findById(id).select("name").lean();
|
||||
update.entityName = deriveEntityName(current?.name);
|
||||
}
|
||||
} else if (update.name !== undefined) {
|
||||
// 名称变了但没传主体名称:旧主体名称是自动派生的,才跟着新名称一起变
|
||||
const current = await this.findById(id).select("name entityName").lean();
|
||||
if (current && current.entityName === deriveEntityName(current.name)) {
|
||||
update.entityName = deriveEntityName(update.name);
|
||||
}
|
||||
}
|
||||
|
||||
// 名称或主体名称变更后重算复旦榜档位;未上榜写默认值
|
||||
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;
|
||||
const fudan = resolveFudanRank([finalEntityName, finalName]);
|
||||
update.fudanRank = fudan ? fudan.rank : DEFAULT_FUDAN_RANK;
|
||||
update.fudanGrade = fudan ? fudan.grade : "";
|
||||
}
|
||||
|
||||
update["meta.updatetime"] = Date.now();
|
||||
return await this.findByIdAndUpdate(id, { $set: update }, { new: true, runValidators: true }).exec();
|
||||
};
|
||||
|
||||
// 根据ID删除医院
|
||||
HospitalSchema.statics.deleteHospitalById = async function (id) {
|
||||
return await this.findByIdAndDelete(id).exec();
|
||||
};
|
||||
|
||||
export { HospitalSchema, DEFAULT_FUDAN_RANK, resolveFudanRank };
|
||||
Reference in New Issue
Block a user