tmp
This commit is contained in:
@@ -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 };
|
||||
Reference in New Issue
Block a user