This commit is contained in:
lik
2026-09-09 17:36:45 +08:00
parent d6efb180dd
commit 223e0ed2ee
25 changed files with 933 additions and 59 deletions
+38 -3
View File
@@ -1,6 +1,7 @@
"use strict";
import mongoose from "mongoose";
import { pinyin } from "pinyin-pro";
/**
* Employee Schema
@@ -8,8 +9,7 @@ import mongoose from "mongoose";
*/
const EmployeeSchema = mongoose.Schema(
{
// 基础信息
name: { type: String, required: true, comment: "姓名" },
// 工号(业务雇佣标识)
employeeNo: { type: String, default: "", unique: true, sparse: true, comment: "工号" },
// 登录账号(关联用户体系)
@@ -38,6 +38,9 @@ const EmployeeSchema = mongoose.Schema(
// 个人信息
profile: {
name: { type: String, required: true, comment: "姓名" },
pinyin: { type: String, default: "", index: true, comment: "姓名的拼音,用于搜索" },
pinyinFL: { type: String, default: "", index: true, comment: "姓名拼音的首字母,用于搜索" },
sex: { type: String, enum: ["male", "female", "other", ""], default: "", comment: "性别" },
birthday: { type: Date, comment: "出生日期" },
idNumber: { type: String, default: "", comment: "身份证号" },
@@ -98,6 +101,31 @@ const EmployeeSchema = mongoose.Schema(
}
);
// ==================== 拼音生成 ====================
/**
* 姓名 → 全拼(小写、无分隔,如 "张三" → "zhangsan"
*/
const genPinyin = (name) =>
pinyin(String(name), { toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
/**
* 姓名 → 拼音首字母(如 "张三" → "zs"
*/
const genPinyinFL = (name) =>
pinyin(String(name), { pattern: "first", toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
// 保存时自动生成/同步拼音字段
EmployeeSchema.pre("save", function (next) {
const name = this.profile?.name;
if (!name) return next();
if (this.isModified("profile.name") || !this.profile.pinyin || !this.profile.pinyinFL) {
this.profile.pinyin = genPinyin(name);
this.profile.pinyinFL = genPinyinFL(name);
}
next();
});
// ==================== 静态方法 ====================
/**
@@ -156,6 +184,13 @@ EmployeeSchema.statics.createEmployee = async function (data) {
* 更新员工
*/
EmployeeSchema.statics.updateEmployee = async function (id, update) {
const newName = update.name || update["profile.name"];
if (newName) {
update["profile.name"] = newName;
delete update.name;
update["profile.pinyin"] = genPinyin(newName);
update["profile.pinyinFL"] = genPinyinFL(newName);
}
update["meta.updatetime"] = Date.now();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec();
};
@@ -179,7 +214,7 @@ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
// ==================== 索引定义 ====================
EmployeeSchema.index({ name: 1 });
EmployeeSchema.index({ "profile.name": 1 });
EmployeeSchema.index({ employeeNo: 1 });
EmployeeSchema.index({ userId: 1 });
EmployeeSchema.index({ orgId: 1, role: 1, status: 1 });
+47 -5
View File
@@ -1,6 +1,7 @@
"use strict";
import mongoose from "mongoose";
import { pinyin } from "pinyin-pro";
/**
* HealthProfile Schema
@@ -19,6 +20,8 @@ const HealthProfileSchema = mongoose.Schema(
// 患者信息
profile: {
name: { type: String, default: "", comment: "患者姓名" },
pinyin: { type: String, default: "", index: true, comment: "患者姓名全拼(小写无分隔,用于搜索)" },
pinyinFL: { type: String, default: "", index: true, comment: "患者姓名拼音首字母(用于搜索)" },
mobile: { type: String, default: "", index: true, comment: "患者电话" },
sex: { type: String, enum: ["male", "female", ""], default: "", comment: "性别" },
birth: { type: String, default: "", comment: "出生年月(YYYY-MM-DD" },
@@ -56,8 +59,44 @@ const HealthProfileSchema = mongoose.Schema(
}
);
// ==================== 拼音生成 ====================
/**
* 患者姓名 → 全拼(小写、无分隔,如 "张三" → "zhangsan"
*/
const genPinyin = (name) =>
pinyin(String(name), { toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
/**
* 患者姓名 → 拼音首字母(如 "张三" → "zs"
*/
const genPinyinFL = (name) =>
pinyin(String(name), { pattern: "first", toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
// 保存时自动生成/同步姓名拼音字段
HealthProfileSchema.pre("save", function (next) {
const name = this.profile?.name;
if (!name) return next();
if (this.isModified("profile.name") || !this.profile.pinyin || !this.profile.pinyinFL) {
this.profile.pinyin = genPinyin(name);
this.profile.pinyinFL = genPinyinFL(name);
}
next();
});
// ==================== 静态方法 ====================
/**
* 更新包含姓名时同步拼音字段(update 为带 dotted path 的扁平对象)
*/
const syncPinyinOnUpdate = (update) => {
const name = update["profile.name"] ?? update.profile?.name;
if (name) {
update["profile.pinyin"] = genPinyin(name);
update["profile.pinyinFL"] = genPinyinFL(name);
}
};
/**
* 根据用户ID查找健康档案
*/
@@ -85,9 +124,10 @@ HealthProfileSchema.statics.findProfiles = async function (options = {}) {
filter.userId = userId;
}
if (name) {
// 转义正则特殊字符,避免查询报错或 ReDoS
// 转义正则特殊字符,避免查询报错或 ReDoS;姓名支持中文、全拼、拼音首字母匹配
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
filter["profile.name"] = { $regex: escaped, $options: "i" };
const re = { $regex: escaped, $options: "i" };
filter.$or = [{ "profile.name": re }, { "profile.pinyin": re }, { "profile.pinyinFL": re }];
}
if (mobile) {
filter["profile.mobile"] = mobile;
@@ -124,17 +164,19 @@ HealthProfileSchema.statics.createProfile = async function (data) {
};
/**
* 根据ID更新健康档案
* 根据ID更新健康档案(更新姓名时同步拼音字段)
*/
HealthProfileSchema.statics.updateProfileById = async function (id, update) {
syncPinyinOnUpdate(update);
update["meta.updatetime"] = Date.now();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec();
};
/**
* 根据用户ID更新健康档案
* 根据用户ID更新健康档案(更新姓名时同步拼音字段)
*/
HealthProfileSchema.statics.updateProfile = async function (userId, update) {
syncPinyinOnUpdate(update);
update["meta.updatetime"] = Date.now();
return await this.findOneAndUpdate({ userId }, { $set: update }, { new: true }).exec();
};
@@ -151,4 +193,4 @@ HealthProfileSchema.statics.deleteProfileById = async function (id) {
HealthProfileSchema.index({ userId: 1 }, { sparse: true });
HealthProfileSchema.index({ "profile.mobile": 1 });
export { HealthProfileSchema };
export { HealthProfileSchema, genPinyin, genPinyinFL };
+69 -3
View File
@@ -1,6 +1,7 @@
"use strict";
import mongoose from "mongoose";
import { pinyin } from "pinyin-pro";
/**
* Organization Schema
@@ -11,7 +12,9 @@ const OrganizationSchema = mongoose.Schema(
// 基础信息
name: { type: String, required: true, index: true, comment: "机构名称" },
shortName: { type: String, default: "", comment: "机构简称" },
code: { type: String, default: "", unique: true, sparse: true, comment: "机构编码" },
pinyin: { type: String, default: "", index: true, comment: "机构名称全拼(小写无分隔,用于搜索)" },
pinyinFL: { type: String, default: "", index: true, comment: "机构名称拼音首字母(用于搜索)" },
code: { type: String, unique: true, sparse: true, comment: "机构编码(不填时字段缺省,避免与 unique sparse 索引冲突)" },
// 机构类型
type: {
@@ -64,6 +67,39 @@ const OrganizationSchema = mongoose.Schema(
description: { type: String, default: "", comment: "机构简介" },
remark: { type: String, default: "", comment: "备注" },
// 就医信息(agent-first:少字段、大文本;由 AI 助手录入维护,供查询与看病指导)
forAgent: {
updatedAt: { type: Date, comment: "最近更新时间" },
updatedBy: { type: String, default: "", comment: "最近更新人" },
// 医院级总览:门诊时间、挂号渠道、缴费、医保、异地备案、交通停车,一个文本块全包含
overview: { type: String, default: "", comment: "医院级信息全文(门诊/挂号/缴费/医保/交通)" },
// 医院/导引照片
photos: [{
url: { type: String, default: "", comment: "图片链接" },
caption: { type: String, default: "", comment: "说明(医院照片/导引照片 + 描述)" },
}],
// 科室:name 是锚点;detail 包含位置/电话/临床还是医技/就诊提示全文
departments: [{
name: { type: String, comment: "科室名称(锚点)" },
detail: { type: String, default: "", comment: "科室信息全文(位置/电话/类型/就诊提示)" },
photo: { type: String, default: "", comment: "科室位置导引照片链接" },
}],
// 医生:name+department 是锚点;detail 包含职称/擅长/号别/坐班停诊全文
doctors: [{
name: { type: String, comment: "医生姓名(锚点)" },
department: { type: String, comment: "所属科室(锚点)" },
detail: { type: String, default: "", comment: "医生信息全文(职称/擅长/号别/坐班停诊)" },
photo: { type: String, default: "", comment: "医生照片链接" },
}],
// 场景指南:初诊/复诊/住院/检查须知/老人服务等,键为场景名,值为文本,结构自由演化
guides: { type: mongoose.Schema.Types.Mixed, comment: "场景指南(键为场景名,值为文本)" },
},
// 元数据
meta: {
createtime: { type: Date, default: Date.now, comment: "创建时间" },
@@ -78,15 +114,41 @@ const OrganizationSchema = mongoose.Schema(
}
);
// ==================== 拼音生成 ====================
/**
* 机构名称 → 全拼(小写、无分隔,如 "北京协和医院" → "beijingxieheyiyuan"
*/
const genPinyin = (name) =>
pinyin(String(name), { toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
/**
* 机构名称 → 拼音首字母(如 "北京协和医院" → "bjxyy"
*/
const genPinyinFL = (name) =>
pinyin(String(name), { pattern: "first", toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
// 保存时自动生成/同步拼音字段
OrganizationSchema.pre("save", function (next) {
if (!this.name) return next();
if (this.isModified("name") || !this.pinyin || !this.pinyinFL) {
this.pinyin = genPinyin(this.name);
this.pinyinFL = genPinyinFL(this.name);
}
next();
});
// ==================== 静态方法 ====================
/**
* 根据名称模糊查询机构
* 根据名称/拼音/拼音首字母模糊查询机构
*/
OrganizationSchema.statics.findByName = async function (name, options = {}) {
const { page = 1, pageSize = 20 } = options;
const skip = (page - 1) * pageSize;
return await this.find({ name: { $regex: name, $options: "i" } })
const escaped = String(name).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const re = { $regex: escaped, $options: "i" };
return await this.find({ $or: [{ name: re }, { pinyin: re }, { pinyinFL: re }] })
.skip(skip)
.limit(pageSize)
.exec();
@@ -124,6 +186,10 @@ OrganizationSchema.statics.createOrg = async function (data) {
* 更新机构
*/
OrganizationSchema.statics.updateOrg = async function (id, update) {
if (update.name) {
update.pinyin = genPinyin(update.name);
update.pinyinFL = genPinyinFL(update.name);
}
update["meta.updatetime"] = Date.now();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec();
};